- Refuse non-GET requests whose Origin (or Sec-Fetch-Site) is not the apex or www. SameSite=Lax lets sibling subdomains such as radar. and git. send the auth cookies. The WebSocket check shares the helper and no longer accepts localhost in production. - Take the admin flag from the user row the token check already loads, so a demotion applies on the next request, not on token expiry. - Cut the refresh token lifetime from 365 to 90 days; refreshes renew it. - Docs: fix the stale revocation notes in backend/README.md and the Cloudflare Access comment in routes.caddy. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Backend
The Go API for adam-french.co.uk. It is one process behind Caddy, serving a GraphQL endpoint, a handful of REST endpoints, and the chat WebSocket, backed by PostgreSQL and a few third-party APIs (Spotify, Steam, Gitea). Anything that needs Claude is asked of the Python service, not called from here.
Caddy strips the /api prefix before proxying, so a browser request to
/api/graphql arrives here as /graphql. Every path below is as this service
sees it.
Layout
main.go wiring: env -> services -> Store -> routes
models/models.go GORM entities, which are ALSO the GraphQL object types
handlers/ REST endpoints + the shared Store dependency container
store.go Store: DB, service handles, and the third-party caches
handle_*.go one file per domain
services/ stateful collaborators (DB, auth, chat hub, API clients)
graph/ GraphQL layer
schema/*.graphql source of truth for the schema, including who may call what
*.resolvers.go resolver implementations, one per schema file
*_helpers.go hand-written mapping helpers (never regenerated)
directives.go @admin / @signedIn: the authorisation checks, written once
directives_test.go fails when a mutation is added without one
context.go reading identity out of the resolver context
middleware.go putting it in
doc.go package overview — read this before touching resolvers
generated.go GENERATED, do not edit
model/models_gen.go GENERATED, do not edit
Request lifecycle
- Caddy terminates TLS, applies its own rate limits, strips
/api, and proxies to this service. - Gin routes the request.
SetTrustedProxiespins the Docker network CIDR so thatClientIP()— which the login rate limiter keys on — cannot be spoofed viaX-Forwarded-For. - REST: the route's middleware chain runs.
AuthMiddlewarerequires a valid access token and stashes the claims in the Gin context;AdminMiddlewarethen requiresadmin: truein those claims. A handler reached through theadmingroup can assume both. - GraphQL: there is no guarding middleware.
AuthContextMiddlewareonly annotates the request context with the Gin context and, if a valid access token is present, the JWT claims — and then lets everything through. A field is guarded by a directive on it in the schema,@adminor@signedIn, which runs before the resolver. A field with neither is public. See GraphQL authorisation. - Handlers and resolvers both reach the database and the caches through the
single
handlers.Storebuilt inmain.
Auth
POST /auth/login(or theloginmutation) checks a bcrypt hash and sets two cookies, bothHttpOnly+Secure+SameSite=Lax:access_token(7 days) carriesid,username,adminandtv;refresh_token(90 days) carriesidandtv. Every check loads the user row (one SELECT) to comparetv, and takesadminfrom the row, not the token.
POST /auth/refresh(or therefreshTokenmutation) exchanges the refresh token for a new pair, so the 90 days slide while the account is in use.GET /auth/validate-adminanswers with a bare status code (200 / 403 / 401) and will transparently refresh, so returning admins are recognised without a visible re-login.GET /auth/validate/:permissionis the same for one named permission (200 / 403 / 401, and 404 for a key that does not exist). It backs Caddy's permission gate, which lets a non-admin account into one service — the receipt printer at/receipt/— without making it an admin. Grantable keys are the registryservices.PermissionTypes(services/permissions.go); a user's grants areUser.Permissions, set from the admin panel's Manage Users tab (setUserPermissions). Admins pass every permission check. The check reads the user row, not the token, so a grant or revoke applies on the next request. On 200 it returns the username inX-Auth-User, which Caddy forwards to the gated service asX-Remote-User.- Revocation is by
User.TokenVersion: logout and a password change bump it, which invalidates every token already issued to that user. Demoting an admin applies on their next request. - Any request that is not GET/HEAD/OPTIONS must come from the site's own
origin (
services.RequireSameOrigin). SameSite=Lax alone would let a page on a sibling subdomain such as radar. or git. post with the cookies. Requests withoutOriginorSec-Fetch-Site(curl, server-to-server) pass.
The two implementations (REST in handlers/handle_auth.go, GraphQL in
graph/auth.resolvers.go) are parallel by design, since the front end uses
both. They share services.Auth and the rate limiter, but each has its own
cookie-writing helper — change one and you must change the other.
GraphQL authorisation
Who may call a field is written in the schema, on the field:
extend type Query {
bookmarks: [Bookmark!]! # public
places: [Place!]! @signedIn # any signed-in user
users: [User!]! @admin # admins only
}
| directive | passes when | error otherwise |
|---|---|---|
@admin |
the access token's admin claim is true |
admin access required |
@signedIn |
there is a valid access token at all | authentication required |
| (none) | always | — |
Both are declared in graph/schema/schema.graphql, implemented once in
graph/directives.go, and handed to gqlgen in main.go
(graph.Config{Directives: graph.Directives()}). gqlgen calls the directive
before the resolver; on an error the resolver never runs and the client gets
that message for the field. The two strings are shown as-is by the front end,
so they are part of the API. If the Directives line were ever dropped from
main.go the guarded fields would all fail (directive admin is not implemented), not open.
This replaced an if !IsAdminFromCtx(ctx) { return ... } that was pasted into
the top of every guarded resolver, where leaving it out was invisible. Now a
guarded resolver has no check in it, and the absence of a guard is something a
test can see.
What is guarded. Queries are public and mutations are @admin, except:
login,logout,refreshToken— public; they are how a session starts and ends.createPlace,updatePlace,deletePlaceand theplacesquery —@signedIn. Places are a shared list with no per-user ownership.users,user— queries, but@admin: private data.
What a directive cannot say stays in the resolver, using IsAdminFromCtx /
UserIDFromCtx from graph/context.go:
updatePost/deletePostare@adminand require the caller to be the post's author;setUserAdminrefuses to change the caller's own flag.mehas no directive: it returns its ownunauthorizederror.messageshas no directive: it filters private messages out for non-admins instead of refusing.spotifyNeedsReauthhas no directive: it isfalsefor non-admins.
The safety net is graph/directives_test.go, which walks the parsed
schema:
- every
Mutationfield must carry@adminor@signedIn, unless it is in thepublicMutationslist in that file (the three above). A new mutation with no directive fails the build's tests until one or the other is done; - the
@signedInmutations and the guarded queries are pinned by name, so loosening@adminto@signedIn, or dropping a guard from a private query, has to be done in the test as well; - the directive functions are unit-tested, and a handful of operations are run through the real executable schema to pin the messages on the wire.
One caveat: the test reads the .graphql files, but enforcement is in
generated.go. Adding a directive to an existing field does nothing until
gqlgen is re-run.
Adding an entity
- Model — add the struct to
models/models.gowithID uint(gorm:"primarykey") and agorm.DeletedAt, and list it inAutoMigrateinservices/database.go. There are no migration files. - Schema — add
graph/schema/<name>.graphqlwith the type, its inputs, andextend type Query/extend type Mutation. Put@adminor@signedInon every mutation, and on any query that is not for the public. Use#comments, not"""descriptions. - Binding — map the type onto the model in
gqlgen.yml, withdeletedAt: resolver: false. Without the entry gqlgen generates a second, unrelated struct ingraph/model/. - Generate —
go run github.com/99designs/gqlgen generate(below). This createsgraph/<name>.resolvers.gowith a stub per query and mutation. - Resolvers — fill the stubs in. Do not write an auth check; the
directive has run. Do not write an
IDresolver;ID uintbinds by itself (see IDs). Put commentary inside the function bodies, since gqlgen rewrites what is above the signature. A delete should load the row first and return an error for a missing id, as the existing ones do. - Test —
go test ./....directives_test.gowill tell you if a mutation was left unguarded; if you added a guarded query, add it to the pinned list there.
GraphQL vs REST
GraphQL is the default and covers essentially all the site's data. REST exists for the things GraphQL cannot do:
| REST endpoint | why it is not GraphQL |
|---|---|
/auth/* |
cookie handling and the status-code-only admin probe |
/spotify/callback |
OAuth providers redirect a browser here |
/uploads, /radio/upload, /rowing (POST) |
multipart file uploads |
/uploads (GET, admin) |
lists the stored files, next to the endpoint that stores them |
/notes/*path |
streams a file download, not JSON |
/ws |
WebSocket upgrade |
/rowing (GET), /spotify/listening, /spotify/recent |
predate the equivalent queries; kept working |
Calling the Python service
Every Claude call the site makes lives in the Python service; this backend has
no Anthropic client and does not read CLAUDE_API_KEY. (The job tracker at
/jobs/ is a separate app with its own Claude calls.) It reaches the
internal routes directly over the Docker network
(http://$PYTHON_HOST:$PYTHON_PORT, default python:8000), never through
Caddy, which answers 404 for /py/internal/:
| route | caller | sends | gets back |
|---|---|---|---|
/internal/rowing/read |
POST /rowing (handlers/handle_rowing.go) |
{media_type, data} (image, base64) |
{timeMinutes, timeSeconds, distance} |
Every route goes through one function, services.CallPython[T] in |
|||
services/python_call.go: marshal the body, POST it, decode a 200 into T. |
|||
| It has the one HTTP client (two-minute timeout, above the Python side's own | |||
| worst case of a retried Claude call) and the one rule for failures: |
- a 502 whose
detailis a string is a message the route wrote for a person ("failed to parse image data"). It comes back as a*services.PythonError, and the HTTP handler relays it to the browser; - anything else — connection refused, a 422 (its
detailis a list), a 503 for an unsetCLAUDE_API_KEY, a reply that will not decode — is an ordinary error. Callers log it and show something generic.
It lives in services so that a background job there could use it as well
as a handler. A new internal route needs only a
request struct, a reply struct and a one-line call.
Services
Everything is constructed in main from environment variables and handed to
handlers.Store. Nothing here fails start-up: a missing Spotify token or an
absent Steam key all just leave that feature
dormant, which is why callers nil-check Store.SpotifyClient and friends.
- database.go — Postgres via GORM. There are no migration files:
AutoMigrateis the whole schema story, so it adds tables/columns but never drops or renames, and a new model must be listed there to get a table. - auth.go — HS256 JWT minting and verification.
- ratelimit.go — in-memory per-IP sliding window (5 logins/minute). Process-local and never evicts keys.
- websocket.go — the chat hub. A package-level singleton: one connection set, one mutex, one DB handle. Public, with admin connections additionally able to see/send private messages and delete.
- spotify.go, steam.go, gitea.go — third-party reads. Results are
cached on
Storewith TTLs of 1 min (Spotify recent, Gitea) and 5 min (Steam). - python_call.go —
CallPython, the one way to reach the Python service's internal routes. See Calling the Python service. - uploads.go — every stored file.
Uploads.Save(ctx, fileHeader, private)validates againstUploadTypes(extension, stored Content-Type, content sniff) andMaxUploadSize, names the file randomly, stores it and returns its/uploads/...URL; an*UploadErroris the uploader's fault and safe to show them. Public files go to the Garage bucket wrapped inFallbackFileStore(a failed S3 write is logged and retried on disk), or to disk alone whenS3_ENDPOINTis unset; private ones always to disk (the edge cannot gate a bucket). Caddy serves/uploads/from the bucket first and disk second, so either store's files work at the same URL. A new handler that takes a file callsstore.Uploads.Save.Uploads.List(behind admin-onlyGET /uploads, for the canvas's upload picker) returns the union of bucket and disk, allow-listed types only, newest first; it is admin-only because listing would give away every unguessable URL. - notes.go — path-traversal-safe file lookup for the notes directory.
- seed.go — dev-only test data. Creates an admin with the password
password, so it must never run in production (SEED_DB=trueis set only indocker-compose.dev.yml).
Regenerating gqlgen
The .graphql files are the source of truth.
cd backend && go run github.com/99designs/gqlgen generate
If that stops with missing go.sum entry ... urfave/cli, the generator's own
dependencies are not in go.sum. go get github.com/99designs/gqlgen@<the version in go.mod> adds them.
This rewrites graph/generated.go and graph/model/models_gen.go and adds
stubs for new fields to the matching *.resolvers.go. Existing resolver bodies
are preserved, but hand-written code in a regenerated file can be relocated to
the end of it — which is why shared helpers live in *_helpers.go instead.
gqlgen.yml maps most GraphQL object types directly onto the GORM structs in
models/, so graph/model/ holds only input and payload types. Adding a type
to the schema without a corresponding entry in gqlgen.yml gets you a fresh
generated struct rather than the model you expected.
One scalar-level binding in gqlgen.yml is worth knowing about: ID lists
both graphql.IntID and graphql.UintID (see IDs under Gotchas).
If you delete a field or stop needing a resolver, delete the resolver function by hand before regenerating; otherwise gqlgen keeps it, commented out, in a warning block at the end of the file.
Gotchas
- Soft delete everywhere. Every model has a
gorm.DeletedAt, soDeletesets a timestamp and later queries filter the row out. Rows are never actually removed, and auniqueIndexstill collides with deleted rows — a soft-deleted username cannot be reused. Preloadis not lazy.Post.Authoris nil unless the query preloads it; there is no error to tell you why the field came back null.- IDs. Models use
uint; the schema usesID.gqlgen.ymlbindsIDtographql.IntIDandgraphql.UintID, so a model'sID uintfield is served directly — there are noIDresolvers, and a new type does not need one.IntIDis listed first, which is why resolver arguments are stillid int. Both write an id as a quoted decimal string ("12") and accept a string or a number, so the wire format is the same either way. Other unsigned fields are not IDs and still have resolvers:Message.authorId,Rowing.time,Rowing.distance. - Deletes error on a missing id. Every delete mutation loads the row
first, so a wrong id is a
record not founderror. Message.AuthorIDis not a user id. It is a per-process counter handed out by the chat hub on connect, meaningless across restarts.- The chat table is capped. Every insert soft-deletes all but the newest 50 messages.
- Store's caches are unsynchronised and shared by all requests. The races are benign (at worst a duplicate upstream fetch), but do not add a field there that would be unsafe to tear.
- Claude's output is never parsed here. Prompts, the markdown-fence stripping and the JSON parsing are all in the Python service; this side receives typed JSON or an error (see Calling the Python service).
- Introspection and the playground are off unless
DEV_MODE=trueandGQL_INTROSPECTION/GQL_PLAYGROUNDare set. - No graceful shutdown.
r.Runblocks forever and its error is ignored; the container is simply killed.
Environment
| var | purpose |
|---|---|
POSTGRES_USER / _PASSWORD / _DB / _HOST / _PORT |
database connection |
BACKEND_PORT |
listen port |
BACKEND_SECRET |
JWT signing secret |
BACKEND_ENDPOINT |
this service's public base URL |
DOMAIN |
auth cookie domain and WebSocket origin allow-list |
DEV_MODE |
release vs debug Gin; gates the two GraphQL dev flags |
SEED_DB |
seed dev test data (never in production) |
GQL_INTROSPECTION / GQL_PLAYGROUND |
dev-only GraphQL extras |
SPOTIFY_CLIENT_ID / _SECRET / _REDIRECT_URI / SPOTIFY_AUTH_STATE |
Spotify OAuth |
PYTHON_HOST / PYTHON_PORT |
Python service, for the rowing photo reader (default python:8000) |
GITEA_HOST / GITEA_PORT |
Gitea activity feed |
STEAM_API_KEY / STEAM_ID |
Steam presence |
Paths baked into the code, all inside mounted volumes so they survive a
container restart: /backend/logs, /backend/notes, /backend/uploads,
/backend/fallback_music, /backend/token/ (the Spotify OAuth token).
CLAUDE_API_KEY is not in the table: the backend does not read it. The
python and jobs containers, the two that use it, get it through their
explicit environment: lists in docker-compose.yml. The email and job-lead variables still in .env belong to the job
tracker (jobs), which has its own repo and README.