Description
Currently we can add middlewares to gin routes. However, there is no easy way to add middleware "just before" the real HandlerFunc. Why this could be useful? For instance we are using this way to measure application performance in function level (spans). Someone might could say "then add it as last one". But it is not that easy when you have tens of router groups, it needs to be added ALL groups and routes as last one. If single route has middleware, it needs to be added there as last one as well.
Expectations
how we are solving this currently?
rg := &r.RouterGroup
GET(rg, "/v1/example", router.example)
v1foo := r.Group("/v1/foo")
{
GET(v1foo, "/disable", router.foo)
}
// GET wrapper to include sentrySpanTracer as last middleware.
func GET(group *gin.RouterGroup, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return group.Handle(http.MethodGet, relativePath, addSpanTracer(handlers)...)
}
// will add middleware just before (as last middleware) the real HandlerFunc
func addSpanTracer(handlers []gin.HandlerFunc) []gin.HandlerFunc {
lastElement := handlers[len(handlers)-1]
handlers = handlers[:len(handlers)-1]
handlers = append(handlers, sentrySpanTracer(), lastElement)
return handlers
}
The original code:
r.GET("/v1/example", router.example)
v1foo := r.Group("/v1/foo")
{
v1foo.GET("/disable", router.foo)
}
Comment From: notAxion
would like to have this as well