-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
31 lines (26 loc) · 1003 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
package lightwork
import (
"net/http"
)
type Middleware func(next Handler) (h Handler)
// ClassicHandlerShim converts a classic Go http.Handler into a batteryholder Handler.
func ClassicHandlerShim(classicHandler http.Handler) Handler {
return func(c *Context) error {
classicHandler.ServeHTTP(c.Response.rw, c.Request.req)
return nil
}
}
// ClassicMiddlewareShim is a helper function that allows you to use a classic Go middleware handler as middleware.
// If your middleware accepts more than one parameter, you'll have to curry the other parameters, as this only allows for simple middleware with a single http.Handler parameter.
func ClassicMiddlewareShim(classicMiddleware func(next http.Handler) http.Handler) Middleware {
return func(n Handler) Handler {
return func(c *Context) (err error) {
classicHandler := classicMiddleware(
http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
n(c)
}),
)
return ClassicHandlerShim(classicHandler)(c)
}
}
}