-
Notifications
You must be signed in to change notification settings - Fork 9
/
handler.go
30 lines (25 loc) · 1.1 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package luddite
import "net/http"
// Handler is an interface that objects can implement to be registered to serve
// as middleware in a service's middleware stack. HandleHTTP should yield to the
// next middleware in the chain by invoking the next http.HandlerFunc passed in.
//
// If the Handler writes to the ResponseWriter, the next http.HandlerFunc should
// not be invoked.
type Handler interface {
HandleHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)
}
// HandlerFunc is an adapter to allow the use of ordinary functions as
// middleware handlers. If f is a function with the appropriate signature,
// HandlerFunc(f) is a Handler object that calls f.
type HandlerFunc func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)
func (h HandlerFunc) HandleHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
h(rw, r, next)
}
// WrapHttpHandler converts an http.Handler into a Handler.
func WrapHttpHandler(h http.Handler) Handler {
return HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
h.ServeHTTP(rw, r)
next(rw, r)
})
}