""" FastAPI Application Entry Point Enterprise-grade FastAPI application with proper structure and middleware """ import logging import uvicorn from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from src.config.config import settings from src.config.migrate import migrate_sync # Configure logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) logger = logging.getLogger(__name__) # Import routers # ========== DATABASE MIGRATIONS ========== # Run database migrations on application startup logger.info("🔄 Running database migrations...") if not migrate_sync(settings.DATABASE_URL): logger.warning("⚠️ Some migrations failed, but application will continue") # ========== FASTAPI APPLICATION INITIALIZATION ========== # Initialize FastAPI application app = FastAPI( title=settings.APP_NAME, version=settings.APP_VERSION, description="", docs_url="/docs" if settings.DEBUG else None, redoc_url="/redoc" if settings.DEBUG else None, openapi_url="/openapi.json" if settings.DEBUG else None, ) # CORS Middleware Configuration app.add_middleware( CORSMiddleware, allow_origins=settings.CORS_ORIGINS if isinstance(settings.CORS_ORIGINS, list) else ["*"], allow_credentials=True, allow_methods=settings.CORS_METHODS if isinstance(settings.CORS_METHODS, list) else ["*"], allow_headers=settings.CORS_HEADERS if isinstance(settings.CORS_HEADERS, list) else ["*"], ) # Include routers import src.models # Trigger model registration from src.routes.index import router as api_router app.include_router(api_router) @app.on_event("startup") async def startup_event(): """ Application startup event handler Performs initialization tasks before accepting requests """ logger.info("🚀 FastAPI application started successfully") logger.info(f"📚 API Documentation available at: http://localhost:{settings.PORT}/docs") @app.on_event("shutdown") async def shutdown_event(): """ Application shutdown event handler Performs cleanup tasks when application stops """ logger.info("🛑 FastAPI application shutting down") @app.get("/") async def root(): """ Root endpoint - API information and health status """ return { "message": "Welcome to API", "version": settings.APP_VERSION, "docs": "/docs" if settings.DEBUG else "disabled", "status": "running" } @app.get("/health") async def health_check(): """ Health check endpoint for monitoring """ return { "status": "healthy", "app": settings.APP_NAME, "version": settings.APP_VERSION } if __name__ == "__main__": uvicorn.run( "main:app", host=settings.HOST, port=settings.PORT, reload=settings.DEBUG )