Please take a look at the following code
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.New()
r.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{}) })
r.Use(gin.Logger())
r.GET("/pingx", func(c *gin.Context) { c.JSON(200, gin.H{}) })
r.Run()
}
And now please take a look at the documentation for Use() (emphasis mine):
Use attaches a global middleware to the router. ie. the middleware attached though Use() will be included in the handlers chain for every single request
Who in the world would have guessed that the middlewares are only called with the routes that where declared after them, not before?
Steps to reproduce:
1. go run main.go
with main.go being the code above
2. curl localhost:8080/pingx
and watch as the logger logs requests on stdout
3. curl localhost:8080/ping
and watch as nothing is logged anywhere.
To me this is a bug but I have a feeling that this is intended behavior...
If it is intended, I would suggest to update the documentation, but this is not enough.
I would also change the README because this behavior is documented nowhere.
Also I suggest renaming .Use
to .UseFromNowOn
or to have both calls available.
This bug prevents from using middlewares with OpenAPI go-gin-server
generator . OpenAPI generator is a tool that writes API routes (among others). By nature, a lot of users will add the CORS middleware, and will:
1. Encounter the bug
2. Add code in an auto-generated file as a work-around
Also, this thread shows users struggling with the bug. It has been around for years https://github.com/gin-contrib/cors/issues/29
Thank you for your understanding
- go version: go1.12.7
- gin version (or commit ref): v1.4.0-dev
- operating system: linux/amd64
Note. For some people, it might look intuitive in the code above that Use
means UseFromNowOn
. But I happened to work on this, where NewRouter
is generated code. It is way less explicit.
router := routers.NewRouter()
router.Use(cors.Default())
log.Fatal(router.Run(":8000"))
Comment From: simplytunde
@sguillia I just confirmed this behavior with Use
. I think a clearer documentation that points out this behavior will be a better.
Comment From: aight8
Same for group.Use(...) The middleware is not global anymore than.
Comment From: enarha
Just encountered the issue myself. I was writing a test to verify the middleware (a logger) is used and I was using a method to override the logger from the test. The logger was only used for the 404 responses and not for the existing routes defined before the call of Use(...). Looking at the code https://github.com/gin-gonic/gin/blob/master/gin.go#L303-L307 I see two methods are called to rebuild 404 and 405 handlers and that's probably the reason why the 404 responses are still covered by the middleware. I guess the same thing should be done for just all handlers. I can work on a fix unless someone argues that the existing behavior is the required one. In that case I can create only a fix for the documentation.
Comment From: avi-sailthru
Have got the same issue. Anyone know a way around it ?
Comment From: enarha
Once you are aware of the issue the workaround is easy, you just set the Use
methods before the GET
/POST
etc handlers.
r := gin.New()
// setting middleware before routes, otherwise it does not work (gin bug)
r.Use(ginzap.Ginzap(zapLogger, time.RFC3339, true))
r.Use(ginzap.RecoveryWithZap(zapLogger, true))
r.GET("/", handler1)
r.POST("/", handler2)
...
Comment From: avi-sailthru
Issue is I'm using OpenAPI go-gin-server generator. So my router is already setup before hand. If we can't use Use()
after declaring the handlers I can't make it work other than touching the generated code.
Comment From: enarha
Understood. I'll look into that.