From cbf23a5183370a1c49224cecd8640c8604f06d7e Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Tue, 23 Aug 2022 14:04:45 -0600 Subject: [PATCH] Adding basic middleware --- middleware.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 middleware.go diff --git a/middleware.go b/middleware.go new file mode 100644 index 0000000..6e45a82 --- /dev/null +++ b/middleware.go @@ -0,0 +1,29 @@ +package gobwebs + +import ( + "log" + "sync" + + "github.com/labstack/echo/v4" +) + +// RouteDebugMiddleware prints the route name of a matched path. +// From: https://github.com/labstack/echo/discussions/2081#discussioncomment-2040098 +func RouteDebugMiddleware(next echo.HandlerFunc) echo.HandlerFunc { + var doOnce sync.Once + var registeredRoutes []*echo.Route + + return func(c echo.Context) error { + doOnce.Do(func() { + registeredRoutes = c.Echo().Routes() + }) + + for _, r := range registeredRoutes { + if r.Method == c.Request().Method && r.Path == c.Path() { + log.Printf("*** Route Name: %s", r.Name) + break + } + } + return next(c) + } +} -- 2.45.2