Gin Middleware: Auth and Logging
Writing and registering Gin middleware
Gin middleware signature
Gin middleware is a gin.HandlerFunc that calls c.Next() to pass control to the next handler, or c.Abort() to stop the chain.
func MyMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
// before handler
c.Next()
// after handler
}
}Applying middleware globally
Use r.Use to apply middleware to all routes:
r := gin.New()
r.Use(gin.Logger())
r.Use(gin.Recovery())All lessons in this course
- Gin Router and Route Groups
- Request Binding and Validation
- Response Helpers and Status Codes
- Gin Middleware: Auth and Logging