import logging from collections.abc import AsyncGenerator from contextlib import asynccontextmanager from fastapi import FastAPI from src.gateway.config import get_gateway_config from src.gateway.routers import artifacts, models # Configure logging logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", ) logger = logging.getLogger(__name__) @asynccontextmanager async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]: """Application lifespan handler.""" config = get_gateway_config() logger.info(f"Starting API Gateway on {config.host}:{config.port}") # Initialize MCP tools at startup try: from src.mcp import initialize_mcp_tools await initialize_mcp_tools() except Exception as e: logger.warning(f"Failed to initialize MCP tools: {e}") yield logger.info("Shutting down API Gateway") def create_app() -> FastAPI: """Create and configure the FastAPI application. Returns: Configured FastAPI application instance. """ app = FastAPI( title="DeerFlow API Gateway", description="API Gateway for DeerFlow - provides custom endpoints (models, artifacts). LangGraph requests are handled by nginx.", version="0.1.0", lifespan=lifespan, ) # CORS is handled by nginx - no need for FastAPI middleware # Include routers # Models API is mounted at /api/models app.include_router(models.router) # Artifacts API is mounted at /api/threads/{thread_id}/artifacts app.include_router(artifacts.router) @app.get("/health") async def health_check() -> dict: """Health check endpoint.""" return {"status": "healthy", "service": "deer-flow-gateway"} return app # Create app instance for uvicorn app = create_app()