Go uses the middleware of the GIN framework

Middleware mainly refers to the interception of HTTP requests and some processing before the request arrives, such as data processing, permission control, authentication and so on.

R := gn.default (), Logger() and Recovery() are used globally for both middleware. If you don’t want to use either middleware, you can use r := gn.new ().

Middleware notation, which returns gin.HandlerFunc

func RequestInfos() gin.HandlerFunc {
  return func(Context *gin.Context){
  path := context.FullPath()
  method := context.Request.Method
  。。。。
  }
}

Added global middleware so RequestInfos() is executed first for every HTTP request

r.Use(requestInfos())

A single route is used, and RequestInfos() is executed when accessing the /benchmark route.

r.GET("/benchmark", requestInfos(), benchEndpoint)

Used in routing groups, middleware is used when accessing /test/ ABC

r := gin.Default()
v := r.Group("/test")
v.Use(requestInfos())
{
  v.POST("/abc", benchEndpoint)
}

How do I make the middleware execute after the request is executed

func RequestInfos() gin.HandlerFunc {
return func(context *gin.Context){
path := context.FullPath()
method := context.Request.Method
context.Next()
// after the request is executed
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish