forked from guregu/kami
/
handler_17.go
61 lines (52 loc) · 1.95 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// +build go1.7
package kami
import (
"context"
"fmt"
"net/http"
netcontext "golang.org/x/net/context"
)
// HandlerType is the type of Handlers and types that kami internally converts to
// ContextHandler. In order to provide an expressive API, this type is an alias for
// interface{} that is named for the purposes of documentation, however only the
// following concrete types are accepted:
// - types that implement http.Handler
// - types that implement ContextHandler
// - func(http.ResponseWriter, *http.Request)
// - func(context.Context, http.ResponseWriter, *http.Request)
type HandlerType interface{}
// ContextHandler is like http.Handler but supports context.
type ContextHandler interface {
ServeHTTPContext(context.Context, http.ResponseWriter, *http.Request)
}
// OldContextHandler is like ContextHandler but uses the old x/net/context.
type OldContextHandler interface {
ServeHTTPContext(netcontext.Context, http.ResponseWriter, *http.Request)
}
// HandlerFunc is like http.HandlerFunc with context.
type HandlerFunc func(context.Context, http.ResponseWriter, *http.Request)
func (h HandlerFunc) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request) {
h(ctx, w, r)
}
// 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))
}