maybe.go 636 B

123456789101112131415161718
  1. package middleware
  2. import "net/http"
  3. // Maybe middleware will allow you to change the flow of the middleware stack execution depending on return
  4. // value of maybeFn(request). This is useful for example if you'd like to skip a middleware handler if
  5. // a request does not satisfy the maybeFn logic.
  6. func Maybe(mw func(http.Handler) http.Handler, maybeFn func(r *http.Request) bool) func(http.Handler) http.Handler {
  7. return func(next http.Handler) http.Handler {
  8. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  9. if maybeFn(r) {
  10. mw(next).ServeHTTP(w, r)
  11. } else {
  12. next.ServeHTTP(w, r)
  13. }
  14. })
  15. }
  16. }