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

httpx

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package httpx is the thin layer over net/http.ServeMux that gives the server prefixed, middleware-carrying route groups and a record of every registered pattern (for the route-table test and `hatamoto routes`).

Routing rule: the mux is populated once at boot and never changes; anything dynamic (plugin routes) is resolved inside fixed catch-all patterns.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Refuse

func Refuse(w http.ResponseWriter, r *http.Request)

Refuse answers a request the admin refuses to acknowledge (a sealed admin path, an unknown path under the admin prefix) with the site's standard not-found response, so a probe learns nothing. Until a responder is registered it writes a plain 404. It is safe for concurrent use.

func RegisterRefuser

func RegisterRefuser(h http.HandlerFunc)

RegisterRefuser sets the handler Refuse calls. The server registers its themed standard not-found responder at construction. Registration is atomic and the last writer wins.

func WithErrorBodies

func WithErrorBodies(mux *http.ServeMux, fail func(w http.ResponseWriter, status int)) http.Handler

WithErrorBodies serves mux, replacing the plain-text 404 and 405 that ServeMux generates with the body written by fail(w, status). The Allow header of a 405 is kept.

Types

type Group

type Group struct {
	// contains filtered or unexported fields
}

Group registers patterns under a path prefix, wrapping every handler in the group's middleware.

func NewGroup

func NewGroup(mux registrar, prefix string, mw ...Middleware) *Group

NewGroup returns the root group of mux; all groups derived from it share one route record.

func (*Group) Group

func (g *Group) Group(prefix string, mw ...Middleware) *Group

Group returns a nested group: prefix is appended and mw run inside the parent's middleware.

func (*Group) Handle

func (g *Group) Handle(pattern string, h http.Handler)

Handle registers pattern ("[METHOD ]/path", path relative to the group). A path of "/" matches the group's prefix with or without a trailing slash, and nothing below it. A method is required (a method-less pattern would conflict with the catch-alls). Invalid or conflicting patterns panic at registration.

func (*Group) HandleFunc

func (g *Group) HandleFunc(pattern string, h http.HandlerFunc)

HandleFunc is Handle for a handler function.

func (*Group) Routes

func (g *Group) Routes() []Route

Routes returns every pattern registered through the group tree so far.

type Middleware

type Middleware = func(http.Handler) http.Handler

Middleware wraps a handler.

func Chain

func Chain(mw ...Middleware) Middleware

Chain composes middleware; the first runs outermost.

type Recorder

type Recorder struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

Recorder wraps a ResponseWriter to record the status code and body size. Unwrap exposes the underlying writer to http.ResponseController, so Flush, Hijack and deadlines keep working through it.

func NewRecorder

func NewRecorder(w http.ResponseWriter) *Recorder

NewRecorder wraps w.

func (*Recorder) BytesWritten

func (r *Recorder) BytesWritten() int

BytesWritten is the number of body bytes written.

func (*Recorder) Status

func (r *Recorder) Status() int

Status is the written status code, or 0 when the handler never wrote one.

func (*Recorder) Unwrap

func (r *Recorder) Unwrap() http.ResponseWriter

Unwrap returns the wrapped writer for http.ResponseController.

func (*Recorder) Write

func (r *Recorder) Write(b []byte) (int, error)

func (*Recorder) WriteHeader

func (r *Recorder) WriteHeader(code int)

type Route

type Route struct {
	Pattern string // "METHOD /full/path" as given to the mux
	Handler string
}

Route is one registered pattern and the name of its handler.

type Router

type Router interface {
	Handle(pattern string, h http.Handler)
	HandleFunc(pattern string, h http.HandlerFunc)
}

Router is the registration surface handed to route mounters.

Jump to

Keyboard shortcuts

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