from __future__ import annotations import asyncio from collections.abc import AsyncIterator, Awaitable, Callable from contextlib import asynccontextmanager import logging from typing import Any import httpx from fastapi import FastAPI, HTTPException, Request from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse, PlainTextResponse from fastapi.staticfiles import StaticFiles from pydantic import BaseModel, Field from .config import Settings, load_settings from .hygiene import SCORER_ID, SCORER_VERSION, score_dict from .leaderboard import BOARD_FILE, SEED_REPOS, Leaderboard, RankBody, RankError from .telemetry import RateLimiter, ScanBody, StatsStore, SyncedFile, Telemetry, preset_digests log = logging.getLogger("space") MAX_BODY_CHARS = 200_000 ROOT_NOTE = "codehygiene Space API is running. Build space/web to serve the UI here.\n" SCAN_BATCHES_PER_MINUTE = 10 SCAN_BATCHES_PER_MINUTE_ALL = 600 RANKS_PER_MINUTE = 2 RANKS_PER_MINUTE_ALL = 20 STATS_HEADERS = {"Cache-Control": "public, max-age=60"} class ScoreBody(BaseModel): filename: str = "" content: str = Field(max_length=MAX_BODY_CHARS) threshold: float = Field(0.55, ge=0.0, le=1.0) def caller_of(request: Request) -> str: """Address used only for the rate-limit bucket check, then discarded.""" forwarded = request.headers.get("x-forwarded-for", "").split(",")[0].strip() return forwarded or (request.client.host if request.client else "") def error_response(error: RankError) -> JSONResponse: return JSONResponse(error.payload(), status_code=error.status) def create_app( settings: Settings | None = None, github_transport: httpx.AsyncBaseTransport | None = None, board: Leaderboard | None = None, ) -> FastAPI: settings = settings or load_settings() telemetry = Telemetry(excluded=preset_digests(settings.presets_path)) board = board or Leaderboard(settings, github_transport, telemetry=telemetry) if board.telemetry is None: board.telemetry = telemetry dataset = (settings.stats_dataset_repo, settings.hf_token) synced = ( SyncedFile(StatsStore(settings.stats_path, *dataset), telemetry), SyncedFile(StatsStore(settings.leaderboard_path, *dataset, filename=BOARD_FILE), board), ) scan_limit = RateLimiter(SCAN_BATCHES_PER_MINUTE) scan_limit_all = RateLimiter(SCAN_BATCHES_PER_MINUTE_ALL) rank_limit = RateLimiter(RANKS_PER_MINUTE) rank_limit_all = RateLimiter(RANKS_PER_MINUTE_ALL) async def sync_files() -> None: while True: await asyncio.sleep(settings.stats_sync_seconds) for file in synced: await file.flush() @asynccontextmanager async def lifespan(_: FastAPI) -> AsyncIterator[None]: for file in synced: await file.load() tasks = [asyncio.create_task(sync_files())] if settings.leaderboard_seed: tasks.append(asyncio.create_task(board.maintain(SEED_REPOS, settings.leaderboard_seed_pause))) yield for task in tasks: task.cancel() await board.close() for file in synced: await file.flush() app = FastAPI(title="codehygiene", lifespan=lifespan) app.state.telemetry = telemetry app.state.board = board app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]) @app.middleware("http") async def static_cache_headers( request: Request, call_next: Callable[[Request], Awaitable[Any]] ) -> Any: response = await call_next(request) if request.method not in ("GET", "HEAD") or request.url.path.startswith("/api/"): return response if "cache-control" in response.headers: return response content_type = response.headers.get("content-type", "") if content_type.startswith("text/html"): response.headers["Cache-Control"] = "no-cache" elif request.url.path.startswith("/fonts/") or "v=" in request.url.query: response.headers["Cache-Control"] = "public, max-age=31536000, immutable" else: response.headers["Cache-Control"] = "no-cache" return response @app.get("/api/health") def health() -> dict[str, Any]: return { "status": "ok", "scorer_version": SCORER_VERSION, "scorer_id": SCORER_ID, } @app.get("/api/stats") def stats() -> JSONResponse: body = { **telemetry.public(), "leaderboard_entries": len(board.current()), "scorer_version": SCORER_VERSION, } return JSONResponse(body, headers=STATS_HEADERS) @app.get("/api/leaderboard") def leaderboard() -> JSONResponse: return JSONResponse(board.public(), headers=STATS_HEADERS) @app.post("/api/leaderboard/rank", response_model=None) async def leaderboard_rank(body: RankBody, request: Request) -> dict[str, Any] | JSONResponse: if not (rank_limit.allow(caller_of(request)) and rank_limit_all.allow("*")): return error_response(RankError(429, "busy", "Too many rank requests. Try again shortly.")) try: return await board.rank(body.owner, body.repo) except RankError as error: return error_response(error) except httpx.HTTPError as error: log.warning("GitHub archive request failed: %s", type(error).__name__) return error_response(RankError(502, "github_error", "GitHub could not be reached.")) @app.post("/api/telemetry/scan") def telemetry_scan(body: ScanBody, request: Request) -> dict[str, int]: allowed = scan_limit.allow(caller_of(request)) and scan_limit_all.allow("*") if not allowed: raise HTTPException(status_code=429, detail="Too many scan reports.") return {"counted": telemetry.record_scan(body)} @app.post("/api/score") def score(body: ScoreBody) -> dict[str, Any]: return score_dict(body.content, body.threshold) if (settings.web_dist / "index.html").is_file(): app.mount("/", StaticFiles(directory=settings.web_dist, html=True), name="web") else: @app.get("/", response_class=PlainTextResponse) def root_note() -> str: return ROOT_NOTE return app app = create_app()