server

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 29 Imported by: 0

Documentation

Overview

Package server wires the HTTP listener, middleware, and graceful shutdown.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccessLog

func AccessLog(logger *slog.Logger) func(http.Handler) http.Handler

AccessLog emits one structured line per request. Trace and span IDs are added automatically by the logger's handler.

func CORS

func CORS(allowed []string, reflectAnyWhenEmpty bool) func(http.Handler) http.Handler

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

func MaxBytes(limit int64) func(http.Handler) http.Handler

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

func RateLimit(perSecond float64, burst int) func(http.Handler) http.Handler

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

func Recoverer(logger *slog.Logger) func(http.Handler) http.Handler

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.

func SecureHeaders

func SecureHeaders(next http.Handler) http.Handler

SecureHeaders sets conservative security response headers on every response.

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 NewHealth

func NewHealth(pool *pgxpool.Pool) *Health

func (*Health) Livez

func (h *Health) Livez(w http.ResponseWriter, _ *http.Request)

Livez reports process liveness independent of dependencies.

func (*Health) Readyz

func (h *Health) Readyz(w http.ResponseWriter, r *http.Request)

Readyz reports whether the service should receive traffic: it must be marked ready and the database must answer a ping.

func (*Health) SetReady

func (h *Health) SetReady(ready bool)

type RouterConfig

type RouterConfig struct {
	CORSAllowedOrigins []string
	RateLimitPerSecond float64
	RateLimitBurst     int
	MaxBodyBytes       int64
	Development        bool
}

type RouterDeps

type RouterDeps struct {
	Logger        *slog.Logger
	Telemetry     *observability.Telemetry
	Health        *Health
	Authenticator *auth.Authenticator
	WidgetHandler api.ServerInterface
	Config        RouterConfig
}

Jump to

Keyboard shortcuts

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