Documentation
¶
Overview ¶
Package httpserver provides the small bits of main.go boilerplate that every Go service in the platform re-implements: a /health endpoint, a Prometheus /metrics endpoint, graceful shutdown, and a SIGINT/SIGTERM-aware context.
Index ¶
Constants ¶
const ( DefaultReadHeaderTimeout = 5 * time.Second DefaultReadTimeout = 5 * time.Second DefaultWriteTimeout = 10 * time.Second DefaultIdleTimeout = 0 // 0 = use ReadTimeout (net/http default behaviour) )
Default server timeouts applied by NewHealthServer/NewMetricsServer when no overriding option is supplied.
const DefaultShutdownTimeout = 5 * time.Second
DefaultShutdownTimeout bounds graceful shutdown when Shutdown is called without an already-deadlined context.
Variables ¶
This section is empty.
Functions ¶
func HealthHandler ¶
HealthHandler returns an http.Handler that responds to /health (and any other path) with 200 OK and the body "ok".
func MetricsHandler ¶
MetricsHandler returns an http.Handler serving the default Prometheus registry at /metrics.
func SignalContext ¶
func SignalContext() (context.Context, context.CancelFunc)
SignalContext returns a context that is cancelled when the process receives SIGINT or SIGTERM. The returned cancel function should be deferred to release resources and stop intercepting signals.
Types ¶
type Option ¶ added in v0.2.0
Option customises the underlying *http.Server before it starts. Options are applied in order, so a later option wins over an earlier one.
func WithIdleTimeout ¶ added in v0.2.0
WithIdleTimeout overrides the server's IdleTimeout. When zero, net/http falls back to ReadTimeout.
func WithReadHeaderTimeout ¶ added in v0.2.0
WithReadHeaderTimeout overrides the server's ReadHeaderTimeout.
func WithReadTimeout ¶ added in v0.2.0
WithReadTimeout overrides the server's ReadTimeout. A zero or negative value disables the timeout (net/http semantics).
func WithWriteTimeout ¶ added in v0.2.0
WithWriteTimeout overrides the server's WriteTimeout. A zero or negative value disables the timeout (net/http semantics).
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server wraps an *http.Server with conservative timeouts and a graceful Shutdown helper.
func NewHealthServer ¶
NewHealthServer builds a Server serving /health on addr (e.g. ":8080"). Optional Options override the default read/write/idle timeouts; with no options the conservative defaults are used.
func NewMetricsServer ¶
NewMetricsServer builds a Server serving Prometheus /metrics on addr (e.g. ":9094"). Optional Options override the default read/write/idle timeouts.