Documentation ¶
Overview ¶
Package routegroup provides a way to group routes and applies middleware to them. Works with the standard library's http.ServeMux.
Index ¶
- func Wrap(handler http.Handler, mw1 func(http.Handler) http.Handler, ...) http.Handler
- type Bundle
- func (b *Bundle) DisableNotFoundHandler()
- func (b *Bundle) Group() *Bundle
- func (b *Bundle) Handle(pattern string, handler http.Handler)
- func (b *Bundle) HandleFunc(pattern string, handler http.HandlerFunc)
- func (b *Bundle) Handler(r *http.Request) (h http.Handler, pattern string)
- func (b *Bundle) Mount(basePath string) *Bundle
- func (b *Bundle) Route(configureFn func(*Bundle))
- func (b *Bundle) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (b *Bundle) Use(middleware func(http.Handler) http.Handler, ...)
- func (b *Bundle) With(middleware func(http.Handler) http.Handler, ...) *Bundle
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Bundle ¶
type Bundle struct {
// contains filtered or unexported fields
}
Bundle represents a group of routes with associated middleware.
func Mount ¶
Mount creates a new group with a specified base path.
Example ¶
package main import ( "net/http" "github.com/go-pkgz/routegroup" ) func main() { group := routegroup.Mount(http.NewServeMux(), "/api") // apply middleware to the group group.Use(func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Add("X-Test-Middleware", "true") next.ServeHTTP(w, r) }) }) // add test handlers group.HandleFunc("GET /test", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }) group.HandleFunc("POST /test2", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }) // start the server if err := http.ListenAndServe(":8080", group); err != nil { panic(err) } }
Output:
func New ¶
New creates a new Group.
Example ¶
package main import ( "net/http" "github.com/go-pkgz/routegroup" ) func main() { group := routegroup.New(http.NewServeMux()) // apply middleware to the group group.Use(func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Add("X-Mounted-Middleware", "true") next.ServeHTTP(w, r) }) }) // add test handlers group.HandleFunc("GET /test", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }) group.HandleFunc("POST /test2", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }) // start the server if err := http.ListenAndServe(":8080", group); err != nil { panic(err) } }
Output:
func (*Bundle) DisableNotFoundHandler ¶ added in v1.1.0
func (b *Bundle) DisableNotFoundHandler()
DisableNotFoundHandler disables the automatic registration of a not found handler for the root path.
func (*Bundle) Group ¶ added in v0.2.0
Group creates a new group with the same middleware stack as the original on top of the existing bundle.
func (*Bundle) Handle ¶
Handle adds a new route to the Group's mux, applying all middlewares to the handler.
func (*Bundle) HandleFunc ¶ added in v0.4.0
func (b *Bundle) HandleFunc(pattern string, handler http.HandlerFunc)
HandleFunc registers the handler function for the given pattern to the Group's mux. The handler is wrapped with the Group's middlewares.
func (*Bundle) Handler ¶ added in v0.4.0
Handler returns the handler and the pattern that matches the request. It always returns a non-nil handler, see http.ServeMux.Handler documentation for details.
func (*Bundle) Mount ¶ added in v0.2.0
Mount creates a new group with a specified base path on top of the existing bundle.
func (*Bundle) Route ¶ added in v0.2.0
Route allows for configuring the Group inside the configureFn function.
Example ¶
package main import ( "net/http" "github.com/go-pkgz/routegroup" ) func main() { group := routegroup.New(http.NewServeMux()) // configure the group using Set group.Route(func(g *routegroup.Bundle) { // apply middleware to the group g.Use(func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Add("X-Test-Middleware", "true") next.ServeHTTP(w, r) }) }) // add test handlers g.HandleFunc("GET /test", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }) g.HandleFunc("POST /test2", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }) }) // start the server if err := http.ListenAndServe(":8080", group); err != nil { panic(err) } }
Output:
func (*Bundle) ServeHTTP ¶ added in v0.2.0
func (b *Bundle) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface