| import logging |
| import sys |
| from collections.abc import AsyncGenerator |
| from contextlib import asynccontextmanager |
|
|
| from fastapi import FastAPI |
|
|
| from src.config.app_config import get_app_config |
| from src.gateway.config import get_gateway_config |
| from src.gateway.routers import artifacts, mcp, memory, models, skills, uploads |
|
|
| |
| 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.""" |
|
|
| |
| try: |
| get_app_config() |
| logger.info("Configuration loaded successfully") |
| except Exception as e: |
| logger.error(f"Failed to load configuration: {e}") |
| sys.exit(1) |
| config = get_gateway_config() |
| logger.info(f"Starting API Gateway on {config.host}:{config.port}") |
|
|
| |
| |
| |
| |
|
|
| 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=""" |
| ## DeerFlow API Gateway |
| |
| API Gateway for DeerFlow - A LangGraph-based AI agent backend with sandbox execution capabilities. |
| |
| ### Features |
| |
| - **Models Management**: Query and retrieve available AI models |
| - **MCP Configuration**: Manage Model Context Protocol (MCP) server configurations |
| - **Memory Management**: Access and manage global memory data for personalized conversations |
| - **Skills Management**: Query and manage skills and their enabled status |
| - **Artifacts**: Access thread artifacts and generated files |
| - **Health Monitoring**: System health check endpoints |
| |
| ### Architecture |
| |
| LangGraph requests are handled by nginx reverse proxy. |
| This gateway provides custom endpoints for models, MCP configuration, skills, and artifacts. |
| """, |
| version="0.1.0", |
| lifespan=lifespan, |
| docs_url="/docs", |
| redoc_url="/redoc", |
| openapi_url="/openapi.json", |
| openapi_tags=[ |
| { |
| "name": "models", |
| "description": "Operations for querying available AI models and their configurations", |
| }, |
| { |
| "name": "mcp", |
| "description": "Manage Model Context Protocol (MCP) server configurations", |
| }, |
| { |
| "name": "memory", |
| "description": "Access and manage global memory data for personalized conversations", |
| }, |
| { |
| "name": "skills", |
| "description": "Manage skills and their configurations", |
| }, |
| { |
| "name": "artifacts", |
| "description": "Access and download thread artifacts and generated files", |
| }, |
| { |
| "name": "uploads", |
| "description": "Upload and manage user files for threads", |
| }, |
| { |
| "name": "health", |
| "description": "Health check and system status endpoints", |
| }, |
| ], |
| ) |
|
|
| |
|
|
| |
| |
| app.include_router(models.router) |
|
|
| |
| app.include_router(mcp.router) |
|
|
| |
| app.include_router(memory.router) |
|
|
| |
| app.include_router(skills.router) |
|
|
| |
| app.include_router(artifacts.router) |
|
|
| |
| app.include_router(uploads.router) |
|
|
| @app.get("/health", tags=["health"]) |
| async def health_check() -> dict: |
| """Health check endpoint. |
| |
| Returns: |
| Service health status information. |
| """ |
| return {"status": "healthy", "service": "deer-flow-gateway"} |
|
|
| return app |
|
|
|
|
| |
| app = create_app() |
|
|