server

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: AGPL-3.0 Imports: 22 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 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 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.

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)

Jump to

Keyboard shortcuts

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