|
|
"""Main FastAPI application factory for KGraph-MCP.""" |
|
|
|
|
|
import logging |
|
|
from contextlib import asynccontextmanager |
|
|
|
|
|
from fastapi import FastAPI |
|
|
from fastapi.middleware.cors import CORSMiddleware |
|
|
|
|
|
from .core.config import get_settings |
|
|
from .core.dependencies import initialize_services |
|
|
from .routes import api_router |
|
|
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
@asynccontextmanager |
|
|
async def lifespan(app: FastAPI): |
|
|
"""Application lifespan events.""" |
|
|
|
|
|
logger.info("Starting up KGraph-MCP API...") |
|
|
settings = get_settings() |
|
|
|
|
|
|
|
|
logging.basicConfig( |
|
|
level=getattr(logging, settings.log_level.upper()), |
|
|
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", |
|
|
) |
|
|
|
|
|
|
|
|
success = initialize_services() |
|
|
if not success: |
|
|
logger.warning( |
|
|
"Some services failed to initialize - continuing with limited functionality" |
|
|
) |
|
|
|
|
|
logger.info("β
KGraph-MCP API startup complete!") |
|
|
|
|
|
yield |
|
|
|
|
|
|
|
|
logger.info("Shutting down KGraph-MCP API...") |
|
|
|
|
|
|
|
|
def create_app() -> FastAPI: |
|
|
"""Create and configure the FastAPI application.""" |
|
|
settings = get_settings() |
|
|
|
|
|
app = FastAPI( |
|
|
title=settings.app_title, |
|
|
description=settings.app_description, |
|
|
version=settings.app_version, |
|
|
debug=settings.debug, |
|
|
lifespan=lifespan, |
|
|
) |
|
|
|
|
|
|
|
|
app.add_middleware( |
|
|
CORSMiddleware, |
|
|
allow_origins=settings.cors_origins, |
|
|
allow_credentials=settings.cors_credentials, |
|
|
allow_methods=settings.cors_methods, |
|
|
allow_headers=settings.cors_headers, |
|
|
) |
|
|
|
|
|
|
|
|
app.include_router(api_router) |
|
|
|
|
|
return app |
|
|
|
|
|
|
|
|
|
|
|
app = create_app() |
|
|
|