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) HandleFiles(pattern string, root http.FileSystem)
- func (b *Bundle) HandleFunc(pattern string, handler http.HandlerFunc)
- func (b *Bundle) HandleRoot(method string, handler http.Handler)
- func (b *Bundle) HandleRootFunc(method 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) NotFoundHandler(handler http.HandlerFunc)
- 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 used to disable auto-registration of a catch-all 404. Deprecated: now a no-op retained for API compatibility.
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) HandleFiles ¶ added in v1.3.0
func (b *Bundle) HandleFiles(pattern string, root http.FileSystem)
HandleFiles is a helper to serve static files from a directory
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) HandleRoot ¶ added in v1.4.0
HandleRoot adds a handler for the group's root path without trailing slash. This avoids the 301 redirect that would occur with a "/" pattern. Method parameter can be empty to register for all HTTP methods.
Example ¶
This example shows how to use HandleRoot to handle the root path of a mounted group without trailing slash
package main
import (
"fmt"
"net/http"
"github.com/go-pkgz/routegroup"
)
func main() {
group := routegroup.New(http.NewServeMux())
// create API group
apiGroup := group.Mount("/api")
// apply middleware
apiGroup.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-API", "true")
next.ServeHTTP(w, r)
})
})
// handle root path (responds to /api without redirect)
apiGroup.HandleRoot("GET", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "API root")
}))
// regular routes
apiGroup.HandleFunc("GET /users", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "List of users")
})
}
Output:
func (*Bundle) HandleRootFunc ¶ added in v1.4.1
func (b *Bundle) HandleRootFunc(method string, handler http.HandlerFunc)
HandleRootFunc is like HandleRoot but takes a handler function.
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) NotFoundHandler ¶ added in v1.2.0
func (b *Bundle) NotFoundHandler(handler http.HandlerFunc)
NotFoundHandler sets a custom handler for any unmatched routes (404 responses). Note: This handler is only used for true 404s. Requests to valid paths with incorrect HTTP methods will still return 405 Method Not Allowed with Allow header.
func (*Bundle) Route ¶ added in v0.2.0
Route allows for configuring the Group inside the configureFn function. When called on the root bundle, it automatically creates a new group to avoid accidentally modifying the root bundle's middleware stack.
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
func (*Bundle) Use ¶
func (b *Bundle) Use(middleware func(http.Handler) http.Handler, more ...func(http.Handler) http.Handler)
Use adds middleware(s) to the Group. Middlewares are executed in the order they are added. Note: Root-level middlewares (added to the root bundle) have access to the matched route pattern via r.Pattern, but execute before path parameters are parsed. Therefore, r.PathValue() will return empty strings in root middlewares. Middlewares on mounted groups execute after routing and have full access to path values.
func (*Bundle) With ¶
func (b *Bundle) With(middleware func(http.Handler) http.Handler, more ...func(http.Handler) http.Handler) *Bundle
With adds new middleware(s) to the Group and returns a new Group with the updated middleware stack. The With method is similar to Use, but instead of modifying the current Group, it returns a new Group instance with the added middleware(s). This allows for creating chain of middleware without affecting the original Group.