forked from uptrace/bunrouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
244 lines (191 loc) · 6.26 KB
/
group.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
package bunrouter
import (
"fmt"
"net/http"
)
// Group is a group of routes and middlewares.
type Group struct {
router *Router
path string
stack []MiddlewareFunc
}
// NewGroup adds a sub-group to this group.
func (g *Group) NewGroup(path string, opts ...GroupOption) *Group {
group := &Group{
router: g.router,
path: joinPath(g.path, path),
stack: g.cloneStack(),
}
cfg := &config{
group: group,
}
for _, opt := range opts {
opt.apply(cfg)
}
return group
}
func (g *Group) cloneStack() []MiddlewareFunc {
return g.stack[:len(g.stack):len(g.stack)]
}
func (g *Group) Use(middleware MiddlewareFunc) *Group {
return g.NewGroup("", Use(middleware))
}
func (g *Group) WithMiddleware(middleware MiddlewareFunc) *Group {
return g.NewGroup("", WithMiddleware(middleware))
}
func (g *Group) WithGroup(path string, fn func(g *Group)) {
fn(g.NewGroup(path))
}
func (g *Group) Handle(meth string, path string, handler HandlerFunc) {
g.router.mu.Lock()
defer g.router.mu.Unlock()
checkPath(path)
path = g.path + path
if path == "" {
panic("path can't be empty")
}
if len(g.stack) > 0 {
handler = g.handlerWithMiddlewares(handler)
}
node := g.router.tree.addRoute(path)
node.setHandler(meth, handler)
if node.handlerMap.notAllowed == nil {
node.handlerMap.notAllowed = g.handlerWithMiddlewares(g.router.methodNotAllowedHandler)
}
}
// Syntactic sugar for Handle("GET", path, handler)
func (g *Group) GET(path string, handler HandlerFunc) {
g.Handle("GET", path, handler)
}
// Syntactic sugar for Handle("POST", path, handler)
func (g *Group) POST(path string, handler HandlerFunc) {
g.Handle("POST", path, handler)
}
// Syntactic sugar for Handle("PUT", path, handler)
func (g *Group) PUT(path string, handler HandlerFunc) {
g.Handle("PUT", path, handler)
}
// Syntactic sugar for Handle("DELETE", path, handler)
func (g *Group) DELETE(path string, handler HandlerFunc) {
g.Handle("DELETE", path, handler)
}
// Syntactic sugar for Handle("PATCH", path, handler)
func (g *Group) PATCH(path string, handler HandlerFunc) {
g.Handle("PATCH", path, handler)
}
// Syntactic sugar for Handle("HEAD", path, handler)
func (g *Group) HEAD(path string, handler HandlerFunc) {
g.Handle("HEAD", path, handler)
}
// Syntactic sugar for Handle("OPTIONS", path, handler)
func (g *Group) OPTIONS(path string, handler HandlerFunc) {
g.Handle("OPTIONS", path, handler)
}
func (g *Group) handlerWithMiddlewares(handler HandlerFunc) HandlerFunc {
for i := len(g.stack) - 1; i >= 0; i-- {
handler = g.stack[i](handler)
}
return handler
}
func (g *Group) Compat() *CompatGroup {
return &CompatGroup{group: g}
}
func (g *Group) Verbose() *VerboseGroup {
return &VerboseGroup{group: g}
}
//------------------------------------------------------------------------------
// CompatGroup is like Group, but it works with http.HandlerFunc instead of bunrouter handler.
type CompatGroup struct {
group *Group
}
func (g CompatGroup) NewGroup(path string, opts ...GroupOption) *CompatGroup {
return &CompatGroup{group: g.group.NewGroup(path, opts...)}
}
func (g CompatGroup) WithMiddleware(middleware MiddlewareFunc) *CompatGroup {
return &CompatGroup{group: g.group.WithMiddleware(middleware)}
}
func (g CompatGroup) WithGroup(path string, fn func(g *CompatGroup)) {
fn(g.NewGroup(path))
}
func (g CompatGroup) Handle(method string, path string, handler http.HandlerFunc) {
g.group.Handle(method, path, HTTPHandlerFunc(handler))
}
func (g CompatGroup) GET(path string, handler http.HandlerFunc) {
g.Handle(http.MethodGet, path, handler)
}
func (g CompatGroup) POST(path string, handler http.HandlerFunc) {
g.Handle("POST", path, handler)
}
func (g CompatGroup) PUT(path string, handler http.HandlerFunc) {
g.Handle("PUT", path, handler)
}
func (g CompatGroup) DELETE(path string, handler http.HandlerFunc) {
g.Handle("DELETE", path, handler)
}
func (g CompatGroup) PATCH(path string, handler http.HandlerFunc) {
g.Handle("PATCH", path, handler)
}
func (g CompatGroup) HEAD(path string, handler http.HandlerFunc) {
g.Handle("HEAD", path, handler)
}
func (g CompatGroup) OPTIONS(path string, handler http.HandlerFunc) {
g.Handle("OPTIONS", path, handler)
}
//------------------------------------------------------------------------------
type VerboseHandlerFunc func(w http.ResponseWriter, req *http.Request, ps Params)
// VerboseGroup is like Group, but it works with VerboseHandlerFunc instead of bunrouter handler.
type VerboseGroup struct {
group *Group
}
func (g VerboseGroup) NewGroup(path string, opts ...GroupOption) *VerboseGroup {
return &VerboseGroup{group: g.group.NewGroup(path, opts...)}
}
func (g VerboseGroup) WithMiddleware(middleware MiddlewareFunc) *VerboseGroup {
return &VerboseGroup{group: g.group.WithMiddleware(middleware)}
}
func (g VerboseGroup) WithGroup(path string, fn func(g *VerboseGroup)) {
fn(g.NewGroup(path))
}
func (g VerboseGroup) Handle(method string, path string, handler VerboseHandlerFunc) {
g.group.Handle(method, path, func(w http.ResponseWriter, req Request) error {
handler(w, req.Request, req.Params())
return nil
})
}
func (g VerboseGroup) GET(path string, handler VerboseHandlerFunc) {
g.Handle(http.MethodGet, path, handler)
}
func (g VerboseGroup) POST(path string, handler VerboseHandlerFunc) {
g.Handle("POST", path, handler)
}
func (g VerboseGroup) PUT(path string, handler VerboseHandlerFunc) {
g.Handle("PUT", path, handler)
}
func (g VerboseGroup) DELETE(path string, handler VerboseHandlerFunc) {
g.Handle("DELETE", path, handler)
}
func (g VerboseGroup) PATCH(path string, handler VerboseHandlerFunc) {
g.Handle("PATCH", path, handler)
}
func (g VerboseGroup) HEAD(path string, handler VerboseHandlerFunc) {
g.Handle("HEAD", path, handler)
}
func (g VerboseGroup) OPTIONS(path string, handler VerboseHandlerFunc) {
g.Handle("OPTIONS", path, handler)
}
//------------------------------------------------------------------------------
func joinPath(base, path string) string {
checkPath(path)
path = base + path
// Don't want trailing slash as all sub-paths start with slash
if len(path) > 1 && path[len(path)-1] == '/' {
path = path[:len(path)-1]
}
return path
}
func checkPath(path string) {
// All non-empty paths must start with a slash
if len(path) > 0 && path[0] != '/' {
panic(fmt.Sprintf("path %s must start with a slash", path))
}
}