Spaces:
Sleeping
Sleeping
| import os | |
| import traceback | |
| from contextlib import asynccontextmanager | |
| from fastapi import FastAPI, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.responses import FileResponse, JSONResponse | |
| from .database import engine, Base | |
| from .routers import auth_router, proxy_config_router, proxy_endpoint_router | |
| async def lifespan(app: FastAPI): | |
| Base.metadata.create_all(bind=engine) | |
| yield | |
| app = FastAPI( | |
| title="Anthropic ↔ OpenAI Proxy", | |
| description="Converts Anthropic API calls to OpenAI-compatible backend calls via LiteLLM", | |
| version="1.0.0", | |
| lifespan=lifespan, | |
| ) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Global exception handler — 500 errors ka actual reason dikhayega | |
| async def global_exception_handler(request: Request, exc: Exception): | |
| return JSONResponse( | |
| status_code=500, | |
| content={ | |
| "detail": str(exc), | |
| "type": type(exc).__name__, | |
| "trace": traceback.format_exc().splitlines()[-5:], # last 5 lines | |
| }, | |
| ) | |
| app.include_router(auth_router.router) | |
| app.include_router(proxy_config_router.router) | |
| app.include_router(proxy_endpoint_router.router) | |
| _static_dir = os.path.join(os.path.dirname(__file__), "static") | |
| app.mount("/static", StaticFiles(directory=_static_dir), name="static") | |
| def serve_ui(): | |
| return FileResponse(os.path.join(_static_dir, "index.html")) | |
| def health(): | |
| return { | |
| "status": "ok", | |
| "db": str(engine.url), | |
| } |