import asyncio from contextlib import asynccontextmanager from prometheus_fastapi_instrumentator import Instrumentator from .routers.api import generation, pos from fastapi import FastAPI from .routers.api import jobs from .routers import media as media_router from .routers.api.main import api_router from .routers.bff.main import bff_router from .outbound.storage_factory import init_storage from .observability import setup_observability from . import worker @asynccontextmanager async def lifespan(app: FastAPI): init_storage() setup_observability(app) worker_task = asyncio.create_task(worker.worker_loop()) yield worker_task.cancel() try: await worker_task except asyncio.CancelledError: pass app = FastAPI(title="Language Learning API", lifespan=lifespan) app.include_router(api_router) app.include_router(bff_router) app.include_router(media_router.router) Instrumentator().instrument(app).expose(app, should_gzip=True) @app.get("/health") async def health() -> dict: return {"status": "ok"}