-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapters.go
53 lines (44 loc) · 1.46 KB
/
adapters.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package mohttp
import (
"golang.org/x/net/context"
"net/http"
)
// FromHTTPMiddleware transforms a golang stdlib middleware of the form
// func(http.Handler) Handler
// into a mohttp.Handler
func FromHTTPMiddleware(fn func(http.Handler) http.Handler) Handler {
return HandlerFunc(func(c context.Context) {
h := fn(mkNextHTTPHandler(c))
h.ServeHTTP(GetResponseWriter(c), GetRequest(c))
})
}
// FromHTTPHandler transforms a golang stdlib http.Handler
// into a mohttp.Handler
func FromHTTPHandler(handler http.Handler) Handler {
return HandlerFunc(func(c context.Context) {
handler.ServeHTTP(GetResponseWriter(c), GetRequest(c))
Next(c)
})
}
type NegronHandler interface {
ServeHTTP(http.ResponseWriter, *http.Request, http.HandlerFunc)
}
type NegroniHandlerFunc func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)
func (fn NegroniHandlerFunc) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
fn(rw, r, next)
}
func FromNegroniHandler(n NegronHandler) Handler {
return HandlerFunc(func(c context.Context) {
n.ServeHTTP(GetResponseWriter(c), GetRequest(c), mkNextHTTPHandler(c))
})
}
func FromNegroniHandlerFunc(fn func(http.ResponseWriter, *http.Request, http.HandlerFunc)) Handler {
return FromNegroniHandler(NegroniHandlerFunc(fn))
}
func mkNextHTTPHandler(c context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
c = WithRequest(c, r)
c = WithResponseWriter(c, w)
Next(c)
}
}