import os from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from prometheus_fastapi_instrumentator import Instrumentator from slowapi import _rate_limit_exceeded_handler as _slowapi_handler from slowapi.errors import RateLimitExceeded from starlette.requests import Request from starlette.responses import Response from app.api.limiter import limiter from app.api.routes import router def handle_rate_limit(request: Request, exc: Exception) -> Response: return _slowapi_handler(request, exc) # type: ignore[arg-type, return-value, unused-ignore] app = FastAPI( title="AI Code Review Agent", description="Multi-agent AI platform that simulates a software engineering team", version="0.1.0", ) Instrumentator().instrument(app).expose(app, endpoint="/metrics/prometheus") app.state.limiter = limiter app.add_exception_handler(RateLimitExceeded, handle_rate_limit) # type: ignore[arg-type, unused-ignore] # Allow Gradio frontend to talk to FastAPI app.add_middleware( CORSMiddleware, allow_origins=os.getenv("ALLOWED_ORIGINS", "http://localhost:7860").split(","), allow_methods=["POST", "GET"], allow_headers=["X-API-Key", "Content-Type"], ) app.include_router(router, prefix="/api/v1") @app.get("/") def root() -> dict[str, str]: return {"message": "AI Code Review Agent", "version": "0.1.0", "docs": "/docs"}