go-blocks/router.go
2025-05-15 14:33:40 +02:00

62 lines
1.2 KiB
Go

package goblocks
import (
"net/http"
"slices"
)
// Middleware is a function that wraps an http.Handler.
type Middleware func(http.Handler) http.Handler
// Router wraps http.ServeMux and provides grouping and middleware support.
type Router struct {
globalChain []Middleware
routeChain []Middleware
isSub bool
*http.ServeMux
}
func NewRouter() *Router {
return &Router{ServeMux: http.NewServeMux()}
}
func (r *Router) Use(mw ...Middleware) {
if r.isSub {
r.routeChain = append(r.routeChain, mw...)
} else {
r.globalChain = append(r.globalChain, mw...)
}
}
func (r *Router) Group(fn func(r *Router)) {
sub := &Router{
ServeMux: r.ServeMux,
routeChain: slices.Clone(r.routeChain),
isSub: true,
}
fn(sub)
}
func (r *Router) HandleFunc(pattern string, h http.HandlerFunc) {
r.Handle(pattern, h)
}
func (r *Router) Handle(pattern string, h http.Handler) {
chain := slices.Clone(r.routeChain)
slices.Reverse(chain)
for _, mw := range chain {
h = mw(h)
}
r.ServeMux.Handle(pattern, h)
}
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
h := http.Handler(r.ServeMux)
chain := slices.Clone(r.globalChain)
slices.Reverse(chain)
for _, mw := range chain {
h = mw(h)
}
h.ServeHTTP(w, req)
}