httpserver

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package httpserver wires the HTTP transport: the router, the core middleware chain, and the server lifecycle. It owns the New constructor that an application extends by adding fields to Deps and mounting its own routes through Deps.Routes; the routes registered here (/healthz, /readyz, optional /metrics) are transport-generic — an application's pages, static assets, and any other embedded content are wired in by the application itself, not by this package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(cfg Config, deps Deps) *http.Server

New builds the application's HTTP server from cfg and deps with the core middleware (request id, structured logging, panic recovery, per-request timeout) applied to every route. ReadHeaderTimeout stays a short, fixed guard against Slowloris-style header trickling regardless of upload size; ReadTimeout/WriteTimeout and the per-request context deadline all derive from cfg.Server.RequestTimeout, since a legitimate large photo upload over a slow connection needs a body-read (and therefore also a write-deadline) budget well beyond what a small fast request needs — see RequestTimeout's doc comment for why WriteTimeout must grow too, not just ReadTimeout.

func StaticFileServer

func StaticFileServer(fsys fs.FS) http.Handler

StaticFileServer serves fsys with a moderate cache lifetime and nosniff. Mount it under a prefix, e.g. mux.Handle("GET /static/", http.StripPrefix("/static/", httpserver.StaticFileServer(web.StaticFS())))

Types

type Config

type Config struct {
	Server config.ServerConfig
	HSTS   config.HSTSConfig
}

Config is the configuration the HTTP transport needs — the generic sub-configs New actually reads, not an application's root configuration (which stays app-side; see the config package doc comment). A generic sub-config New comes to need later is added as a new field here rather than changing New's signature.

type Deps

type Deps struct {
	// Logger receives the structured per-request log line. Required.
	Logger *slog.Logger
	// Ready backs the /readyz probe; nil means "always ready".
	Ready ReadinessFunc
	// Routes, if non-nil, registers an application's own routes (pages, HTMX
	// fragments, static assets, feature handlers) on the mux after the
	// platform routes. This is the extension point for application wiring
	// without changing New.
	Routes func(mux *http.ServeMux)
	// Middleware is an optional list of feature middleware inserted between
	// Timeout and CaptureRoutePattern in the canonical chain (e.g.
	// session/auth), so Timeout's deadline still bounds that middleware's
	// own work, not just the final handler. Middleware is applied in the
	// order given (first entry is outermost).
	Middleware []middleware.Middleware
	// HTTPMetrics, if non-nil, enables per-request Prometheus instrumentation
	// (request count, latency, in-flight gauge) via the Metrics middleware.
	// nil disables instrumentation (tests, first-run setup).
	HTTPMetrics *metrics.HTTPMetrics
	// MetricsHandler, if non-nil, is served at GET /metrics (the Prometheus
	// scrape endpoint, typically promhttp.HandlerFor over the registry that
	// HTTPMetrics is registered on). nil leaves the route unregistered.
	MetricsHandler http.Handler
}

Deps carries the dependencies the HTTP layer needs. An application appends fields here (session manager, repositories, handlers) rather than changing the New signature.

type ReadinessFunc

type ReadinessFunc func(ctx context.Context) error

ReadinessFunc reports whether the server's backing dependencies (e.g. the database) are reachable. It returns a non-nil error when the server is not ready to serve traffic.

Directories

Path Synopsis
Package middleware provides composable net/http middleware for the request backbone: request IDs, structured logging, panic recovery, and per-request timeouts.
Package middleware provides composable net/http middleware for the request backbone: request IDs, structured logging, panic recovery, and per-request timeouts.

Jump to

Keyboard shortcuts

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