-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
handler_old.go
51 lines (43 loc) · 1.57 KB
/
handler_old.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
// +build !go1.7
package kami
import (
"fmt"
"net/http"
"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)
}
// 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 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))
}