65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from app.api.middleware.rate_limit import TierRateLimitMiddleware
|
|
from app.api.middleware.sanitizer import SanitizerMiddleware
|
|
from app.config.settings import settings
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# Startup: initialise DB connection pool and agent registry
|
|
from app.core.agent_registry import registry # noqa: F401 — triggers module load
|
|
import app.agents # noqa: F401 — triggers @registry.register decorators
|
|
|
|
yield
|
|
|
|
# Shutdown: dispose SQLAlchemy connection pool
|
|
from app.db import engine
|
|
await engine.dispose()
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
app = FastAPI(
|
|
title="Adiuva Cloud API",
|
|
version="0.1.0",
|
|
docs_url="/docs" if settings.ENV == "dev" else None,
|
|
redoc_url=None,
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.CORS_ORIGINS,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
# Middleware stack (Starlette inserts at position 0, so last-added = outermost).
|
|
# Request flow: TierRateLimit → Sanitizer → CORS → Router
|
|
# Response flow: Router → CORS → Sanitizer → TierRateLimit
|
|
app.add_middleware(SanitizerMiddleware)
|
|
app.add_middleware(TierRateLimitMiddleware)
|
|
|
|
from app.api.routes import auth, backup, billing, chat, plans, plugins, storage, vectors
|
|
|
|
app.include_router(auth.router, prefix="/api/v1")
|
|
app.include_router(chat.router, prefix="/api/v1")
|
|
app.include_router(plans.router, prefix="/api/v1")
|
|
app.include_router(storage.router, prefix="/api/v1")
|
|
app.include_router(vectors.router, prefix="/api/v1")
|
|
app.include_router(backup.router, prefix="/api/v1")
|
|
app.include_router(plugins.router, prefix="/api/v1")
|
|
app.include_router(billing.router, prefix="/api/v1")
|
|
|
|
@app.get("/api/v1/health", tags=["health"])
|
|
async def health() -> dict:
|
|
return {"status": "ok", "version": app.version}
|
|
|
|
return app
|
|
|
|
|
|
app = create_app()
|