From 79b6414727c5ef4f5bd29a89fd84f87781112271 Mon Sep 17 00:00:00 2001 From: Levente Batuska Date: Sat, 26 Oct 2024 14:49:36 +0200 Subject: [PATCH] feat(SimpleRouter): add SubPath function --- simplerouter/router.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/simplerouter/router.go b/simplerouter/router.go index 0c19614..990a9aa 100644 --- a/simplerouter/router.go +++ b/simplerouter/router.go @@ -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) }