httpx

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package httpx provides shared HTTP server lifecycle helpers so that cocoonstack binaries don't each reinvent graceful-shutdown plumbing.

Index

Constants

View Source
const DefaultReadHeaderTimeout = 10 * time.Second

DefaultReadHeaderTimeout is the conservative default for http.Server.ReadHeaderTimeout. 10s is what every cocoonstack consumer was already using and mitigates Slowloris attacks (gosec G112) by capping how long a client may take to send headers.

Variables

This section is empty.

Functions

func NewServer

func NewServer(addr string, handler http.Handler) *http.Server

NewServer returns an *http.Server with Addr, Handler, and the safe ReadHeaderTimeout default set. Use this in preference to composing an http.Server literal so every server in the stack carries the same Slowloris-mitigation timeout.

func Run

func Run(ctx context.Context, shutdownTimeout time.Duration, specs ...ServerSpec) error

Run starts every spec in its own goroutine and blocks until ctx is canceled or any spec's Start returns a non-ErrServerClosed error. On exit it calls Shutdown on each server using a fresh timeout context that is *not* derived from the canceled parent (shutdown must outlive the signal-derived ctx). Errors from both serve and shutdown are aggregated with errors.Join. http.ErrServerClosed is treated as a clean shutdown and never returned.

Types

type ServerSpec

type ServerSpec struct {
	Server *http.Server
	Start  StartFunc
}

ServerSpec pairs an http.Server with the StartFunc that boots it. Start is called in a goroutine; Server is what Run calls Shutdown on when the parent context is canceled.

func HTTPSServerSpec

func HTTPSServerSpec(srv *http.Server, cert, key string) ServerSpec

HTTPSServerSpec wraps srv with srv.ListenAndServeTLS(cert, key) as its StartFunc. If cert and key are empty, the server is expected to have its TLSConfig.Certificates populated and srv.ListenAndServeTLS("", "") will still work.

func HTTPServerSpec

func HTTPServerSpec(srv *http.Server) ServerSpec

HTTPServerSpec wraps srv with srv.ListenAndServe as its StartFunc.

type StartFunc

type StartFunc func() error

StartFunc is the listen-and-serve entry point for a single server, invoked in its own goroutine by Run. Typical implementations are (*http.Server).ListenAndServe or (*http.Server).ListenAndServeTLS.

Jump to

Keyboard shortcuts

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