from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from starlette.responses import JSONResponse import uvicorn from storage.database import init_db from exceptions import ServiceNotConfiguredError from middleware.config_guard import ConfigGuardMiddleware from api import debates_router, api_keys_router, models_router, setup_router app = FastAPI( title="Dialectica - Multi-Model Debate Framework", description="A framework for structured debates between multiple language models", version="0.1.0" ) # Add CORS middleware app.add_middleware( CORSMiddleware, allow_origins=["*"], # Allow all origins for development allow_credentials=True, allow_methods=["*"], # Allow all methods allow_headers=["*"], # Allow all headers # Add exposed headers to allow frontend to access response headers expose_headers=["Access-Control-Allow-Origin", "Access-Control-Allow-Credentials"] ) # Config guard: return 503 for business routes when DB not configured app.add_middleware(ConfigGuardMiddleware) @app.exception_handler(ServiceNotConfiguredError) async def not_configured_handler(request, exc): return JSONResponse( status_code=503, content={"error_code": "SERVICE_NOT_CONFIGURED", "detail": str(exc)}, ) @app.on_event("startup") def startup_event(): """Initialize database on startup (skipped if not configured).""" init_db() # Register routers app.include_router(debates_router) app.include_router(api_keys_router) app.include_router(models_router) app.include_router(setup_router) @app.get("/") async def root(): return {"message": "Welcome to Dialectica - Multi-Model Debate Framework"} if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=8000)