from collections.abc import AsyncIterator from contextlib import asynccontextmanager from dotenv import load_dotenv _ = load_dotenv() # load .env before db.py reads DATABASE_URL from fastapi import Depends, FastAPI # noqa: E402 from fastapi.middleware.cors import CORSMiddleware # noqa: E402 from sqlmodel import Session # noqa: E402 from db import create_db_and_tables, engine # noqa: E402 from innercontext.api import ( # noqa: E402 admin, ai_logs, auth, health, inventory, products, profile, routines, skincare, ) from innercontext.api.auth_deps import get_current_user # noqa: E402 from innercontext.services.pricing_jobs import enqueue_pricing_recalc # noqa: E402 @asynccontextmanager async def lifespan(_app: FastAPI) -> AsyncIterator[None]: create_db_and_tables() try: with Session(engine) as session: _ = enqueue_pricing_recalc(session) session.commit() except Exception as exc: # pragma: no cover print(f"[startup] failed to enqueue pricing recalculation job: {exc}") yield app = FastAPI( title="innercontext API", lifespan=lifespan, redirect_slashes=False, ) app.add_middleware( CORSMiddleware, # ty: ignore[invalid-argument-type] allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) protected = [Depends(get_current_user)] app.include_router(auth.router, prefix="/auth", tags=["auth"]) app.include_router(admin.router, prefix="/admin", tags=["admin"]) app.include_router( products.router, prefix="/products", tags=["products"], dependencies=protected, ) app.include_router( inventory.router, prefix="/inventory", tags=["inventory"], dependencies=protected, ) app.include_router( profile.router, prefix="/profile", tags=["profile"], dependencies=protected, ) app.include_router( health.router, prefix="/health", tags=["health"], dependencies=protected, ) app.include_router( routines.router, prefix="/routines", tags=["routines"], dependencies=protected, ) app.include_router( skincare.router, prefix="/skincare", tags=["skincare"], dependencies=protected, ) app.include_router( ai_logs.router, prefix="/ai-logs", tags=["ai-logs"], dependencies=protected, ) @app.get("/health-check") def health_check(): return {"status": "ok"}