echo icon indicating copy to clipboard operation
echo copied to clipboard

middleware is applied for all the path registered before and after `echo.Use`

Open kemingy opened this issue 5 years ago • 5 comments

Issue Description

Checklist

  • [x] Dependencies installed
  • [x] No typos
  • [x] Searched existing issues and docs

Expected behaviour

According to the document https://echo.labstack.com/middleware

Middleware registed using Echo#Use() is only executed for paths which are registered after Echo#Use() has been called.

Actual behaviour

Middleware registed using Echo#Use() is executed for paths which are registered before and after Echo#Use() has been called.

I guess the document is not updated?

Steps to reproduce

Working code to debug

package main

import (
	"net/http"

	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
)

func main() {
	e := echo.New()
	e.GET("/", func(c echo.Context) error {
		return c.String(http.StatusOK, "hello world")
	})
	e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
		Format: "method=${method}, uri=${uri}, status=${status}\n",
	}))
	e.GET("/log", func(c echo.Context) error {
		return c.String(http.StatusOK, "logging")
	})
	e.Logger.Fatal(e.Start(":8000"))
}

Version/commit

v4.1.14

kemingy avatar Mar 02 '20 08:03 kemingy

Have you found any workaround @kemingy? I've tried to play with groups without success. It's like if the root middlewares are always applied...

jeanhalle avatar Apr 22 '20 03:04 jeanhalle

Have you found any workaround @kemingy? I've tried to play with groups without success. It's like if the root middlewares are always applied...

I guess currently the only way is using the middleware skipper.

kemingy avatar Apr 22 '20 03:04 kemingy

Which is not available on every third-party middleware...I've finally moved all my stuff into one group (and apply middleware there) and left my health endpoints at root level.

jeanhalle avatar Apr 22 '20 03:04 jeanhalle

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 21 '20 04:06 stale[bot]

#1660 is still alive, so I'm reopening the issue

pafuent avatar Dec 16 '20 03:12 pafuent

So I am getting 200 logs for my health check endpoints and I would prefer to either not see them at all or see them only if I set a log level of debug. What is the current solution to something like this?

https://github.com/labstack/echo/pull/1660 ended up getting closed.

ekeric13 avatar Feb 01 '23 01:02 ekeric13

Middleware skippers are the way to exclude certain paths to be excluded from middleware.

If you need to add skipper to some 3rd party middleware that does not have it, you can do this:

	e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
		loggerMw := middleware.Logger() //  to showcase some "3rd party" middleware
		return func(c echo.Context) error {
			if c.Path() == "/health" {
				return next(c)
			}
			return loggerMw(next)(c)
		}
	})

p.s. Logger and RequestLogger middlewares have skipper functionality built in

aldas avatar Feb 01 '23 05:02 aldas

Okay yeah I can just use this fn: https://pkg.go.dev/github.com/labstack/echo/[email protected]/middleware#LoggerWithConfig And then pass it:

LoggerConfig{
  Skipper: func(c echo.Context) bool {
			if c.Path() == "/health" {
				return true
			}
                        return false
                  }
}

ekeric13 avatar Feb 02 '23 00:02 ekeric13