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:
go run main.go with main.go being the code above
curl localhost:8080/pingx and watch as the logger logs requests on stdout
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:
- Encounter the bug
- 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
gin-contrib/cors#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"))
Please take a look at the following code
And now please take a look at the documentation for Use() (emphasis mine):
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:
go run main.gowith main.go being the code abovecurl localhost:8080/pingxand watch as the logger logs requests on stdoutcurl localhost:8080/pingand 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
.Useto.UseFromNowOnor to have both calls available.This bug prevents from using middlewares with OpenAPI
go-gin-servergenerator . OpenAPI generator is a tool that writes API routes (among others). By nature, a lot of users will add the CORS middleware, and will:Also, this thread shows users struggling with the bug. It has been around for years
gin-contrib/cors#29
Thank you for your understanding
Note. For some people, it might look intuitive in the code above that
UsemeansUseFromNowOn. But I happened to work on this, whereNewRouteris generated code. It is way less explicit.