Documentation
¶
Index ¶
- type BetterMux
- func (mux *BetterMux) Delete(pattern string, handler http.HandlerFunc)
- func (mux *BetterMux) Get(pattern string, handler http.HandlerFunc)
- func (mux *BetterMux) Handle(pattern string, handler http.Handler)
- func (mux *BetterMux) HandleFunc(pattern string, fn http.HandlerFunc)
- func (mux *BetterMux) Mount(path string, handler http.Handler)
- func (mux *BetterMux) Options(pattern string, handler http.HandlerFunc)
- func (mux *BetterMux) Patch(pattern string, handler http.HandlerFunc)
- func (mux *BetterMux) Post(pattern string, handler http.HandlerFunc)
- func (mux *BetterMux) Put(pattern string, handler http.HandlerFunc)
- func (mux *BetterMux) Route(path string, fn func(*BetterMux)) *BetterMux
- func (mux *BetterMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (mux *BetterMux) With(mfn func(http.Handler) http.Handler) *BetterMux
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BetterMux ¶
type BetterMux struct {
// RealMux is the underlying *http.ServeMux that all routes are
// registered on and that ServeHTTP delegates to.
RealMux *http.ServeMux
// ParentMux points to the BetterMux this one was derived from via With,
// and is used to walk the middleware chain at route registration time.
// It is nil for a mux created by NewBetterMux or
// NewBetterMuxWithServeMux.
ParentMux *BetterMux
// Middleware is the middleware function declared by With on this mux.
// It is nil unless the mux was created by With.
Middleware func(http.Handler) http.Handler
}
BetterMux is a group routing and middleware wrapper around the standard library's http.ServeMux. It has zero runtime overhead: all of its logic runs at route registration time, and serving is delegated entirely to the underlying *http.ServeMux.
The API is inspired by github.com/go-chi/chi.
func NewBetterMux ¶ added in v1.1.0
func NewBetterMux() *BetterMux
NewBetterMux returns a new BetterMux backed by a fresh http.ServeMux.
func NewBetterMuxWithServeMux ¶ added in v1.1.0
NewBetterMuxWithServeMux returns a new BetterMux that wraps the given *http.ServeMux. If mux is nil, a new http.ServeMux is created.
func (*BetterMux) Delete ¶
func (mux *BetterMux) Delete(pattern string, handler http.HandlerFunc)
Delete registers handler for the "DELETE <pattern>" pattern, using the Go 1.22+ enhanced ServeMux routing.
func (*BetterMux) Get ¶
func (mux *BetterMux) Get(pattern string, handler http.HandlerFunc)
Get registers handler for the "GET <pattern>" pattern, using the Go 1.22+ enhanced ServeMux routing (pattern may contain wildcards such as "/users/{id}", read via r.PathValue).
func (*BetterMux) Handle ¶
Handle registers handler for the given pattern on the underlying http.ServeMux, applying the mux's middleware chain (declared via With) to the handler first.
func (*BetterMux) HandleFunc ¶
func (mux *BetterMux) HandleFunc(pattern string, fn http.HandlerFunc)
HandleFunc registers the handler function fn for the given pattern, equivalent to Handle(pattern, fn).
func (*BetterMux) Mount ¶ added in v1.1.0
Mount attaches handler to the given path prefix. The prefix is stripped from the request path before it reaches handler (via http.StripPrefix), so a handler mounted at "/static" receives requests rooted at "/".
Both "/static" and "/static/" are accepted and produce the same registration (the subtree pattern "/static/").
func (*BetterMux) Options ¶
func (mux *BetterMux) Options(pattern string, handler http.HandlerFunc)
Options registers handler for the "OPTIONS <pattern>" pattern, using the Go 1.22+ enhanced ServeMux routing.
func (*BetterMux) Patch ¶
func (mux *BetterMux) Patch(pattern string, handler http.HandlerFunc)
Patch registers handler for the "PATCH <pattern>" pattern, using the Go 1.22+ enhanced ServeMux routing.
func (*BetterMux) Post ¶
func (mux *BetterMux) Post(pattern string, handler http.HandlerFunc)
Post registers handler for the "POST <pattern>" pattern, using the Go 1.22+ enhanced ServeMux routing.
func (*BetterMux) Put ¶
func (mux *BetterMux) Put(pattern string, handler http.HandlerFunc)
Put registers handler for the "PUT <pattern>" pattern, using the Go 1.22+ enhanced ServeMux routing.
func (*BetterMux) Route ¶
Route creates a child BetterMux, runs fn on it to register routes, and mounts the child at the given path prefix (see Mount). It returns the child mux so routes can also be registered afterwards.
func (*BetterMux) ServeHTTP ¶
func (mux *BetterMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler by delegating to the underlying http.ServeMux.
func (*BetterMux) With ¶
With returns a new BetterMux that shares the same underlying http.ServeMux and declares mfn as middleware for it. With can be chained; middleware declared first is applied first (outermost). The middleware chain is applied to a handler when it is registered via Handle, HandleFunc, or any of the method helpers such as Get.