from collections.abc import AsyncIterator from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from prometheus_fastapi_instrumentator import Instrumentator from app.api.v1.router import api_router from app.core.config import Settings, get_settings from app.core.logging import configure_logging, get_logger from app.db.session import get_engine logger = get_logger(__name__) @asynccontextmanager async def lifespan(_: FastAPI) -> AsyncIterator[None]: settings = get_settings() logger.info( "Démarrage de %s %s en environnement %s", settings.name, settings.version, settings.env ) yield await get_engine().dispose() def create_app(settings: Settings | None = None) -> FastAPI: resolved = settings or get_settings() configure_logging(resolved) application = FastAPI( title=resolved.name, version=resolved.version, debug=resolved.debug, lifespan=lifespan, docs_url=None if resolved.is_production else "/docs", redoc_url=None if resolved.is_production else "/redoc", openapi_url=None if resolved.is_production else "/openapi.json", ) if resolved.allowed_origins: application.add_middleware( CORSMiddleware, allow_origins=resolved.allowed_origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) Instrumentator().instrument(application).expose( application, endpoint="/metrics", include_in_schema=False ) application.include_router(api_router, prefix=resolved.api_prefix) return application