81 lines
2.4 KiB
Python
81 lines
2.4 KiB
Python
|
|
"""
|
||
|
|
TCG Vault Backend API
|
||
|
|
Main FastAPI application entry point
|
||
|
|
"""
|
||
|
|
|
||
|
|
from fastapi import FastAPI
|
||
|
|
from fastapi.middleware.cors import CORSMiddleware
|
||
|
|
from contextlib import asynccontextmanager
|
||
|
|
import os
|
||
|
|
|
||
|
|
from app.database import init_db
|
||
|
|
from app.routers import cards, collections, decks, ocr, ai, pricing, auth
|
||
|
|
|
||
|
|
|
||
|
|
@asynccontextmanager
|
||
|
|
async def lifespan(app: FastAPI):
|
||
|
|
"""Handle application startup and shutdown"""
|
||
|
|
# Startup
|
||
|
|
print("🚀 Starting TCG Vault API...")
|
||
|
|
init_db()
|
||
|
|
print("✅ Database initialized")
|
||
|
|
yield
|
||
|
|
# Shutdown
|
||
|
|
print("👋 Shutting down TCG Vault API...")
|
||
|
|
|
||
|
|
|
||
|
|
app = FastAPI(
|
||
|
|
title="TCG Vault API",
|
||
|
|
description="Trading Card Collection Manager with OCR, AI, and Pricing",
|
||
|
|
version="1.0.0",
|
||
|
|
lifespan=lifespan
|
||
|
|
)
|
||
|
|
|
||
|
|
# CORS middleware - handle both development and production
|
||
|
|
allowed_origins = [
|
||
|
|
"http://localhost:3000", # React dev server
|
||
|
|
"http://127.0.0.1:3000", # Alternative dev server
|
||
|
|
]
|
||
|
|
|
||
|
|
# Add production origins from environment variable
|
||
|
|
if os.getenv("ALLOWED_ORIGINS"):
|
||
|
|
production_origins = os.getenv("ALLOWED_ORIGINS").split(",")
|
||
|
|
allowed_origins.extend([origin.strip() for origin in production_origins])
|
||
|
|
|
||
|
|
# In production, allow common deployment patterns
|
||
|
|
if os.getenv("RAILWAY_ENVIRONMENT") or os.getenv("RENDER") or os.getenv("VERCEL"):
|
||
|
|
# Allow common hosting patterns (you'll set specific domains via ALLOWED_ORIGINS)
|
||
|
|
pass
|
||
|
|
|
||
|
|
app.add_middleware(
|
||
|
|
CORSMiddleware,
|
||
|
|
allow_origins=allowed_origins,
|
||
|
|
allow_credentials=True,
|
||
|
|
allow_methods=["*"],
|
||
|
|
allow_headers=["*"],
|
||
|
|
)
|
||
|
|
|
||
|
|
# Include routers
|
||
|
|
app.include_router(auth.router, prefix="/api/v1/auth", tags=["Authentication"])
|
||
|
|
app.include_router(cards.router, prefix="/api/v1/cards", tags=["Cards"])
|
||
|
|
app.include_router(collections.router, prefix="/api/v1/collections", tags=["Collections"])
|
||
|
|
app.include_router(decks.router, prefix="/api/v1/decks", tags=["Decks"])
|
||
|
|
app.include_router(ocr.router, prefix="/api/v1/ocr", tags=["OCR"])
|
||
|
|
app.include_router(ai.router, prefix="/api/v1/ai", tags=["AI"])
|
||
|
|
app.include_router(pricing.router, prefix="/api/v1/pricing", tags=["Pricing"])
|
||
|
|
|
||
|
|
|
||
|
|
@app.get("/")
|
||
|
|
async def root():
|
||
|
|
"""Health check endpoint"""
|
||
|
|
return {"message": "TCG Vault API is running! 🃏"}
|
||
|
|
|
||
|
|
|
||
|
|
@app.get("/health")
|
||
|
|
async def health_check():
|
||
|
|
"""Detailed health check"""
|
||
|
|
return {
|
||
|
|
"status": "healthy",
|
||
|
|
"service": "TCG Vault API",
|
||
|
|
"version": "1.0.0"
|
||
|
|
}
|