Help us shape the future of Go! Take the 2026 Go Developer Survey

httpx

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package httpx holds the transport-neutral HTTP scaffolding shared by the products: CORS, gzip, the health handler, and ListenAndServe with the standard timeouts and graceful shutdown. Routing and payloads stay in the products.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcceptsGzip

func AcceptsGzip(header string) bool

AcceptsGzip reports whether an Accept-Encoding header value asks for gzip (directly or via "*"), honoring q=0 exclusions.

func CORS

func CORS(origins []string, next http.Handler) http.Handler

CORS sets Access-Control-Allow-Origin on responses whose Origin header matches an allowed origin (exact match, or "*" to allow any). Preflight requests are not answered here — mount Preflight on the paths that need it (products differ in method sets), or wrap with CORSWithPreflight.

func CORSWithPreflight

func CORSWithPreflight(origins []string, allowMethods string, next http.Handler) http.Handler

CORSWithPreflight combines CORS with a catch-all preflight: OPTIONS requests carrying Access-Control-Request-Method are answered by Preflight on every path; everything else flows through CORS to next.

func Gzip

func Gzip(next http.Handler) http.Handler

Gzip compresses responses for clients that accept gzip. OPTIONS (CORS preflight) is skipped: those responses have no body, and wrapping them would emit an empty gzip frame against a 204.

func Healthz

func Healthz(w http.ResponseWriter, _ *http.Request)

Healthz answers {"status":"ok"} — the liveness/readiness body both products serve at /healthz and /readyz.

func ListenAndServe

func ListenAndServe(ctx context.Context, h http.Handler, o ServeOptions) error

ListenAndServe runs h until ctx is cancelled, then shuts down gracefully with a 10s deadline. Timeouts: ReadHeader 10s, Read = RequestTimeout+10s, Idle 120s.

func OriginAllowed

func OriginAllowed(origins []string, origin string) bool

OriginAllowed reports whether origin matches the allowlist: exact match, or "*" to allow any.

func Preflight

func Preflight(origins []string, allowMethods string) http.HandlerFunc

Preflight answers CORS preflight (OPTIONS) requests: 204 with the allow headers for an allowed origin, a bare 204 otherwise. allowMethods is the literal Access-Control-Allow-Methods value (e.g. "POST, GET, OPTIONS").

Types

type ServeOptions

type ServeOptions struct {
	Addr string
	// RequestTimeout is the handler's overall timeout; ReadTimeout is set to
	// RequestTimeout+10s (60s when zero) so the body read cannot outlast it.
	RequestTimeout time.Duration
	// Log receives the "listening" line; nil uses slog.Default(). LogAttrs
	// are appended to it (products add e.g. graphiql=true).
	Log      *slog.Logger
	LogAttrs []any
}

ServeOptions configures ListenAndServe.

Jump to

Keyboard shortcuts

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