Structure en couches api / services / repositories / models, sens de dependance unique, une session SQLAlchemy async injectee par dependance. - Python 3.14, dependances gerees par uv et verrouillees dans uv.lock - FastAPI expose par une factory : aucune configuration lue a l'import, ce qui rend tests et migrations independants de l'environnement - Settings Pydantic, APP_SECRET_KEY et DATABASE_URL sans valeur par defaut - Sondes /health/live et /health/ready, metriques Prometheus sur /metrics - Lint et format ruff, mypy strict, pytest avec couverture - Alembic branche sur DATABASE_URL et non sur alembic.ini - Image Docker multi-stage, utilisateur non root, sonde de sante integree
55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
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(
|
|
"Demarrage 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
|