router improvements

This commit is contained in:
Pedro Pérez 2025-05-29 13:06:38 +02:00
parent d54fbcacf0
commit 48fe13eecc

View File

@ -29,12 +29,25 @@ func (r *Router) Use(mw ...Middleware) {
}
}
func (r *Router) Group(fn func(r *Router)) {
func (r *Router) Group(basePath string, fn func(r *Router)) {
sub := &Router{
ServeMux: r.ServeMux,
routeChain: slices.Clone(r.routeChain),
isSub: true,
}
// Añadir middleware para manejar el basePath
sub.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if !strings.HasPrefix(req.URL.Path, basePath) {
http.NotFound(w, req)
return
}
req.URL.Path = strings.TrimPrefix(req.URL.Path, basePath)
next.ServeHTTP(w, req)
})
})
fn(sub)
}