Skip to content

get name Route from middleware #2081

Answered by aldas
johndiego asked this question in Q&A
Jan 24, 2022 · 2 comments · 2 replies
Discussion options

You must be logged in to vote

maybe

	routeNameMiddleware := func(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)
		}
	}
	e.Use(routeNameMiddleware)

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@johndiego
Comment options

Comment options

You must be logged in to vote
1 reply
@johndiego
Comment options

Answer selected by johndiego
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants