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 ¶
- func AcceptsGzip(header string) bool
- func CORS(origins []string, next http.Handler) http.Handler
- func CORSWithPreflight(origins []string, allowMethods string, next http.Handler) http.Handler
- func Gzip(next http.Handler) http.Handler
- func Healthz(w http.ResponseWriter, _ *http.Request)
- func ListenAndServe(ctx context.Context, h http.Handler, o ServeOptions) error
- func OriginAllowed(origins []string, origin string) bool
- func Preflight(origins []string, allowMethods string) http.HandlerFunc
- type ServeOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AcceptsGzip ¶
AcceptsGzip reports whether an Accept-Encoding header value asks for gzip (directly or via "*"), honoring q=0 exclusions.
func CORS ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.