~netlandish/gobwebs

cbf23a5183370a1c49224cecd8640c8604f06d7e — Peter Sanchez 2 years ago e1ec263
Adding basic middleware
1 files changed, 29 insertions(+), 0 deletions(-)

A middleware.go
A middleware.go => middleware.go +29 -0
@@ 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)
	}
}