diff --git a/generator/README.md b/generator/README.md new file mode 100644 index 0000000..8eb6951 --- /dev/null +++ b/generator/README.md @@ -0,0 +1,56 @@ +# Generator Service + +Serves precomputed SVGs to the public internet. Holds user settings and +the widgets cache. Runs a build worker + cron in the background. Also +serves the React SPA (frontend sources live under `frontend/`). + +## Run locally (3 processes) + + cd generator + pip install -r requirements.txt + + # API + FETCHER_URL=http://localhost:5001 FETCHER_INTERNAL_TOKEN=dev python -m src.api + # Build worker + FETCHER_URL=http://localhost:5001 FETCHER_INTERNAL_TOKEN=dev python -m src.worker + # Poll cron + FETCHER_URL=http://localhost:5001 FETCHER_INTERNAL_TOKEN=dev python -m src.cron + +## Frontend (dev) + + cd generator/frontend && npm install && npm run dev + # Vite dev server proxies /api to the Flask app. + +## Test + + cd generator && pytest + +## Docker + +Multi-stage (builds the frontend with node, serves everything from Python): + + docker build -t ghstats-generator . + docker run --rm -e FETCHER_URL=http://fetcher:5001 -e FETCHER_INTERNAL_TOKEN=dev \ + -p 5002:5002 -v $(pwd)/data:/app/data ghstats-generator + # Worker: override CMD to ["python","-m","src.worker"] + # Cron: override CMD to ["python","-m","src.cron"] + +## Endpoints + +All API endpoints are under `/api/`. The root and `/assets/*` serve the SPA. + +| Method | Path | Auth | Description | +|---|---|---|---| +| GET | /api/health | none | health | +| GET | /api/ | none | composite SVG; auto-enrolls unknown users | +| GET | /api//.svg | none | individual widget SVG | +| POST | /api/enroll | none | explicit enroll (body: {username}) | +| GET | /api//settings | stub | current settings | +| PATCH | /api//settings | stub | update settings (enqueues rebuild) | +| POST | /api//refresh | stub | one-shot re-fetch + rebuild | + +Responses carry `X-Widget-Status: ready | building | rate_limited | not_found`. + +## Env + +See `src/config.py`. diff --git a/generator/tests/integration/test_end_to_end.py b/generator/tests/integration/test_end_to_end.py new file mode 100644 index 0000000..a470d36 --- /dev/null +++ b/generator/tests/integration/test_end_to_end.py @@ -0,0 +1,39 @@ +"""End-to-end: api + worker in-process + mocked fetcher HTTP.""" +import os, tempfile +import pytest +import responses +from src import api as apimod, db as dbmod, worker + + +@pytest.fixture +def env(monkeypatch): + with tempfile.TemporaryDirectory() as d: + monkeypatch.setattr(dbmod, "SETTINGS_DB_PATH", os.path.join(d, "s.db")) + monkeypatch.setattr(dbmod, "WIDGETS_DB_PATH", os.path.join(d, "w.db")) + monkeypatch.setattr(apimod.config, "FETCHER_URL", "http://fetcher-mock") + monkeypatch.setattr(apimod.config, "FETCHER_INTERNAL_TOKEN", "t") + dbmod.init_dbs() + yield + + +@responses.activate +def test_first_request_builds_then_serves_ready(env): + responses.add( + responses.GET, "http://fetcher-mock/data/alice", + json={"data": {"user": {"login": "alice"}, "repos": [], "events": [], + "commits": [], "total_commits": 0, "recent_commits": 0, + "total_prs": 0, "collaborators_data": [], "avatar_b64": ""}, + "payload_hash": "h1"}, status=200, + ) + client = apimod.app.test_client() + + r1 = client.get("/api/alice") + assert r1.headers["X-Widget-Status"] == "building" + + for _ in range(5): + if not worker.process_one(): + break + + r2 = client.get("/api/alice") + assert r2.headers["X-Widget-Status"] == "ready" + assert r2.data.startswith(b"