IHttpMiddlewares
About Middlewares
Middlewares are used to perform operations before the handler function is executed. These are functions that accept http.Handler
and return http.Handler
. Below is an example of how to define a middleware:
main.go
middleware := func (next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// do something before the handler function
next.ServeHTTP(w, r)
})
}
handler := func (w http.ResponseWriter, r *http.Request) {
// handle http request
}
Then you can use the middleware in the http route:
main.go
router := api.Http().HttpRouter().PluginRouter()
router.Get("/welcome", handler, middleware).Name("welcome")
Built-in Middlewares
To get an instance of the built-in IHttpMiddlewares
:
Below are the list of available built-in middlewares in IHttpMiddlewares
:
AdminAuth
It returns a middleware that ensures that only authenticated admins can access the route.
pluginRouter := api.Http().HttpRouter().PluginRouter()
authMw := httpMw.AdminAuth()
handler := func(w http.ResponseWriter, r *http.Request) {
// handle the http request...
}
pluginRouter.Get("/protected-page", handler, authMw)
CacheResponse
It returns a middleware that caches the response throughout the duration of the application lifetime which can improve system performance.
pluginRouter := api.Http().HttpRouter().PluginRouter()
cacheMw := httpMw.CacheResponse()
handler := func(w http.ResponseWriter, r *http.Request) {
// handle the http request...
}
pluginRouter.Get("/some-generated-image.png", handler, cacheMw)
PendingPurchase
It returns a middleware that redirects the user to the pending order payment page when a pending purchase request is present.