Middleware

Create a Middleware

Middleware is a function that receives a Handle and returns a Handle. Let’s take ServerHeader as an example, it writes Server header and then invokes the next middleware or handler.

func ServerHeader(srv string) clevergo.MiddlewareFunc {
    return func(next clevergo.Handle) clevergo.Handle {
        return func(c *clevergo.Context) error {
            // writes server header.
            c.Response.Header().Set("Server", srv)
            // invokes the next middleware or handler.
            return next(c)
        }
    }
}

Global Middleware

app.Use(
    clevergo.Recovery(true), // built-in recovery middleware.
    ServerHeader("CleverGo"),
)

Route Group Middleware

See Route Group.

Route Middleware

See Route Middleware.

Third-party Middleware

WrapHH is an adapter that converts func(http.Handler) http.Handler to a middleware, you can use most of the third-party middleware in CleverGo without pain.

app.Use(
    // https://github.com/nytimes/gziphandler
    clevergo.WrapHH(gziphandler.GzipHandler),
)
comments powered by Disqus