from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles from fastapi.responses import FileResponse from app.api.v1 import health, example, auth, keys, usage from app.config.settings import settings from app.core.database import init_db import os @asynccontextmanager async def lifespan(app: FastAPI): await init_db() yield def create_app() -> FastAPI: app = FastAPI( title="SuperDream", description="SuperDream API", version="0.1.0", lifespan=lifespan, ) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Register API routers app.include_router(health.router, prefix="/api/v1", tags=["health"]) app.include_router(auth.router, prefix="/api/v1") app.include_router(keys.router, prefix="/api/v1") app.include_router(usage.router, prefix="/api/v1") app.include_router(example.router, prefix="/api/v1", tags=["example"]) # Serve static files static_dir = os.path.join(os.path.dirname(__file__), "static") if os.path.exists(static_dir): app.mount("/static", StaticFiles(directory=static_dir), name="static") # Serve frontend dist in production frontend_dist = os.path.join(os.path.dirname(__file__), "..", "frontend", "dist") if os.path.exists(frontend_dist): app.mount("/assets", StaticFiles(directory=os.path.join(frontend_dist, "assets")), name="frontend-assets") @app.get("/{full_path:path}") async def serve_frontend(full_path: str): file_path = os.path.join(frontend_dist, full_path) if os.path.isfile(file_path): return FileResponse(file_path) return FileResponse(os.path.join(frontend_dist, "index.html")) return app app = create_app()