from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from app.core.config import settings from app.api.v1.api import api_router from app.db.base import engine, Base async def init_db(): async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) @asynccontextmanager async def lifespan(application: FastAPI): await init_db() yield app = FastAPI( title=settings.PROJECT_NAME, version=settings.VERSION, description=settings.DESCRIPTION, openapi_url=f"{settings.API_V1_STR}/openapi.json", lifespan=lifespan, ) # Set up CORS middleware app.add_middleware( CORSMiddleware, allow_origins=settings.ALLOWED_ORIGINS, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Include API router app.include_router(router=api_router, prefix=settings.API_V1_STR) if __name__ == "__main__": import uvicorn uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)