from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.openapi.utils import get_openapi import uvicorn from contextlib import asynccontextmanager import os from app.core.config import settings from app.api.v1.api import api_router from app.api.health import health_router @asynccontextmanager async def lifespan(app: FastAPI): # Startup logic yield # Shutdown logic app = FastAPI( title=settings.PROJECT_NAME, description=settings.PROJECT_DESCRIPTION, version=settings.VERSION, openapi_url="/openapi.json", docs_url="/docs", redoc_url="/redoc", lifespan=lifespan, ) # Set up CORS if settings.BACKEND_CORS_ORIGINS: app.add_middleware( CORSMiddleware, allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Include routers app.include_router(health_router) app.include_router(api_router, prefix=settings.API_V1_STR) # Override the default OpenAPI schema to add our custom info def custom_openapi(): if app.openapi_schema: return app.openapi_schema openapi_schema = get_openapi( title=settings.PROJECT_NAME, version=settings.VERSION, description=settings.PROJECT_DESCRIPTION, routes=app.routes, ) # Custom OpenAPI schema modifications can be added here app.openapi_schema = openapi_schema return app.openapi_schema app.openapi = custom_openapi if __name__ == "__main__": uvicorn.run( "main:app", host="0.0.0.0", port=int(os.getenv("PORT", "8000")), reload=settings.DEBUG, )