diff --git a/router.go b/router.go index c48e118..ddc88a2 100644 --- a/router.go +++ b/router.go @@ -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) }