Documentation
¶
Overview ¶
Package server wires the HTTP listener, middleware, and graceful shutdown.
Index ¶
- func AccessLog(logger *slog.Logger) func(http.Handler) http.Handler
- func CORS(allowed []string, reflectAnyWhenEmpty bool) func(http.Handler) http.Handler
- func MaxBytes(limit int64) func(http.Handler) http.Handler
- func NewAdminServer(cfg config.AdminConfig) *http.Server
- func NewRouter(deps RouterDeps) (http.Handler, error)
- func RateLimit(perSecond float64, burst int) func(http.Handler) http.Handler
- func Recoverer(logger *slog.Logger) func(http.Handler) http.Handler
- func Run(ctx context.Context, cfg config.HTTPConfig, handler http.Handler, ...) error
- func SecureHeaders(next http.Handler) http.Handler
- type Health
- type RouterConfig
- type RouterDeps
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AccessLog ¶
AccessLog emits one structured line per request. Trace and span IDs are added automatically by the logger's handler.
func CORS ¶
CORS reflects allowed origins. With an empty allow-list it echoes any origin only when reflectAnyWhenEmpty is set (development); in production an empty list means no CORS headers are sent, so the policy fails closed.
func MaxBytes ¶
MaxBytes caps request body size. It rejects a declared-oversize body up front with 413, and caps the actual read with http.MaxBytesReader to also catch chunked or mis-declared bodies. Applied globally, it protects routes added by generated modules too.
func NewAdminServer ¶
func NewAdminServer(cfg config.AdminConfig) *http.Server
NewAdminServer builds the introspection listener exposing pprof and expvar. It is intentionally separate from the public server and, when a token is set, requires it as a bearer credential. Bind it to localhost in production.
func NewRouter ¶
func NewRouter(deps RouterDeps) (http.Handler, error)
NewRouter assembles the public HTTP handler: global middleware, unauthenticated operational endpoints, and the spec-validated, authenticated API routes.
func RateLimit ¶
RateLimit applies a per-client-IP token bucket. Idle limiters are evicted so the map does not grow without bound. Behind a trusted proxy, add an X-Forwarded-For parser with an explicit trusted-proxy allow-list.
func Recoverer ¶
Recoverer converts a panic into a 500 problem response and logs the stack, keeping a single bad request from taking down the process.
func Run ¶
func Run(ctx context.Context, cfg config.HTTPConfig, handler http.Handler, health *Health, logger *slog.Logger) error
Run starts the HTTP server and blocks until ctx is cancelled, then performs a readiness-first graceful drain: flip readiness so load balancers stop routing, pause, then stop accepting and wait for in-flight requests.
Types ¶
type Health ¶
type Health struct {
// contains filtered or unexported fields
}
Health answers Kubernetes-style probes. Readiness is flipped off at the start of graceful shutdown so a load balancer stops routing before in-flight requests are drained.
func (*Health) Livez ¶
func (h *Health) Livez(w http.ResponseWriter, _ *http.Request)
Livez reports process liveness independent of dependencies.
type RouterConfig ¶
type RouterDeps ¶
type RouterDeps struct {
Logger *slog.Logger
Telemetry *observability.Telemetry
Health *Health
Authenticator *auth.Authenticator
WidgetHandler api.ServerInterface
Config RouterConfig
}