Split the job tracker out of web_server into its own app
Test and publish / test (push) Successful in 6m39s
Test and publish / publish (push) Failing after 0s

The applications tracker, leads inbox, email pipeline and capture userscript
move here from web_server's backend, Python service and Vue app, as one Go
binary that serves its own Vue SPA under /jobs/.

- The Go services, resolvers and tests move over almost unchanged.
- The three Claude calls are ported from Python to Go (internal/claude),
  with the same prompts, per-model request shaping and error messages.
- There is no login: internal/auth asks the site backend's
  /auth/validate-admin with the visitor's cookies, and relays refreshed
  cookies back.
- A Settings page replaces the admin-panel capture token card and adds the
  Microsoft sign-in link and a manual email sync.
- The userscript is 2.0.0, at /jobs/, and migrates a saved API base.
- Dockerfile, a standalone compose with Postgres, Makefile, and a Gitea CI
  workflow that tests and publishes the image.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-14 16:24:16 +01:00
co-authored by Claude Opus 5.5
commit 67cb8137f9
88 changed files with 22715 additions and 0 deletions
+165
View File
@@ -0,0 +1,165 @@
# Job tracker
A private tracker for job applications, served at `/jobs/` on adam-french.co.uk. It has four parts:
- **Applications** (`/jobs/`): the list, the pipeline view and the Quick Reference snippets.
- **Leads inbox** (`/jobs/leads`): postings the server finds on Greenhouse, Lever and Ashby boards and in Adzuna and Reed searches, scored by Claude against a search profile.
- **Email pipeline**: reads the mailbox (Microsoft Graph or IMAP), has Claude classify recruitment mail, and creates or advances applications.
- **Capture userscript** (`web/public/job-capture.user.js`): adds the posting open in the browser.
It was split out of the `web_server` repo, where it lived inside the site's Go backend, Python service and Vue app. It is now one Go binary that serves its own Vue SPA. It runs as one container behind the site's nginx and borrows the site's login.
## Layout
```
cmd/jobtracker/ main: reads the environment, wires everything, registers routes
internal/auth/ is this request the admin? (asks the site backend)
internal/claude/ the three Claude calls: read a posting, score a lead, classify an email
internal/graph/ gqlgen GraphQL: schema/*.graphql is the source of truth
internal/handlers/ REST: capture routes, capture token, email controls, SPA file server
internal/models/ GORM models (six tables)
internal/services/ lead pipeline, job sources, email sync (Graph + IMAP), upsert, status vocabulary
web/ Vue 3 + Vite SPA, built for base /jobs/
```
## Routes
Everything is served under `BASE_PATH` (`/jobs`). nginx proxies the prefix through unchanged.
| Path | Auth | What |
|---|---|---|
| `/jobs/`, `/jobs/leads`, `/jobs/settings` | none (the data behind them is admin-only) | SPA |
| `POST /jobs/api/graphql` | `@admin` on every field | everything the pages show and edit |
| `/jobs/api/capture`, `/capture/status`, `/capture/extract` | bearer capture token | the userscript |
| `/jobs/api/admin/capture-token` (GET/POST/DELETE) | admin | the token, from the Settings page |
| `GET /jobs/api/email/status`, `POST /jobs/api/email/sync` | admin | email controls on the Settings page |
| `GET /jobs/api/email/callback` | open (Microsoft redirects here) | Graph OAuth |
| `GET /jobs/api/health` | open | healthcheck: process up and database reachable |
## Auth
There are no accounts here. `internal/auth` forwards the browser's `Cookie` header to the site backend's `GET /auth/validate-admin`, which is the same check nginx uses for `/radio/admin` and `/sb`, and treats a 2xx as "admin".
- **Expired access token:** `validate-admin` falls back to the refresh token and answers with new cookies. Those `Set-Cookie` headers are relayed to the browser.
- **Caching:** a "yes" is cached for 30 seconds per cookie, so logging out takes up to 30 seconds to reach the tracker.
- **Lapsed session:** the SPA sends the browser to the site's `/admin/login?redirect=/jobs/...` (`web/src/session.js`).
`AUTH_MODE=dev` admits everyone, for the standalone compose. It is refused unless `DEV_MODE=true`.
## Development
```
make check # go vet + go test, and a vite build
make generate # after editing internal/graph/schema/*.graphql
make dev # docker compose up --build: tracker + Postgres, seeded, no login
```
Then open http://localhost:8080/jobs/. Set `CLAUDE_API_KEY` in your shell or a `.env` to try the Claude features.
For SPA hot reload, run `make dev` (or the Go binary) and then `cd web && npm run dev`. Vite proxies `/jobs/api` to `localhost:8080`; see `web/vite.config.js`.
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 `jobs`, with an explicit `environment:` list rather than `env_file`, so the site's other secrets stay out of the container. The site's nginx routes `/jobs/` to it. The site's Postgres container holds the data, in its own `jobs` database and role. `./jobs-data` is mounted at `/data`; the container runs as uid 1000, so `chown 1000 jobs-data` on the host.
### Deploying
A push to `main` here runs `.gitea/workflows/ci.yaml`. It tests, then builds and pushes `adam-french.co.uk/adamf/job-tracker:<sha>` and `:latest`.
To roll the site onto a new image:
```
cd ~/deploy/web_server && docker compose pull jobs && docker compose up -d jobs
```
To pin a version instead of `latest`, set `JOBS_IMAGE_TAG=<sha>` in the site's `.env`.
### One-off: registry setup
The image lives in this Gitea's container registry. Gitea is served under `/gitea/`, but Docker always talks to `/v2/` at the root of the host. So the site's nginx needs a `/v2/` route before anything can be pushed or pulled.
1. **Check Gitea's registry is on.** On the Pi, bypassing nginx:
```
curl -si http://localhost:3000/v2/ | head -5
```
You want a `401` with `WWW-Authenticate: Bearer realm="https://adam-french.co.uk/gitea/v2/token"`. A `404` means packages are off: add `[packages]` with `ENABLED = true` to Gitea's `app.ini.template` and recreate Gitea.
2. **Route `/v2/` to Gitea.** Add this to `nginx/nginx_routes.conf.template` in web_server, beside the `/gitea/` block, and deploy. Token requests go to the realm Gitea advertises (`/gitea/v2/token`), which the existing `/gitea/` route already serves.
```nginx
location /v2/ {
client_max_body_size 0; # image layers exceed the global 50M
proxy_request_buffering off; # stream uploads instead of spooling them
proxy_read_timeout 900s;
proxy_send_timeout 900s;
proxy_pass $upstream_gitea;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
```
Then `curl -si https://adam-french.co.uk/v2/` should give the same `401` as step 1. A `200` with HTML means the request is still reaching the site's SPA.
3. **Make a token.** In Gitea, go to Settings → Applications and create a token with **package: read and write**.
4. **Round trip a throwaway image** on the Pi:
```
docker login adam-french.co.uk -u adamf # the token is the password
docker pull alpine:3.22
docker tag alpine:3.22 adam-french.co.uk/adamf/registry-test:1
docker push adam-french.co.uk/adamf/registry-test:1
docker rmi adam-french.co.uk/adamf/registry-test:1
docker pull adam-french.co.uk/adamf/registry-test:1
```
Delete the test package afterwards, from your profile → Packages → registry-test → Settings. If the push fails, the error says where:
- **`unauthorized`:** the token's scope is wrong, or `/gitea/v2/token` isn't reachable (it should answer `401`, not `404`).
- **`413`:** the `client_max_body_size 0` line isn't in effect.
- **HTML or "invalid character '<'":** `/v2/` is still reaching the SPA.
5. **Give CI the credentials.** In this repo's Settings → Actions → Secrets, add `REGISTRY_USER` (`adamf`) and `REGISTRY_TOKEN` (the token). The next push to `main` publishes the image.
The Pi also has to be logged in to pull the image. The `docker login` in step 4 covers that; it is stored in that user's `~/.docker/config.json`.
### One-off cutover from the old in-backend tracker
Do the registry setup above first: step 6 needs the image.
Run these on the Pi, in this order:
1. **Stop the old schedulers.** On the old backend, set `EMAIL_SYNC_ENABLED=false` and `JOB_LEADS_ENABLED=false`, so mail and boards are never processed twice.
2. **Create the database and role.** Use the values you put in the site's `.env` as `POSTGRES_JOBS_USER`, `POSTGRES_JOBS_PASSWORD` and `POSTGRES_JOBS_DB`:
```
docker exec db psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" \
-c "CREATE ROLE jobs LOGIN PASSWORD '…'" -c "CREATE DATABASE jobs OWNER jobs"
```
3. **Copy the six tables**, with their schema and sequences. AutoMigrate then finds nothing to do:
```
docker exec db pg_dump -U "$POSTGRES_USER" -d "$POSTGRES_DB" --no-owner --no-privileges \
-t job_applications -t job_app_references -t processed_emails \
-t job_sources -t job_leads -t job_search_profiles \
| docker exec -i db psql -U jobs -d jobs -v ON_ERROR_STOP=1
```
Then compare `SELECT count(*)` on each table in the two databases.
4. **Move the token files:**
```
mkdir -p jobs-data
cp backend/token/msgraph_token.json backend/token/job_capture_token.json jobs-data/
sudo chown -R 1000:1000 jobs-data
```
5. **Point Microsoft at the new callback.** In the Entra app registration, add the redirect URI `https://adam-french.co.uk/jobs/api/email/callback`, and set `MSGRAPH_REDIRECT_URI` to the same value.
6. **Deploy and check.** Deploy `web_server` with the `jobs` service and routes, then re-enable the two schedulers, now read by `jobs`. Check:
- the tracker loads
- "Check now" on Leads works
- "Sync now" on Settings works
- a capture from a real posting appears in the tracker
The old tables stay in the site database until they are dropped by hand. GORM never drops anything, so this is safe until you are sure.
## Things that are easy to get wrong
- **The status vocabulary has copies.** `enum JobStatus` (GraphQL) and `services.statusOrder` are tied by a test, and so are `claude.EmailStatuses` and the classifier prompt. Two copies are not checked and must be edited by hand: `web/src/views/jobStatus.js` and `STATUS_LABELS` in the userscript.
- **Bump the userscript's `@version` on every edit**, or installed copies never update.
- **The Graph OAuth `state` is a fixed string** (carried over unchanged from the old backend), and `/email/callback` is open. A forged callback can only link a mailbox the attacker controls, but a per-request nonce, like the site's Spotify flow uses, would close that.
- **`BASE_PATH` and Vite's `base` must agree.** The SPA's asset URLs and router base are fixed at build time.