keel/net/http/middleware/skipper.go
2021-09-06 22:13:07 +02:00

36 lines
832 B
Go

package middleware
import (
"net/http"
)
type Skipper func(*http.Request) bool
// RequestURIWhitelistSkipper returns a HTTPMiddlewareConfig.Skipper which skips all but the given uris
func RequestURIWhitelistSkipper(uris ...string) Skipper {
urisMap := make(map[string]bool, len(uris))
for _, uri := range uris {
urisMap[uri] = true
}
return func(r *http.Request) bool {
if _, ok := urisMap[r.RequestURI]; ok {
return true
}
return false
}
}
// RequestURIBlacklistSkipper returns a HTTPMiddlewareConfig.Skipper which skips the given uris
func RequestURIBlacklistSkipper(uris ...string) Skipper {
urisMap := make(map[string]bool, len(uris))
for _, uri := range uris {
urisMap[uri] = true
}
return func(r *http.Request) bool {
if _, ok := urisMap[r.RequestURI]; ok {
return false
}
return true
}
}