import time from contextlib import asynccontextmanager from fastapi import FastAPI from sqlalchemy import text from app.routes import router as api_router from app.utils.cors import setup_cors from app.utils.create_tables import init_db from app.utils.db import engine from app.utils.logger_cfg import logger app_start_time = time.perf_counter() logger.debug("App start timestamp recorded") @asynccontextmanager async def lifespan(app: FastAPI): logger.info("Application startup initiated") try: async with engine.begin() as conn: logger.debug("Executing test query: SELECT 1") db_status = await conn.scalar(text("SELECT 1")) logger.info("Database connection successful: {}", db_status) logger.debug("Starting database table initialization") await init_db() logger.info("Database initialization completed") except Exception: logger.exception("Database startup failed") raise elapsed = time.perf_counter() - app_start_time app.state.startup_elapsed = elapsed app.state.startup_completed = True logger.success( "Application startup completed in {:.2f} ms ({:.6f} s)", elapsed * 1000, elapsed, ) yield logger.info("Application shutdown initiated") try: await engine.dispose() logger.info("Database engine disposed successfully") except Exception: logger.exception("Error during shutdown") app = FastAPI(lifespan=lifespan) setup_cors(app) app.include_router(api_router, prefix="/api")