-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
69 lines (54 loc) · 1.08 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
package router
type Group struct {
prefix string
routes []*Route
router *Router
middleware []Middleware
}
func (g *Group) calculateRouteRegexs() {
for _, r := range g.routes {
r.regex = r.calculateRouteRegex()
}
}
func newGroup(router *Router, routes ...*Route) *Group {
g := &Group{router: router}
for _, r := range routes {
g.Add(r)
g.calculateRouteRegexs()
}
g.routes = routes
return g
}
func (g *Group) Prefix(path string) *Group {
g.prefix = path
g.calculateRouteRegexs()
return g
}
func (g *Group) Middleware(middleware ...Middleware) *Group {
g.middleware = middleware
return g
}
func (g *Group) Add(routes ...*Route) *Group {
for _, r := range routes {
i, found := g.findExistingRoute(r)
if found {
g.routes[i] = r
} else {
g.routes = append(g.routes, r)
}
r.group = g
r.buildHandler()
}
return g
}
func (g *Group) findExistingRoute(route *Route) (int, bool) {
for i, r := range g.routes {
if r.path == route.path && methodsMatch(r, route) {
return i, true
}
}
return -1, false
}
func (g *Group) Routes() []*Route {
return g.routes
}