-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware.go
35 lines (29 loc) · 952 Bytes
/
middleware.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
31
32
33
34
35
package route
import "net/http"
type (
// MiddlewareFunc defines a function to process middleware.
MiddlewareFunc func(c Context, next HandlerFunc) error
// Skipper defines a function to skip middleware. Returning true skips processing
// the middleware.
Skipper func(Context) bool
)
// WrapMiddleware wraps `func(http.Handler) http.Handler` into `mux.MiddlewareFunc`
func WrapMiddleware(m func(http.Handler) http.Handler) MiddlewareFunc {
return func(c Context, next HandlerFunc) (err error) {
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.SetRequest(r)
err = next(c)
})).ServeHTTP(c.Response(), c.Request())
return
}
}
// DefaultSkipper returns false which processes the middleware.
func DefaultSkipper(Context) bool {
return false
}
// compose chains given handler with next middleware.
func compose(h HandlerFunc, m MiddlewareFunc) HandlerFunc {
return func(c Context) error {
return m(c, h)
}
}