51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from app.api.v1.router import api_router
|
|
from app.core.config import settings
|
|
from app.crud.user import ensure_admin_exists
|
|
from app.db.base import Base
|
|
from app.db.session import SessionLocal, engine
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(_: FastAPI):
|
|
# Ensure models are imported so metadata is populated
|
|
from app.models import user as user_model # noqa: F401
|
|
|
|
if settings.ENV == "development":
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
async with SessionLocal() as session:
|
|
await ensure_admin_exists(session)
|
|
yield
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
app = FastAPI(
|
|
title="CTMS API",
|
|
version="0.1.0",
|
|
debug=settings.ENV == "development",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
@app.get("/health", tags=["health"])
|
|
async def health() -> dict[str, str]:
|
|
return {"status": "ok"}
|
|
|
|
app.include_router(api_router, prefix="/api/v1")
|
|
return app
|
|
|
|
|
|
app = create_app()
|