from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from app.api.router import api_router from app.core.config import get_settings from app.core.http.middleware import request_id_middleware from app.core.http.responses import MaskedJSONResponse from app.application.scheduling import attach_scheduler from app.modules.observability.runtime import attach_api_heartbeat def _allow_cors_credentials(cors_origins: list[str]) -> bool: return "*" not in cors_origins def create_app() -> FastAPI: """Create and configure the FastAPI application.""" settings = get_settings() app = FastAPI( title=settings.app_name, debug=settings.debug, version="0.1.0", description=( "AI integration layer for company lifecycle management, existing MySQL " "project systems, Feishu, OpenClaw, Hermes, and model providers." ), default_response_class=MaskedJSONResponse, ) app.middleware("http")(request_id_middleware) app.add_middleware( CORSMiddleware, allow_origins=settings.cors_origins, allow_credentials=_allow_cors_credentials(settings.cors_origins), allow_methods=["*"], allow_headers=["*"], ) app.include_router(api_router, prefix=settings.api_prefix) attach_api_heartbeat(app) attach_scheduler(app) return app app = create_app()