This repository has been archived by the owner on Oct 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgroup.go
307 lines (259 loc) · 9.12 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package treemux
import (
"fmt"
"net/http"
)
type MiddlewareFunc func(next HandlerFunc) HandlerFunc
type GroupOption interface {
Option
groupOption()
}
type groupOption func(cfg *config)
func (fn groupOption) apply(cfg *config) {
fn(cfg)
}
func (fn groupOption) groupOption() {}
// WithGroup calls the fn with the current Group.
func WithGroup(fn func(g *Group)) GroupOption {
return groupOption(func(c *config) {
fn(c.group)
})
}
// WithMiddleware adds a middleware handler to the Group's middleware stack.
func WithMiddleware(fn MiddlewareFunc) GroupOption {
return groupOption(func(c *config) {
c.group.stack = append(c.group.stack, fn)
})
}
// WithHandler is like WithMiddleware, but the handler can't modify the request.
func WithHandler(fn HandlerFunc) GroupOption {
return groupOption(func(c *config) {
middleware := func(next HandlerFunc) HandlerFunc {
return func(w http.ResponseWriter, req Request) error {
if err := fn(w, req); err != nil {
return err
}
return next(w, req)
}
}
c.group.stack = append(c.group.stack, middleware)
})
}
// Group is a group of routes and middlewares.
type Group struct {
mux *Router
path string
stack []MiddlewareFunc
}
// NewGroup adds a sub-group to this group.
func (g *Group) NewGroup(path string, opts ...GroupOption) *Group {
group := &Group{
mux: g.mux,
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) Compat() *CompatGroup {
return &CompatGroup{group: g}
}
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))
}
// Path elements starting with : indicate a wildcard in the path. A wildcard will only match on a
// single path segment. That is, the pattern `/post/:postid` will match on `/post/1` or `/post/1/`,
// but not `/post/1/2`.
//
// A path element starting with * is a catch-all, whose value will be a string containing all text
// in the URL matched by the wildcards. For example, with a pattern of `/images/*path` and a
// requested URL `images/abc/def`, path would contain `abc/def`.
//
// # Routing Rule Priority
//
// The priority rules in the router are simple.
//
// 1. Static path segments take the highest priority. If a segment and its subtree are able to match the URL, that match is returned.
//
// 2. Wildcards take second priority. For a particular wildcard to match, that wildcard and its subtree must match the URL.
//
// 3. Finally, a catch-all rule will match when the earlier path segments have matched, and none of the static or wildcard conditions have matched. Catch-all rules must be at the end of a pattern.
//
// So with the following patterns, we'll see certain matches:
// router = treemux.New()
// router.GET("/:page", pageHandler)
// router.GET("/:year/:month/:post", postHandler)
// router.GET("/:year/:month", archiveHandler)
// router.GET("/images/*path", staticHandler)
// router.GET("/favicon.ico", staticHandler)
//
// /abc will match /:page
// /2014/05 will match /:year/:month
// /2014/05/really-great-blog-post will match /:year/:month/:post
// /images/CoolImage.gif will match /images/*path
// /images/2014/05/MayImage.jpg will also match /images/*path, with all the text after /images stored in the variable path.
// /favicon.ico will match /favicon.ico
//
// # Trailing Slashes
//
// The router has special handling for paths with trailing slashes. If a pattern is added to the
// router with a trailing slash, any matches on that pattern without a trailing slash will be
// redirected to the version with the slash. If a pattern does not have a trailing slash, matches on
// that pattern with a trailing slash will be redirected to the version without.
//
// The trailing slash flag is only stored once for a pattern. That is, if a pattern is added for a
// method with a trailing slash, all other methods for that pattern will also be considered to have a
// trailing slash, regardless of whether or not it is specified for those methods too.
//
// This behavior can be turned off by setting TreeMux.RedirectTrailingSlash to false. By
// default it is set to true. The specifics of the redirect depend on RedirectBehavior.
//
// One exception to this rule is catch-all patterns. By default, trailing slash redirection is
// disabled on catch-all patterns, since the structure of the entire URL and the desired patterns
// can not be predicted. If trailing slash removal is desired on catch-all patterns, set
// TreeMux.RemoveCatchAllTrailingSlash to true.
//
// router = treemux.New()
// router.GET("/about", pageHandler)
// router.GET("/posts/", postIndexHandler)
// router.POST("/posts", postFormHandler)
//
// GET /about will match normally.
// GET /about/ will redirect to /about.
// GET /posts will redirect to /posts/.
// GET /posts/ will match normally.
// POST /posts will redirect to /posts/, because the GET method used a trailing slash.
func (g *Group) Handle(method string, path string, handler HandlerFunc) {
g.mux.mu.Lock()
defer g.mux.mu.Unlock()
if len(g.stack) > 0 {
handler = g.handlerWithMiddlewares(handler)
}
var addSlash bool
addOne := func(fullPath string) {
node := g.mux.root.addPath(fullPath[1:], nil, false)
if node.route == "" {
node.route = fullPath
} else if node.route != fullPath {
panic(fmt.Errorf("%q != %q", node.route, fullPath))
}
if addSlash {
node.addSlash = true
}
node.setHandler(method, handler, false)
if g.mux.headCanUseGet &&
method == http.MethodGet &&
node.handlerMap.Get(http.MethodHead) == nil {
node.setHandler(http.MethodHead, handler, true)
}
}
checkPath(path)
path = g.path + path
if path == "" {
panic("Cannot map an empty path")
}
if len(path) > 1 && path[len(path)-1] == '/' && g.mux.redirectTrailingSlash {
addSlash = true
path = path[:len(path)-1]
}
addOne(path)
}
// 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
}
//------------------------------------------------------------------------------
// CompatGroup is like Group, but it works with http.HandlerFunc instead of treemux 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, HTTPHandler(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)
}
//------------------------------------------------------------------------------
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) > 0 && 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 slash", path))
}
}