43 lines
1018 B
Python
43 lines
1018 B
Python
from collections.abc import AsyncGenerator
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
|
|
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)
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
@app.get("/")
|
|
def health() -> dict[str, str]:
|
|
logger.info("Health check endpoint accessed")
|
|
return {"status": "healthy"}
|
|
|
|
return app
|
|
|
|
|
|
app = create_app()
|