Skip to content

Commit

Permalink
middleware: add Maybe helper
Browse files Browse the repository at this point in the history
  • Loading branch information
pkieltyka committed Nov 15, 2021
1 parent 2bfa508 commit 520175f
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions middleware/maybe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package middleware

import "net/http"

// Maybe middleware will allow you to change the flow of the middleware stack execution depending on return
// value of maybeFn(request). This is useful for example if you'd like to skip a middleware handler if
// a request does not satisfied the maybeFn logic.
func Maybe(mw func(http.Handler) http.Handler, maybeFn func(r *http.Request) bool) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if maybeFn(r) {
mw(next).ServeHTTP(w, r)
} else {
next.ServeHTTP(w, r)
}
})
}
}

0 comments on commit 520175f

Please sign in to comment.