-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlerfunc.go
124 lines (118 loc) · 3.66 KB
/
handlerfunc.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2015 Afshin Darian. All rights reserved.
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.
package bear
import (
"fmt"
"net/http"
)
// HandlerFunc is similar to http.HandlerFunc, except it requires
// an extra argument for the *Context of a request.
type HandlerFunc func(http.ResponseWriter, *http.Request, *Context)
// handlerize takes one of handler formats that Mux accepts.
// It returns a HandlerFunc, a flag indicating whether the HandlerFunc
// can be follwed by other handlers, and any error that may have arisen in
// conversion.
func handlerize(function interface{}) (HandlerFunc, bool, error) {
followable := true
unfollowable := false
switch function.(type) {
case HandlerFunc:
handler := function.(HandlerFunc)
if handler == nil {
return nil, unfollowable, fmt.Errorf("nil middleware")
} else {
return HandlerFunc(handler), followable, nil
}
case func(*Context):
handler := function.(func(*Context))
if handler == nil {
return nil, unfollowable, fmt.Errorf("nil middleware")
} else {
handler := HandlerFunc(
func(_ http.ResponseWriter, _ *http.Request, ctx *Context) {
handler(ctx)
})
return handler, followable, nil
}
case func(http.ResponseWriter, *http.Request, *Context):
handler := function.(func(http.ResponseWriter, *http.Request, *Context))
if handler == nil {
return nil, unfollowable, fmt.Errorf("nil middleware")
} else {
return HandlerFunc(handler), followable, nil
}
case http.HandlerFunc:
handler := function.(http.HandlerFunc)
if handler == nil {
return nil, unfollowable, fmt.Errorf("nil middleware")
} else {
handler := HandlerFunc(
func(res http.ResponseWriter, req *http.Request, _ *Context) {
handler(res, req)
})
return handler, unfollowable, nil
}
case func(http.ResponseWriter, *http.Request):
handler := function.(func(http.ResponseWriter, *http.Request))
if handler == nil {
return nil, unfollowable, fmt.Errorf("nil middleware")
} else {
handler := HandlerFunc(
func(res http.ResponseWriter, req *http.Request, _ *Context) {
handler(res, req)
})
return handler, unfollowable, nil
}
default:
err := fmt.Errorf(
"handler must match: %s, %s, or %s",
"http.HandlerFunc", "bear.HandlerFunc", "func(*Context)")
return nil, unfollowable, err
}
}
func handlerizeLax(
verb string, pattern string, functions []interface{}) ([]HandlerFunc, error) {
var handlers []HandlerFunc
unreachable := false
for _, function := range functions {
if unreachable {
err := fmt.Errorf("bear: %s %s has unreachable middleware", verb, pattern)
return nil, err
}
if handler, followable, err := handlerize(function); err != nil {
return nil, fmt.Errorf("bear: %s %s: %s", verb, pattern, err)
} else {
if !followable {
unreachable = true
}
handlers = append(handlers, handler)
}
}
return handlers, nil
}
func handlerizeStrict(functions []interface{}) ([]HandlerFunc, error) {
var handlers []HandlerFunc
for _, function := range functions {
switch function.(type) {
case HandlerFunc:
handler := function.(HandlerFunc)
if handler == nil {
return nil, fmt.Errorf("bear: nil middleware")
} else {
handlers = append(handlers, HandlerFunc(handler))
}
case func(http.ResponseWriter, *http.Request, *Context):
handler := function.(func(http.ResponseWriter, *http.Request, *Context))
if handler == nil {
return nil, fmt.Errorf("bear: nil middleware")
} else {
handlers = append(handlers, HandlerFunc(handler))
}
default:
return nil, fmt.Errorf(
"bear: handler must be a bear.HandlerFunc or match its signature")
}
}
return handlers, nil
}