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 LogContext(next http.Handler) http.Handler
- 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
- type SessionLimiter
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 LogContext ¶
LogContext starts the request's log fields (review 2026-11 OP-7): its id and its route pattern; the API adds the workspace and the caller. Every line logged with the request's context carries 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 a key or a session
// IPPerMinute caps the requests of signed-in browsers from one address
// together; each session is limited to APIPerMinute after sign-in is
// checked (SessionLimiter). Zero means ten times APIPerMinute.
IPPerMinute int
// SessionCookie is the session cookie's name.
SessionCookie string
}
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.
type SessionLimiter ¶
type SessionLimiter struct {
// contains filtered or unexported fields
}
SessionLimiter limits the API requests of each signed-in browser session (review 2026-11 OP-6). It counts in one-minute windows and weighs the previous window by how much of it still overlaps the last minute, like the per-key limiter. Each web replica counts on its own, so behind a load balancer a session gets up to the limit on every replica.
func NewSessionLimiter ¶
func NewSessionLimiter(perMinute int) *SessionLimiter
NewSessionLimiter allows perMinute requests per session.