Documentation ¶
Overview ¶
Package mux is an HTTP multiplexer that uses a radix tree as its backbone.
Example ¶
package main import ( "net/http" "github.com/gbrlsnchs/mux" ) func main() { type key uint8 const ctxKey key = 0 handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }) m := mux.NewRouter("/api", ctxKey) m.Handle(http.MethodGet, "/ping", handler) }
Output:
Index ¶
- func Params(ctx context.Context, ctxKey interface{}) map[string]string
- type MiddlewareFunc
- type Router
- func (rt *Router) Handle(method, path string, handler http.Handler)
- func (rt *Router) HandleFunc(method, path string, handler http.HandlerFunc)
- func (rt *Router) Router(path string) *Router
- func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (rt *Router) SetDebug(debug bool)
- func (rt *Router) SetPlaceholder(c byte)
- func (rt *Router) String() string
- func (rt *Router) Use(fns ...MiddlewareFunc)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type MiddlewareFunc ¶
MiddlewareFunc is a middleware adapter.
func NewChain ¶ added in v0.3.0
func NewChain(fns ...MiddlewareFunc) MiddlewareFunc
NewChain creates a middleware chain, which is a middleware itself.
Example ¶
package main import ( "log" "net/http" "github.com/gbrlsnchs/mux" ) func main() { type key uint8 const ctxKey key = 0 m := mux.NewRouter("/api", ctxKey) guard := mux.NewChain( func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Printf("%s %s", r.Method, r.URL.Path) next.ServeHTTP(w, r) }) }, func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if mux.Params(r.Context(), ctxKey)["secret"] != "my_secret" { w.WriteHeader(http.StatusUnauthorized) return } next.ServeHTTP(w, r) }) }, ) handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }) m.Handle(http.MethodPost, "/unprotected", handler) m.Handle(http.MethodPost, "/protected/:secret", guard(handler)) }
Output:
type Router ¶ added in v0.2.0
type Router struct {
// contains filtered or unexported fields
}
Router is an HTTP multiplexer.
func (*Router) HandleFunc ¶ added in v0.3.0
func (rt *Router) HandleFunc(method, path string, handler http.HandlerFunc)
HandleFunc sets an HTTP request handler function.
func (*Router) ServeHTTP ¶ added in v0.3.0
func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface by finding an endpoint in the trie. If there are any parameters, they are set to the request's context.
func (*Router) SetPlaceholder ¶ added in v0.3.0
SetPlaceholder sets a different placeholder to be used in dynamic paths.
func (*Router) Use ¶ added in v0.3.0
func (rt *Router) Use(fns ...MiddlewareFunc)
Use set middleware functions to run before each request.
Click to show internal directories.
Click to hide internal directories.