from collections.abc import AsyncGenerator from contextlib import asynccontextmanager from fastapi import FastAPI from app.api import api_router from app.core.database import init_db from app.core.logging import get_logger, setup_logging from app.middleware.logging import LoggingMiddleware @asynccontextmanager async def lifespan(_app: FastAPI) -> AsyncGenerator[None, None]: """Application lifespan context manager for setup and teardown.""" setup_logging() logger = get_logger(__name__) logger.info("Starting application") await init_db() logger.info("Database initialized") yield logger.info("Shutting down application") def create_app() -> FastAPI: """Create and configure the FastAPI application.""" app = FastAPI(lifespan=lifespan) app.add_middleware(LoggingMiddleware) # Include API routes app.include_router(api_router) return app app = create_app()