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
|
/********************************
*** Multiplexer for Go ***
*** Bone is under MIT license ***
*** Code by CodingFerret ***
*** github.com/go-zoo ***
*********************************/
package bone
import "net/http"
// Router is the same as a http.Handler
type Router interface {
ServeHTTP(http.ResponseWriter, *http.Request)
}
// Register the route in the router
func (m *Mux) Register(method string, path string, handler http.Handler) *Route {
return m.register(method, path, handler)
}
// GetFunc add a new route to the Mux with the Get method
func (m *Mux) GetFunc(path string, handler http.HandlerFunc) *Route {
return m.register("GET", path, handler)
}
// PostFunc add a new route to the Mux with the Post method
func (m *Mux) PostFunc(path string, handler http.HandlerFunc) *Route {
return m.register("POST", path, handler)
}
// PutFunc add a new route to the Mux with the Put method
func (m *Mux) PutFunc(path string, handler http.HandlerFunc) *Route {
return m.register("PUT", path, handler)
}
// DeleteFunc add a new route to the Mux with the Delete method
func (m *Mux) DeleteFunc(path string, handler http.HandlerFunc) *Route {
return m.register("DELETE", path, handler)
}
// HeadFunc add a new route to the Mux with the Head method
func (m *Mux) HeadFunc(path string, handler http.HandlerFunc) *Route {
return m.register("HEAD", path, handler)
}
// PatchFunc add a new route to the Mux with the Patch method
func (m *Mux) PatchFunc(path string, handler http.HandlerFunc) *Route {
return m.register("PATCH", path, handler)
}
// OptionsFunc add a new route to the Mux with the Options method
func (m *Mux) OptionsFunc(path string, handler http.HandlerFunc) *Route {
return m.register("OPTIONS", path, handler)
}
// NotFoundFunc the mux custom 404 handler
func (m *Mux) NotFoundFunc(handler http.HandlerFunc) {
m.notFound = handler
}
// Handle add a new route to the Mux without a HTTP method
func (m *Mux) Handle(path string, handler http.Handler) {
for _, mt := range method {
m.register(mt, path, handler)
}
}
// HandleFunc is use to pass a func(http.ResponseWriter, *Http.Request) instead of http.Handler
func (m *Mux) HandleFunc(path string, handler http.HandlerFunc) {
m.Handle(path, handler)
}
// Get add a new route to the Mux with the Get method
func (m *Mux) Get(path string, handler http.Handler) *Route {
return m.register("GET", path, handler)
}
// Post add a new route to the Mux with the Post method
func (m *Mux) Post(path string, handler http.Handler) *Route {
return m.register("POST", path, handler)
}
// Put add a new route to the Mux with the Put method
func (m *Mux) Put(path string, handler http.Handler) *Route {
return m.register("PUT", path, handler)
}
// Delete add a new route to the Mux with the Delete method
func (m *Mux) Delete(path string, handler http.Handler) *Route {
return m.register("DELETE", path, handler)
}
// Head add a new route to the Mux with the Head method
func (m *Mux) Head(path string, handler http.Handler) *Route {
return m.register("HEAD", path, handler)
}
// Patch add a new route to the Mux with the Patch method
func (m *Mux) Patch(path string, handler http.Handler) *Route {
return m.register("PATCH", path, handler)
}
// Options add a new route to the Mux with the Options method
func (m *Mux) Options(path string, handler http.Handler) *Route {
return m.register("OPTIONS", path, handler)
}
// NotFound the mux custom 404 handler
func (m *Mux) NotFound(handler http.Handler) {
m.notFound = handler
}
// Register the new route in the router with the provided method and handler
func (m *Mux) register(method string, path string, handler http.Handler) *Route {
r := NewRoute(m.prefix+path, handler)
r.Method = method
if valid(path) {
m.Routes[method] = append(m.Routes[method], r)
return r
}
m.Routes[static] = append(m.Routes[static], r)
return r
}
// SubRoute register a router as a SubRouter of bone
func (m *Mux) SubRoute(path string, router Router) *Route {
r := NewRoute(m.prefix+path, router)
if valid(path) {
r.Atts += SUB
for _, mt := range method {
m.Routes[mt] = append(m.Routes[mt], r)
}
return r
}
return nil
}
|