# thermograph-daemon

Single Go binary that owns the backend's long-lived stateful I/O: the Discord
gateway connection (IDENTIFY/RESUME/heartbeat/reconnect backoff) and the
recurring-job timers (warm-cities, IndexNow). It replaces the two in-process
background jobs `web/app.py` used to run under leader election — one daemon
replica makes the single-connection invariant a deployment fact instead of a
runtime election.

## The Go/Python split

Go here owns **no** climate or grading logic. Grading depends on polars and the
parquet cache, and the slash-command path deliberately shares one grade builder
with the API so bot grades and API grades never drift. So anything data-shaped
is a POST back into the Python app's internal-only routes:

| Route | Purpose |
| --- | --- |
| `POST /internal/discord/grade` | `{"query": "phoenix"}` → gateway-ready Discord message JSON, relayed to Discord verbatim |
| `POST /internal/jobs/warm-cities` | trigger the warm-cities job |
| `POST /internal/jobs/indexnow` | trigger the IndexNow check-and-submit |

Every request carries `X-Thermograph-Internal-Token`. Caddy never routes
`/internal/*` to the backend, so the token is defence in depth, not the only
control. If `THERMOGRAPH_INTERNAL_TOKEN` is unset, the Python side disables the
routes and this daemon refuses to start (fail closed on both ends).

## Configuration (env)

| Var | Required | Meaning |
| --- | --- | --- |
| `THERMOGRAPH_INTERNAL_TOKEN` | yes | shared secret for the internal routes |
| `THERMOGRAPH_API_BASE_INTERNAL` | yes | backend base URL, e.g. `http://web:8137` |
| `THERMOGRAPH_DISCORD_BOT` | no | `1`/`true`/`yes`/`on` enables the gateway |
| `THERMOGRAPH_DISCORD_BOT_TOKEN` | no | bot token (gateway stays off without it) |
| `THERMOGRAPH_WARM_CITIES_INTERVAL_HOURS` | no | default 24; malformed → default |
| `THERMOGRAPH_INDEXNOW_INTERVAL_HOURS` | no | default 6; malformed → default |

## Run locally

```sh
cd backend/daemon
go build ./... && go vet ./... && go test ./...

THERMOGRAPH_INTERNAL_TOKEN=dev-token \
THERMOGRAPH_API_BASE_INTERNAL=http://localhost:8137 \
go run .
```

Deployed as `/usr/local/bin/thermograph-daemon` in the backend image.
