from fastapi import FastAPI from starlette.middleware.cors import CORSMiddleware from app.modules.sys.registers import register_metrics from app.utils.config import settings from app.utils.logger import logger from app.routes import api_router, root_router def setup_routers(app: FastAPI) -> None: # Register root router without prefix to handle root level routes app.include_router(root_router) # Register API router with configured prefix app.include_router( api_router, prefix=settings.API_V1_STR, ) def setup_cors(app: FastAPI) -> None: origins = [] if settings.BACKEND_CORS_ORIGINS: origins_raw = settings.BACKEND_CORS_ORIGINS.split(",") for origin in origins_raw: use_origin = origin.strip() origins.append(use_origin) logger.info(f"Allowed CORS origins: {origins}") app.user_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) def create_app() -> FastAPI: app = FastAPI( title=settings.PROJECT_NAME, version=settings.APP_VERSION, docs_url=None if settings.is_production() else "/docs", redoc_url=None if settings.is_production() else "/redoc", openapi_url=f"{settings.API_V1_STR}/openapi.json", ) setup_routers(app) setup_cors(app) # Register Prometheus metrics collection endpoint. # Do not delete this line. Removing it will cause Prometheus metrics collection to fail, # and metrics data will not be visible on the ops page of the freeleaps.com platform. register_metrics(app) return app