server

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2026 License: AGPL-3.0 Imports: 27 Imported by: 0

Documentation

Overview

Package server builds the HTTP stack: middleware, security headers, the embedded single-page app and graceful shutdown.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoFrontend = errors.New("frontend not built")

ErrNoFrontend is returned by helpers when the dist folder is missing.

Functions

func AuditContext

func AuditContext(next http.Handler) http.Handler

AuditContext stores the client IP, user agent and request id in the request context so audit entries can include them.

func ClientIP

func ClientIP(r *http.Request) *netip.Addr

ClientIP returns the (already proxy-resolved) client address of r.

func LogContext

func LogContext(next http.Handler) http.Handler

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

func RealIP(trusted []netip.Prefix) func(http.Handler) http.Handler

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

func RedactPath(p string) string

RedactPath replaces the secret part of a path with {token}, so logs never hold a working link.

func RequestLogger

func RequestLogger(log *slog.Logger) func(http.Handler) http.Handler

RequestLogger logs one line per request at info (errors at warn/error).

func SecurityHeaders

func SecurityHeaders(httpsOnly bool) func(http.Handler) http.Handler

SecurityHeaders sets defensive headers on every response. Public pages and the embeddable form endpoints relax frame-ancestors themselves.

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.

func NewSPA

func NewSPA(fsys fs.FS) *SPA

NewSPA returns a handler for the built frontend in fsys (the dist folder). When the frontend hasn't been built, it serves a short explanation instead.

func (*SPA) ServeHTTP

func (s *SPA) ServeHTTP(w http.ResponseWriter, r *http.Request)

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.

func (*SessionLimiter) Allow

func (l *SessionLimiter) Allow(session string) (bool, time.Duration)

Allow counts one request of the session, or says how long to wait.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL