Documentation
¶
Overview ¶
Package routegroup provides a way to group routes and applies middleware to them. Works with the standard library's http.ServeMux.
Index ¶
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-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 (*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) Set ¶
Set allows for configuring the Group.
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.Set(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:
Click to show internal directories.
Click to hide internal directories.