from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from sqlalchemy import text from .api import router from .config import get_settings from .db import Base, SessionLocal, engine from .seeds import seed_database @asynccontextmanager async def lifespan(_: FastAPI): Base.metadata.create_all(engine) with SessionLocal() as session: seed_database(session) yield app = FastAPI( title="PK Lab API", version="0.1.0", docs_url="/docs", redoc_url=None, lifespan=lifespan, ) settings = get_settings() app.add_middleware( CORSMiddleware, allow_origins=settings.cors_origin_list, allow_credentials=False, allow_methods=["GET", "POST", "OPTIONS"], allow_headers=["Authorization", "Content-Type"], ) app.include_router(router) @app.get("/health") def health() -> dict[str, str]: with SessionLocal() as session: session.execute(text("SELECT 1")) return {"status": "ok"}