Documentation
¶
Overview ¶
Package server builds the HTTP stack: middleware, security headers, the embedded single-page app and graceful shutdown.
Index ¶
- Variables
- func AuditContext(next http.Handler) http.Handler
- func ClientIP(r *http.Request) *netip.Addr
- func RealIP(trusted []netip.Prefix) func(http.Handler) http.Handler
- func RedactPath(p string) string
- func RequestLogger(log *slog.Logger) func(http.Handler) http.Handler
- func SecurityHeaders(httpsOnly bool) func(http.Handler) http.Handler
- type HTTPMetrics
- type RateLimits
- type SPA
Constants ¶
This section is empty.
Variables ¶
var ErrNoFrontend = errors.New("frontend not built")
ErrNoFrontend is returned by helpers when the dist folder is missing.
Functions ¶
func AuditContext ¶
AuditContext stores the client IP, user agent and request id in the request context so audit entries can include them.
func RealIP ¶
RealIP sets r.RemoteAddr from X-Forwarded-For / X-Real-IP, but only when the direct peer is a trusted proxy. chi's RealIP trusts any client, which would let anyone spoof their address in the audit log and rate limits.
func RedactPath ¶
RedactPath replaces the secret part of a path with {token}, so logs never hold a working link.
func RequestLogger ¶
RequestLogger logs one line per request at info (errors at warn/error).
Types ¶
type HTTPMetrics ¶
type HTTPMetrics struct {
// contains filtered or unexported fields
}
HTTPMetrics records request counts and latency per route pattern (never per raw path, which would explode cardinality with ids).
func NewHTTPMetrics ¶
func NewHTTPMetrics(reg prometheus.Registerer) *HTTPMetrics
NewHTTPMetrics registers the HTTP collectors.
func (*HTTPMetrics) Middleware ¶
func (m *HTTPMetrics) Middleware(next http.Handler) http.Handler
Middleware observes every request after routing.
type RateLimits ¶
type RateLimits struct {
AuthPerMinute int // per IP on /api/v1/auth/*
APIPerMinute int // per API key, or per IP without one
}
RateLimits protects the API from abuse: sign-in endpoints are limited per client IP (on top of the per-email lockout), and every API request is limited per API key (or per IP for browser sessions). Limits answer 429 with Retry-After and an RFC 9457 body.
func (RateLimits) Middleware ¶
func (rl RateLimits) Middleware() func(http.Handler) http.Handler
type SPA ¶
type SPA struct {
// contains filtered or unexported fields
}
SPA serves the embedded single-page app. Hashed files under /assets/ are cached for a year; everything else falls back to index.html, which is never cached so new releases load immediately. Pre-compressed .br and .gz siblings produced by the build are served when the client accepts them.