# USACO site The public face of this repo, served at `/usaco/` on adam-french.co.uk. It shows the solved problems: each solution, its sample input, saved tests and notes, and its git history, plus a stats page with a solve calendar. When the site's admin is logged in, it also shows problems in `solving/` and `skipped/`, and lets them: - mark a problem solved, skipped or back in progress - set its link - write notes It is one Go binary that serves a REST API and the built Vue SPA. It runs as one container behind the site's nginx and borrows the site's login, the same arrangement as job-tracker (adamf/job-tracker), which it is modelled on. ## Git is the store There is no database. The CLI's layout is the data model (see the root README.md): - `solving/`, `solved/` and `skipped//`, each holding the solution files - `.in` for the sample input - `tests/N.in` and `N.out` - `.url` for the problem link - `notes.md` for notes The server keeps a clone of adamf/usaco in `$DATA_DIR/repo` and indexes it into memory. - **Reading.** A Gitea push webhook triggers a fetch and hard reset, then a re-index, so a `usaco done && usaco sync` is live in seconds. A pull every `PULL_INTERVAL` (10m) catches missed hooks. The clone is a cache: the remote always wins. - **Writing.** An admin edit runs under one lock: 1. sync 2. change the files 3. commit as `GIT_AUTHOR_NAME` 4. push The commit subjects match the CLI's: "Solve ", "Skip ", "Reopen " and "Update ". If someone pushed in between and the push is rejected, the edit is replayed once on the new tip. A second rejection returns 409. - **Dates come from git, not mtimes.** A problem's solve date is the commit that moved it into `solved/`. Its first and last dates are its first and last commits. The site commits, so the laptop must pull before pushing. `usaco sync` does `pull --rebase --autostash` and then `push`. ## Layout ``` cmd/usacosite/ main: reads the environment, wires everything, registers routes internal/auth/ is this request the admin? (asks the site backend; copied from job-tracker) internal/repo/ the clone: sync, and edit-commit-push with one replay on rejection internal/index/ reads the clone into memory: problems, files, tests, git dates internal/handlers/ REST API, Gitea webhook, SPA file server web/ Vue 3 + Vite SPA, built for base /usaco/ ``` ## Routes Everything is served under `BASE_PATH` (`/usaco`). nginx proxies the prefix through unchanged. | Path | Auth | What | |---|---|---| | `/usaco/`, `/usaco/p/`, `/usaco/stats` | none | SPA | | `GET /usaco/api/problems` | open; the admin also gets unsolved problems | the list | | `GET /usaco/api/problems/` | open for solved problems; 404 for others unless admin | one problem, with files, tests and history | | `GET /usaco/api/me` | open | `{admin}`, so the SPA knows whether to show the edit controls | | `PUT /usaco/api/problems//meta` | admin | `{url?, notes?}`: writes `.url` and `notes.md` (blank removes the file) | | `POST /usaco/api/problems//status` | admin | `{to: solving\|solved\|skipped}`: moves the directory | | `POST /usaco/api/hooks/gitea` | HMAC (`X-Gitea-Signature`) | queue a pull | | `GET /usaco/api/health` | open | 200 once the clone is indexed | Auth works as in job-tracker: - `internal/auth` forwards the browser's cookies to the site backend's `/auth/validate-admin`. - A "yes" is cached for 30 seconds. - A refused admin action sends the browser to `/admin/login?redirect=…`. ## Development ``` make check # go vet + go test, and a vite build make dev # docker compose up --build: this checkout's problems, no login ``` Then open http://localhost:8080/usaco/. **`make dev`.** The dev compose clones the repo root, mounted read-only, and sets `GIT_PUSH=false`. Edits made in the browser become commits in the container's clone and nowhere else. Only committed work shows up. **Hot reload.** Run the server with `make dev`, then run `cd web && npm run dev`. Vite proxies `/usaco/api` to `localhost:8080`. **Without Docker:** ``` go build -o /tmp/usacosite ./cmd/usacosite && (cd web && npx vite build) DEV_MODE=true AUTH_MODE=dev GIT_PUSH=false GIT_REMOTE_URL=file://$PWD/.. \ DATA_DIR=/tmp/usaco-data STATIC_DIR=$PWD/web/dist /tmp/usacosite ``` Every variable is listed and explained in `.env.example`. ## Running inside web_server The site's `docker-compose.yml` runs the published image as service `usaco`, with an explicit `environment:` list. The site's nginx routes `/usaco/` to it. `./usaco-data` is mounted at `/data` and holds only the clone, so deleting it is safe: the next start re-clones. The container runs as uid 1000, so run `chown 1000 usaco-data` on the host. ### Deploying A push to `main` that touches `site/` or `.gitea/workflows/` runs `.gitea/workflows/ci.yaml` at the repo root. It does the following: 1. Tests. 2. Builds the image from `site/` and pushes it as `adam-french.co.uk/adamf/usaco:` and `:latest`. 3. Rolls the site onto the new image and waits for the healthcheck. Problem commits don't match the path filter, so they never rebuild the image. The webhook brings them in. By hand on the Pi: ``` cd ~/deploy/web_server && docker compose pull usaco && docker compose up -d --no-deps usaco ``` To pin a version, set `USACO_IMAGE_TAG=` in the site's `.env`. ### One-off setup The registry setup is shared with job-tracker (see its README, "One-off: registry setup"). The Pi is already logged in, and the same `REGISTRY_USER` and `REGISTRY_TOKEN` work here. 1. **CI secrets.** In this repo's Settings → Actions → Secrets, add `REGISTRY_USER` and `REGISTRY_TOKEN`. Use the same values as job-tracker. 2. **A git token for the server.** In Gitea, go to Settings → Applications and create a token with **repository: read and write**. Put it in the site's `.env` as `USACO_GIT_TOKEN`. 3. **The data directory.** On the Pi, in `~/deploy/web_server`, run `mkdir usaco-data && sudo chown 1000:1000 usaco-data`. 4. **A webhook secret.** Generate one, for example with `openssl rand -hex 32`, and put it in the site's `.env` as `USACO_WEBHOOK_SECRET`. 5. **Deploy web_server** with the `usaco` service and the `/usaco/` route. Then push this repo, so CI publishes the first image. 6. **The webhook.** In this repo's Settings → Webhooks → Add webhook → Gitea, set: - Target URL: `http://usaco:8080/usaco/api/hooks/gitea` - Method: POST - Content type: application/json - Secret: the value of `USACO_WEBHOOK_SECRET` - Trigger: push events Gitea only delivers to Docker-network hosts that are on `[webhook] ALLOWED_HOST_LIST`. web_server's compose sets that to `external,usaco`. Use "Test delivery": the response should be `202`, and `docker logs usaco` should show a new `[index]` line. ## Things that are easy to get wrong - **`BASE_PATH` and Vite's `base` must agree.** The SPA's asset URLs and router base are fixed at build time. - **Edits must decide from disk, not the index.** `repo.Mutate` may run an edit twice, the second time on a newer tip. The handlers re-locate the problem in the clone each time with `findOnDisk`. - **`tests/*.out` used to be gitignored** by the root `*.out` rule. The root `.gitignore` now re-includes `**/tests/*.out`. A test whose expected output is shown as "Not saved." predates that change or never had one.