-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathhandler_17.go
39 lines (34 loc) · 1.05 KB
/
handler_17.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
// +build go1.7,!go1.9
package kami
import (
"context"
"fmt"
"net/http"
netcontext "golang.org/x/net/context"
)
// OldContextHandler is like ContextHandler but uses the old x/net/context.
type OldContextHandler interface {
ServeHTTPContext(netcontext.Context, http.ResponseWriter, *http.Request)
}
// wrap tries to turn a HandlerType into a ContextHandler
func wrap(h HandlerType) ContextHandler {
switch x := h.(type) {
case ContextHandler:
return x
case func(context.Context, http.ResponseWriter, *http.Request):
return HandlerFunc(x)
case func(netcontext.Context, http.ResponseWriter, *http.Request):
return HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
x(ctx, w, r)
})
case http.Handler:
return HandlerFunc(func(_ context.Context, w http.ResponseWriter, r *http.Request) {
x.ServeHTTP(w, r)
})
case func(http.ResponseWriter, *http.Request):
return HandlerFunc(func(_ context.Context, w http.ResponseWriter, r *http.Request) {
x(w, r)
})
}
panic(fmt.Errorf("unsupported HandlerType: %T", h))
}