Documentation
¶
Overview ¶
Package httpserver wraps http.Server with the request middleware of work (one request.handled per request, a span, panic recovery), GET /healthz, optional CORS and optional pprof. /healthz and /debug/pprof/ sit outside the middleware, so health checks and profiling write no lines.
srv := httpserver.New(log, routes, httpserver.Addr("127.0.0.1:8080"), httpserver.CORS("https://app.example"))
lifecycle.OnStop(ctx, "http", func() error { return srv.Shutdown(context.Background()) })
return srv.ListenAndServe(ctx)
Under lifecycle.Run the stop step "http" is the server's drain: Shutdown waits for the running requests, so the steps registered before it (store, watcher) run after the drain, and ListenAndServe returns once the step has run.
Index ¶
Constants ¶
const ShutdownGrace = 10 * time.Second
ShutdownGrace bounds the graceful stop ListenAndServe starts when its context is cancelled; connections still open after it are closed.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*Server)
Option changes how New builds the server.
type Server ¶
Server is an http.Server with the standard's request handling around the app's handler.
func New ¶
New builds the server: handler behind work.HTTP(log) and CORS, GET /healthz answering "ok", ReadHeaderTimeout 10 s and IdleTimeout 2 min. Every other field of the embedded http.Server may be set before ListenAndServe.
func (*Server) ListenAndServe ¶
ListenAndServe serves until Shutdown is called or ctx is cancelled, and returns nil in both cases. Request contexts carry ctx's values (logger, lifecycle) but not its cancellation, so a stop lets running requests finish. On ctx cancellation it calls Shutdown with ShutdownGrace and returns when that stop has finished: running requests are done or the grace has passed and their connections are closed.