0Pricing
Go Academy · Lesson

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

  1. Gin Router and Route Groups
  2. Request Binding and Validation
  3. Response Helpers and Status Codes
  4. Gin Middleware: Auth and Logging
← Back to Go Academy