-
Notifications
You must be signed in to change notification settings - Fork 1
/
method_handler.go
49 lines (44 loc) · 962 Bytes
/
method_handler.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
package muxter
import (
"net/http"
"strings"
)
type MethodHandler struct {
GET Handler
POST Handler
PUT Handler
PATCH Handler
HEAD Handler
DELETE Handler
MethodNotAllowedHandler Handler
}
func (mh MethodHandler) getHandler(method string) (handler Handler) {
defer func() {
if handler == nil {
if mh.MethodNotAllowedHandler == nil {
handler = defaultMethodNotAllowedHandler
} else {
handler = mh.MethodNotAllowedHandler
}
}
}()
switch strings.ToUpper(method) {
case "GET":
return mh.GET
case "POST":
return mh.POST
case "DELETE":
return mh.DELETE
case "PUT":
return mh.PUT
case "PATCH":
return mh.PATCH
case "HEAD":
return mh.HEAD
default:
return nil
}
}
func (mh MethodHandler) ServeHTTPx(w http.ResponseWriter, r *http.Request, c Context) {
mh.getHandler(r.Method).ServeHTTPx(w, r, c)
}