Engineering note · AuraRisk
Hardening the AuraRisk API
- Project
- AuraRisk — Personal project
- Issue
- #20 Harden HTTP API & structured error handling
- My role
- Author of PR #43
- Stack
- Go · Gin · Prometheus · TypeScript
- Status
- Merged 8 Oct 2026
The problem
Before starting, I checked issue #20 against main and wrote down what was already there. Graceful shutdown existed in a basic form, and the CORS allowlist had been done earlier. The rest had gaps:
- Errors were
{"error": "..."}, but some handlers still returned rawerr.Error().GET /api/reportscould send a database error straight to the client. - 404, 405 and panics came back as plain text or an empty body.
- There were no request IDs and no rate limiting.
/healthalways saidok, whatever the state of the database.- Gin trusted every proxy by default, so any client could set
X-Forwarded-Forand pick its own IP.
What I did
- One error shape. Every error is now
{"error", "code", "request_id"}. Thecodeis derived from the status, such asnot_found. I kepterroras a plain string so the existing apps kept working. - No leaked internals. A 500 returns only
internal server error. The details go to the log with the request ID. An oversized body gets a 413 instead of a decoder message. - Request IDs. The backend reuses an
X-Request-IDfrom the caller only if it is short and log-safe. Otherwise it generates one. The ID appears in the response header, the access log and the error body. - Rate limits. A token bucket per client IP allows 120 requests a minute across
/api. Report creation, device registration and photo-upload start get a tighter 20 a minute on top. Over the limit, clients get 429 withRetry-After. - Trusted proxies. A new
TRUSTED_PROXIESsetting lists which proxies may set the client IP. Empty trusts none. - Liveness and readiness.
/health/livechecks only the process./health/readyreturns 503 when the database is unreachable or the instance is draining. - Graceful shutdown. On SIGTERM the instance fails readiness, waits a configurable drain delay, finishes in-flight requests, then waits for background workers before the database closes. I also added server read, write and idle timeouts.
One readiness choice took some thought. If the weather provider's circuit breaker is open, readiness reports degraded but stays 200:
{"status": "degraded", "checks": {"database": "ok", "weather": "circuit_open"}}
Cached weather can still be served, and every instance shares the same provider. Failing readiness would pull every instance out of the load balancer at once.
This PR also brought in #42, which had been merged into the #41 branch after #41 had already merged, so it never reached main. It fixed the alert-rule tests that #41 broke as well.
How I checked it
I ran go test -race with a throwaway Postgres 16, so the database integration tests ran too. A new hardening_test.go covers request-ID generation, reuse and rejection of unsafe IDs. It checks the error shape for 404, 405, panics and 500s with no leaked details, 413 versus 400, per-client limiting with refill and idle sweep, a disabled limiter, and readiness returning 503 without a database.
I then smoke-tested the real binary: readiness, JSON 404 and 405, a caller-supplied request ID in an error, a 429 with Retry-After, and SIGTERM ending in "Shutdown complete". Mobile tsc and jest and the web build passed.
What I learned
Writing down the gap against main before starting kept the PR honest about what was new. A health check needs to answer a specific question for a specific caller, and a restart signal and a traffic signal are different questions. I also noted the limits. Rate limits are per instance and in memory, and behind a proxy they only work once TRUSTED_PROXIES is set.