thermograph/backend/daemon/internal/apiclient/client.go
emi 2d3f37c474
All checks were successful
Sync infra to hosts / sync-beta (push) Successful in 8s
Sync infra to hosts / sync-prod (push) Successful in 7s
secrets-guard / encrypted (push) Successful in 8s
shell-lint / shellcheck (push) Successful in 10s
Build + push backend image (Forgejo registry) / build-push (push) Successful in 1m14s
Deploy backend to beta VPS / deploy (push) Successful in 2m4s
daemon: move the Discord gateway and scheduler out of the web process into Go (#21)
The gateway bot and APScheduler were long-lived stateful I/O loops running
inside the async web app under a leader election. They move into a single Go
binary that owns ONLY that I/O -- websocket, RESUME, heartbeat, backoff, timers.

It owns no grading logic. Anything needing data calls back over a new
internal-only surface (/internal/discord/grade, /internal/jobs/*). Grading
depends on polars and the parquet cache; reimplementing it in Go would let the
bot's grades drift from the API's. The grade route returns gateway-ready JSON
and Go relays the bytes verbatim.

The binary ships in the backend image and runs as a second compose service off
the same tag, so the two ends of the /internal/* contract can never skew.
deploy.sh rolls daemon alongside backend -- without that the service would never
be created, since a single-service deploy uses --no-deps. It also probes the
image first and skips the daemon when rolling a tag that predates the binary:
infra tracks main while image tags are env-staged, so a host can legitimately be
asked to roll an older backend image, and creating the service anyway would
leave a container crash-looping on a missing binary.

replicas: 1 with order: stop-first replaces the leader election -- Discord
permits one gateway connection per bot token.

THERMOGRAPH_INTERNAL_TOKEN is optional: both ends derive it from
THERMOGRAPH_AUTH_SECRET via HMAC under a domain-separation label, so this needs
no new vault entry. The derivation is pinned to a shared cross-language test
vector asserted on both sides, so drift fails CI instead of 401ing every call.
Fail closed when neither secret is set.

Improvements over the Python: a close intended for RESUME uses 4000 rather than
1000 (Discord invalidates a session closed 1000, so the old default defeated its
own resume); MESSAGE_CREATE runs on a bounded worker pool; and a malformed HELLO
returns an error rather than a clean reconnect, which would otherwise reset
backoff and hot-loop against the gateway.

365 Python tests pass; Go build/vet/test -race clean; shellcheck 0 findings.
2026-07-23 22:49:54 +00:00

107 lines
3.9 KiB
Go

// Package apiclient is the daemon's ONLY channel back into the Python app.
// The daemon owns long-lived I/O (gateway socket, timers) and nothing else:
// grading depends on polars + the parquet cache, and the slash-command path
// deliberately shares one grade builder with the API so the two never drift.
// Reimplementing any of that here would reintroduce exactly that drift, so
// everything data-shaped is a POST to the backend's /internal/* routes.
package apiclient
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
// defaultTimeout bounds every callback. Grading does synchronous parquet/DB
// reads on the Python side (a cold city can take a while), so be generous —
// but never infinite: a hung backend must not wedge the gateway's event loop.
const defaultTimeout = 60 * time.Second
// errBodyLimit caps how much of an error response we echo into logs; enough
// to see a traceback's headline without dumping megabytes.
const errBodyLimit = 512
// Client calls the backend's internal-only routes. The token is defence in
// depth — Caddy never routes /internal/* to the backend in the first place —
// but it is still required on every request so a misrouted or LAN-local call
// cannot hit these endpoints unauthenticated.
type Client struct {
base string
token string
http *http.Client
}
// New returns a Client for the backend at base (e.g. http://web:8137),
// authenticating with token.
func New(base, token string) *Client {
return &Client{
base: strings.TrimRight(base, "/"),
token: token,
http: &http.Client{Timeout: defaultTimeout},
}
}
// post sends body to path and returns the raw response body on 2xx.
func (c *Client) post(ctx context.Context, path string, body any) (json.RawMessage, error) {
payload, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("apiclient: encode %s body: %w", path, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.base+path, bytes.NewReader(payload))
if err != nil {
return nil, fmt.Errorf("apiclient: build %s request: %w", path, err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Thermograph-Internal-Token", c.token)
resp, err := c.http.Do(req)
if err != nil {
return nil, fmt.Errorf("apiclient: POST %s: %w", path, err)
}
defer resp.Body.Close()
// Read one byte past the limit so the truncation marker only appears when
// something was actually cut.
raw, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
if err != nil {
return nil, fmt.Errorf("apiclient: POST %s: read body: %w", path, err)
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
snippet := raw
truncated := ""
if len(snippet) > errBodyLimit {
snippet = snippet[:errBodyLimit]
truncated = "... (truncated)"
}
return nil, fmt.Errorf("apiclient: POST %s: status %d: %s%s", path, resp.StatusCode, snippet, truncated)
}
return json.RawMessage(raw), nil
}
// Grade asks Python to grade a city query and returns the gateway-ready
// Discord message JSON VERBATIM. The raw bytes are relayed to Discord without
// parsing or reshaping — that verbatim relay is what keeps Go out of the
// grading/embed contract: any change to the message shape lives entirely on
// the Python side.
func (c *Client) Grade(ctx context.Context, query string) (json.RawMessage, error) {
return c.post(ctx, "/internal/discord/grade", map[string]string{"query": query})
}
// WarmCities triggers the warm-cities job (the Python side owns what "warm"
// means and which cities are curated).
func (c *Client) WarmCities(ctx context.Context) error {
_, err := c.post(ctx, "/internal/jobs/warm-cities", map[string]string{})
return err
}
// IndexNow triggers the IndexNow check-and-submit job; a no-op on the Python
// side when the indexable URL set has not changed.
func (c *Client) IndexNow(ctx context.Context) error {
_, err := c.post(ctx, "/internal/jobs/indexnow", map[string]string{})
return err
}