import os from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from src.controllers import api_router, websocket_router from src.config import logger, DatabaseConfig from src.middlewares import AuthenticationMiddleware @asynccontextmanager async def lifespan(app: FastAPI): try: logger.info("Starting up the application...") mongo_config = DatabaseConfig() await mongo_config.init_beanie() logger.info("Application started successfully...") yield except Exception as e: logger.error(f"Error during startup: {str(e)}") raise finally: logger.info("Shutting down the application...") logger.info("Application shutdown complete.") app = FastAPI(lifespan=lifespan) app.add_middleware( CORSMiddleware, allow_origins=os.getenv( "CORS_ALLOW_ORIGINS", "http://localhost, http://127.0.0.1" ).split(", "), allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.get("/") async def check_health(): return {"response": "Service is healthy!"} app.include_router(api_router, prefix="/api") app.include_router(websocket_router, prefix="/ws")