Backend API: auth, projects/images/annotations, R2 presign, tracing, thumbnails, pagination, tests, docs
This commit is contained in:
parent
5c5deed2f5
commit
f8ed08b854
49 changed files with 2626 additions and 0 deletions
69
src/main.py
Normal file
69
src/main.py
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import Depends, FastAPI, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from shared import events
|
||||
from shared.logger import get_logger
|
||||
from shared.tracing import TraceMiddleware
|
||||
from shared.traffic import TrafficMiddleware
|
||||
from src.api.v1 import activities, annotations, auth, events as events_api, images, projects, system
|
||||
from src.api.v1.auth import users_router
|
||||
from src.core.config import get_settings
|
||||
from src.core.security import get_current_user
|
||||
|
||||
s = get_settings()
|
||||
log = get_logger("danable")
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
drainer = asyncio.create_task(events.drain_forever())
|
||||
yield
|
||||
drainer.cancel()
|
||||
|
||||
|
||||
app = FastAPI(title=s.app_name, lifespan=lifespan)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["http://localhost:5173", "http://localhost:3000"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
if s.traffic.get("enabled", True):
|
||||
app.add_middleware(TrafficMiddleware)
|
||||
app.add_middleware(TraceMiddleware)
|
||||
|
||||
|
||||
@app.exception_handler(RuntimeError)
|
||||
async def _config_error(request: Request, exc: RuntimeError):
|
||||
# Missing R2 creds etc: 503 with actionable message instead of 500.
|
||||
return JSONResponse(status_code=503, detail=str(exc))
|
||||
|
||||
|
||||
prefix = s.api_prefix
|
||||
authed: list = [Depends(get_current_user)]
|
||||
|
||||
# Public: health/metrics, auth, client-event ingest (session telemetry).
|
||||
app.include_router(system.router, prefix=prefix)
|
||||
app.include_router(auth.router, prefix=prefix)
|
||||
app.include_router(users_router, prefix=prefix, dependencies=authed)
|
||||
app.include_router(events_api.router, prefix=prefix)
|
||||
app.include_router(activities.router, prefix=prefix, dependencies=authed)
|
||||
|
||||
# Domain: require JWT.
|
||||
app.include_router(projects.router, prefix=prefix, dependencies=authed)
|
||||
app.include_router(images.router, prefix=prefix, dependencies=authed)
|
||||
app.include_router(annotations.router, prefix=prefix, dependencies=authed)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
log.info("serving api prefix=%s", prefix)
|
||||
return {"name": s.app_name, "docs": "/docs", "health": f"{prefix}/health"}
|
||||
Loading…
Add table
Add a link
Reference in a new issue