Documentation
¶
Overview ¶
Package routegroup provides a way to group routes and applies middleware to them. Works with the standard library's http.ServeMux.
Index ¶
- type Bundle
- func (b *Bundle) Group() *Bundle
- func (b *Bundle) Handle(path string, handler http.HandlerFunc)
- 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 ¶
This section is empty.
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() {
mux := http.NewServeMux()
group := routegroup.Mount(mux, "/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.Handle("GET /test", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
group.Handle("POST /test2", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
// start the server
if err := http.ListenAndServe(":8080", mux); 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() {
mux := http.NewServeMux()
group := routegroup.New(mux)
// 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.Handle("GET /test", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
group.Handle("POST /test2", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
// start the server
if err := http.ListenAndServe(":8080", mux); err != nil {
panic(err)
}
}
Output:
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 ¶
func (b *Bundle) Handle(path string, handler http.HandlerFunc)
Handle adds a new route to the Group's mux, applying all middlewares to the handler.
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() {
mux := http.NewServeMux()
group := routegroup.New(mux)
// 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.Handle("GET /test", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
g.Handle("POST /test2", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
})
// start the server
if err := http.ListenAndServe(":8080", mux); 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