blob: 60b13986633086579d995b74b50fff8bf0c0d04a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package main
import "net/http"
type conditionalMiddlewareFunc func(r *http.Request) bool
// conditionalMiddleware simply uses handler a if condition is true and handler
// b if condition is false.
func conditionalMiddleware(condition conditionalMiddlewareFunc,
a, b http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if condition(r) {
a.ServeHTTP(w, r)
} else {
b.ServeHTTP(w, r)
}
})
}
|