github.com/xmidt-org/webpa-common@v1.11.9/xhttp/xfilter/filter.go (about) 1 package xfilter 2 3 import ( 4 "net/http" 5 ) 6 7 // Interface is essentially a predicate that determines whether a request is allowed. 8 type Interface interface { 9 // Allow tests whether the given request is allowed to execute. This method can return 10 // errors that implement the go-kit interfaces, e.g. StatusCoder. 11 Allow(*http.Request) error 12 } 13 14 // Func is the function equivalent of Interface 15 type Func func(*http.Request) error 16 17 func (f Func) Allow(r *http.Request) error { 18 return f(r) 19 } 20 21 var allow = Func(func(*http.Request) error { 22 return nil 23 }) 24 25 // Allow returns an xfilter that always returns a nil error 26 func Allow() Interface { 27 return allow 28 } 29 30 // Reject returns an xfilter that always returns the given error. 31 // If err == nil, this function is equivalent to Allow. 32 func Reject(err error) Interface { 33 if err == nil { 34 return Allow() 35 } 36 37 return Func(func(*http.Request) error { return err }) 38 }