feat(SimpleRouter): add SubPath function

This commit is contained in:
2024-10-26 14:49:36 +02:00
parent 5096721360
commit 79b6414727

View File

@@ -10,7 +10,6 @@ import (
"time"
)
// Use this router for graceful shutdown
func (rg *RouteGroup) StartServer(addr string) *http.Server {
server := &http.Server{
Addr: addr,
@@ -118,6 +117,17 @@ func (rg *RouteGroup) registerRoute(method string, path string, handler http.Han
rg.mux.HandleFunc(fullPath, rg.applyMiddlewares(handler).ServeHTTP)
}
func (rg *RouteGroup) SubPath(path string) *RouteGroup {
rgc := &RouteGroup{
mux: rg.mux,
basePath: rg.basePath + path + "/",
}
middlewares := make([]Middleware, len(rg.middlewares))
copy(middlewares, rg.middlewares)
rgc.middlewares = middlewares
return rgc
}
func (rg *RouteGroup) HandleFunc(method string, path string, handler http.HandlerFunc) {
rg.registerRoute(method, path, handler)
}