-
Notifications
You must be signed in to change notification settings - Fork 0
/
methodr.go
255 lines (226 loc) · 5.73 KB
/
methodr.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Package methodr provides http.Handler compliant routing based on the request method.
package methodr
import (
"net/http"
)
// Routing table with handler for all methods
type Mux struct {
Get http.Handler
Head http.Handler
Post http.Handler
Put http.Handler
Delete http.Handler
Trace http.Handler
Options http.Handler
Connect http.Handler
Patch http.Handler
Default http.Handler // Default handler in case of miss
}
const (
methodGET = "GET"
methodHEAD = "HEAD"
methodPOST = "POST"
methodPUT = "PUT"
methodDELETE = "DELETE"
methodTRACE = "TRACE"
methodOPTIONS = "OPTIONS"
methodCONNECT = "CONNECT"
methodPATCH = "PATCH"
)
var (
// Changeable global default handler in case of miss on routing table. Default: DefaultHandlerMethodNotAllowed
DefaultHandler = DefaultHandlerMethodNotAllowed
// Default handler returning http.StatusMethodNotAllowed
DefaultHandlerMethodNotAllowed = http.HandlerFunc(defaultHandleMethodNotAllowed)
defaultHandlerStatusCode = http.StatusMethodNotAllowed // For tests only - consistent with DefaultHandler
)
func defaultHandleMethodNotAllowed(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed)
}
// ServeHTTP routes requests depending on method routing table.
// In case of a miss the custom DEFAULT handler is used, otherwise the global DefaultHandler.
// HEAD requests are delegated to GET if there's a GET handler available and no HEAD handler set.
func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case methodGET:
if m.Get != nil {
m.Get.ServeHTTP(w, r)
} else {
m.handleDefault(w, r)
}
case methodHEAD:
if m.Head != nil {
m.Head.ServeHTTP(w, r)
} else if m.Get != nil { // Special case: HEAD uses GET if GET available and HEAD not set.
m.Get.ServeHTTP(w, r)
} else {
m.handleDefault(w, r)
}
case methodPOST:
if m.Post != nil {
m.Post.ServeHTTP(w, r)
} else {
m.handleDefault(w, r)
}
case methodPUT:
if m.Put != nil {
m.Put.ServeHTTP(w, r)
} else {
m.handleDefault(w, r)
}
case methodDELETE:
if m.Delete != nil {
m.Delete.ServeHTTP(w, r)
} else {
m.handleDefault(w, r)
}
case methodTRACE:
if m.Trace != nil {
m.Trace.ServeHTTP(w, r)
} else {
m.handleDefault(w, r)
}
case methodOPTIONS:
if m.Options != nil {
m.Options.ServeHTTP(w, r)
} else {
m.handleDefault(w, r)
}
case methodCONNECT:
if m.Connect != nil {
m.Connect.ServeHTTP(w, r)
} else {
m.handleDefault(w, r)
}
case methodPATCH:
if m.Patch != nil {
m.Patch.ServeHTTP(w, r)
} else {
m.handleDefault(w, r)
}
default:
m.handleDefault(w, r)
}
}
// Use global default handler if there is no custom default handler HDEFAULT
func (m *Mux) handleDefault(w http.ResponseWriter, r *http.Request) {
if m.Default == nil {
DefaultHandler.ServeHTTP(w, r)
} else {
m.Default.ServeHTTP(w, r)
}
}
// DEFAULT sets default handler used in case of miss on routing table.
func DEFAULT(h http.Handler) *Mux {
return &Mux{
Default: h,
}
}
// DEFAULT sets default handler in case of miss on routing table
func (m *Mux) DEFAULT(h http.Handler) *Mux {
m.Default = h
return m
}
// GET sets the handler used for GET method requests.
// HEAD requests are delegated to GET if there's a GET handler available and no HEAD handler set.
func GET(h http.Handler) *Mux {
return &Mux{
Get: h,
}
}
// GET sets the handler used for GET method requests.
// HEAD requests are delegated to GET if there's a GET handler available and no HEAD handler set.
func (m *Mux) GET(h http.Handler) *Mux {
m.Get = h
return m
}
// HEAD sets the handler used for HEAD method requests.
// HEAD requests are delegated to GET if there's a GET handler available and no HEAD handler set.
func HEAD(h http.Handler) *Mux {
return &Mux{
Head: h,
}
}
// HEAD sets the handler used for HEAD method requests.
// HEAD requests are delegated to GET if there's a GET handler available and no HEAD handler set.
func (m *Mux) HEAD(h http.Handler) *Mux {
m.Head = h
return m
}
// POST sets the handler used for POST method requests.
func POST(h http.Handler) *Mux {
return &Mux{
Post: h,
}
}
// POST sets the handler used for POST method requests.
func (m *Mux) POST(h http.Handler) *Mux {
m.Post = h
return m
}
// PUT sets the handler used for PUT method requests.
func PUT(h http.Handler) *Mux {
return &Mux{
Put: h,
}
}
// PUT sets the handler used for PUT method requests.
func (m *Mux) PUT(h http.Handler) *Mux {
m.Put = h
return m
}
// DELETE sets the handler used for DELETE method requests.
func DELETE(h http.Handler) *Mux {
return &Mux{
Delete: h,
}
}
// DELETE sets the handler used for DELETE method requests.
func (m *Mux) DELETE(h http.Handler) *Mux {
m.Delete = h
return m
}
// TRACE sets the handler used for TRACE method requests.
func TRACE(h http.Handler) *Mux {
return &Mux{
Trace: h,
}
}
// TRACE sets the handler used for TRACE method requests.
func (m *Mux) TRACE(h http.Handler) *Mux {
m.Trace = h
return m
}
// OPTIONS sets the handler used for OPTIONS method requests.
func OPTIONS(h http.Handler) *Mux {
return &Mux{
Options: h,
}
}
// OPTIONS sets the handler used for OPTIONS method requests.
func (m *Mux) OPTIONS(h http.Handler) *Mux {
m.Options = h
return m
}
// CONNECT sets the handler used for CONNECT method requests.
func CONNECT(h http.Handler) *Mux {
return &Mux{
Connect: h,
}
}
// CONNECT sets the handler used for CONNECT method requests.
func (m *Mux) CONNECT(h http.Handler) *Mux {
m.Connect = h
return m
}
// PATCH sets the handler used for PATCH method requests.
func PATCH(h http.Handler) *Mux {
return &Mux{
Patch: h,
}
}
// PATCH sets the handler used for PATCH method requests.
func (m *Mux) PATCH(h http.Handler) *Mux {
m.Patch = h
return m
}