-
Notifications
You must be signed in to change notification settings - Fork 0
/
PathHandler.go
59 lines (49 loc) · 1.23 KB
/
PathHandler.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
package rizo
import (
"net/http"
"strings"
)
//PathHandler ...
type PathHandler struct {
handlers map[string]http.HandlerFunc
}
//NewPathHandler ...
func NewPathHandler() *PathHandler {
return &PathHandler{
handlers: map[string]http.HandlerFunc{},
}
}
//Get ...
func (instance *PathHandler) Get(handler http.HandlerFunc) {
instance.Method("GET", handler)
}
//Post ...
func (instance *PathHandler) Post(handler http.HandlerFunc) {
instance.Method("POST", handler)
}
//Put ...
func (instance *PathHandler) Put(handler http.HandlerFunc) {
instance.Method("PUT", handler)
}
//Delete ...
func (instance *PathHandler) Delete(handler http.HandlerFunc) {
instance.Method("DELETE", handler)
}
//Patch ...
func (instance *PathHandler) Patch(handler http.HandlerFunc) {
instance.Method("PATCH", handler)
}
//Method ...
func (instance *PathHandler) Method(method string, handler http.HandlerFunc) {
upperCaseMethod := strings.ToUpper(method)
instance.handlers[upperCaseMethod] = handler
}
//Handle ...
func (instance *PathHandler) Handle(w http.ResponseWriter, r *http.Request) {
method := strings.ToUpper(r.Method)
if handler, ok := instance.handlers[method]; ok {
handler(w, r)
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
}
}