Conditional is a middleware that only executes middleware if the condition returns true for the request. If the condition returns false, the middleware is skipped, and request handling moves on to the next handler in the chain.
(middleware MiddlewareFunc, condition func(r *http.Request) bool)
| 32 | // returns true for the request. If the condition returns false, the middleware |
| 33 | // is skipped, and request handling moves on to the next handler in the chain. |
| 34 | func Conditional(middleware MiddlewareFunc, condition func(r *http.Request) bool) MiddlewareFunc { |
| 35 | return func(next http.Handler) http.Handler { |
| 36 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 37 | handler := next |
| 38 | if condition(r) { |
| 39 | handler = middleware(next) |
| 40 | } |
| 41 | handler.ServeHTTP(w, r) |
| 42 | }) |
| 43 | } |
| 44 | } |