From 173e12dcf79785e37931c6b06e944081229db2a3 Mon Sep 17 00:00:00 2001 From: miloud Date: Fri, 23 Feb 2024 00:45:30 +0100 Subject: [PATCH] added feat gethandler name --- gin.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/gin.go b/gin.go index 24a9864a..ce0f2f34 100644 --- a/gin.go +++ b/gin.go @@ -291,6 +291,22 @@ func (engine *Engine) SetFuncMap(funcMap template.FuncMap) { engine.FuncMap = funcMap } +// GetHandlerPath takes a name of a handler and returns a slice of matched paths associated with that handler. +func (engine *Engine) GetHandlerPath(handlerName string) []string { + handlers := engine.Routes() + var paths []string + + for _, f := range handlers { + handler := strings.Split(f.Handler, ".") + + if handler[len(handler)-1] == handlerName { + paths = append(paths, f.Path) + } + } + + return paths +} + // NoRoute adds handlers for NoRoute. It returns a 404 code by default. func (engine *Engine) NoRoute(handlers ...HandlerFunc) { engine.noRoute = handlers