router improvements
This commit is contained in:
parent
d54fbcacf0
commit
48fe13eecc
15
router.go
15
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{
|
sub := &Router{
|
||||||
ServeMux: r.ServeMux,
|
ServeMux: r.ServeMux,
|
||||||
routeChain: slices.Clone(r.routeChain),
|
routeChain: slices.Clone(r.routeChain),
|
||||||
isSub: true,
|
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)
|
fn(sub)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user