{"modulePath":"github.com/RCooLeR/kit","version":"v0.0.0-20261128114219-336f9e3ebeeb","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package httpserver\n\nPackage httpserver holds what every service repeats around net/http: a server with all four timeouts set, a run loop that shuts down gracefully, and the liveness and readiness handlers.\n\nThe router is the caller's: any http.Handler works, and this package depends on no router. It also decides no path and no response body. The caller mounts the health handlers where its contract says (new services: /healthz and /readyz) and passes the exact answers.\n\n## Constants\n\n```go\nconst (\n\tDefaultReadHeaderTimeout = 5 * time.Second\n\tDefaultReadTimeout       = 10 * time.Second\n\tDefaultWriteTimeout      = 30 * time.Second\n\tDefaultIdleTimeout       = 60 * time.Second\n\t// DefaultShutdownTimeout is how long Run and Serve wait for requests in flight.\n\tDefaultShutdownTimeout = 10 * time.Second\n)\n```\nTimeouts used when the matching Options field is zero. They suit an API that answers quickly; a service that streams or serves large bodies raises WriteTimeout.\n\n## Functions\n\n```go\nfunc Live(alive Response) http.Handler\n```\nLive returns the liveness handler: it always sends alive, because a process that can answer at all is alive. It panics when alive carries no HTTP status; that is a mistake in the wiring and shows at the first start.\n\n```go\nfunc New(opts Options) *http.Server\n```\nNew returns a server with every timeout set.\n\n```go\nfunc Ready(check Check, ready, notReady Response, onFailure func(error)) http.Handler\n```\nReady returns the readiness handler: ready when check returns nil, notReady otherwise. The error goes to onFailure (nil: dropped) and never into the response, so a probe from outside learns nothing about the inside. It panics on a nil check or a response without an HTTP status, as Live does.\n\n```go\nfunc Run(ctx context.Context, server *http.Server, shutdownTimeout time.Duration) error\n```\nRun listens on server.Addr and serves until ctx ends, then shuts down as Serve does. An address that cannot be bound is reported at once, before anything is served.\n\n```go\nfunc Serve(ctx context.Context, server *http.Server, listener net.Listener, shutdownTimeout time.Duration) error\n```\nServe serves on listener until ctx ends, then stops accepting connections and waits up to shutdownTimeout (zero or less: DefaultShutdownTimeout) for requests in flight. It returns nil after a clean shutdown, and an error when serving failed or the wait ran out; in the latter case the remaining connections are closed. Serve closes the listener.\n\n## Types\n\n```go\ntype Check func(ctx context.Context) error\n```\nCheck reports whether the service can do its work right now, for example by pinging its database. It must respect ctx, which ends when the prober gives up.\n\n```go\ntype Options struct {\n\t// Addr is the listen address, such as \":8080\".\n\tAddr string\n\t// Handler is the service's router.\n\tHandler http.Handler\n\t// A zero timeout means the default above; none can be switched off, because a server without\n\t// them is open to slow-client attacks.\n\tReadHeaderTimeout time.Duration\n\tReadTimeout       time.Duration\n\tWriteTimeout      time.Duration\n\tIdleTimeout       time.Duration\n}\n```\nOptions describe one HTTP server.\n\n```go\ntype Response struct {\n\t// Status is the HTTP status code.\n\tStatus int\n\t// ContentType is sent when Body is not empty.\n\tContentType string\n\t// Body is sent as is; empty for a 204.\n\tBody string\n}\n```\nResponse is one fixed answer of a health endpoint. There is no default: every existing service answers differently (204, \"ok\", a JSON object) and probes and dashboards depend on it, so the caller writes the answer out.\n\n","path":"github.com/RCooLeR/kit/httpserver","name":"httpserver","synopsis":"Package httpserver holds what every service repeats around net/http: a server with all four timeouts set, a run loop that shuts down gracefully, and the liveness and readiness handlers.","isRedistributable":true}
