from contextlib import asynccontextmanager from fastapi import Depends, FastAPI, HTTPException from . import containers, stats from .security import require_api_key @asynccontextmanager async def lifespan(app: FastAPI): # Nothing to set up — importing .security already loaded/generated the # API key, and stats.collect() is stateless — but FastAPI's lifespan is # the natural place a future migration/warmup step would go. yield app = FastAPI(title="VPS Dashboard Stats Agent", version="1.0.0", lifespan=lifespan) @app.get("/api/health") def health(): return {"status": "ok"} @app.get("/api/stats", dependencies=[Depends(require_api_key)]) def get_stats(): return stats.collect() @app.get("/api/containers", dependencies=[Depends(require_api_key)]) def get_containers(): # Opt-in: only works when the agent can reach the Docker API (see README.md). try: return {"containers": containers.collect()} except containers.DockerUnavailable as exc: raise HTTPException(status_code=503, detail=str(exc))