-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
59 lines (50 loc) · 1.44 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
package cithttp
// Group router group, a Group is associated with a prefix and parent group
type Group struct {
engine *Engine
prefix string
parent *Group
middlewares []HandlerFunc // 存放中间件
}
func NewGroup(e *Engine, prefix string) *Group {
return &Group{
engine: e,
prefix: prefix,
parent: nil,
}
}
func (g *Group) getTotalPath() string {
if g.parent == nil {
return g.prefix
}
return g.parent.getTotalPath() + g.prefix
}
func (g *Group) POST(uri string, handlers ...HandlerFunc) {
uri = g.getTotalPath() + uri
allHandlers := append(g.getMiddlewares(), handlers...)
g.engine.POST(uri, allHandlers...)
}
func (g *Group) PUT(uri string, handlers ...HandlerFunc) {
uri = g.getTotalPath() + uri
allHandlers := append(g.getMiddlewares(), handlers...)
g.engine.PUT(uri, allHandlers...)
}
func (g *Group) DELETE(uri string, handlers ...HandlerFunc) {
uri = g.getTotalPath() + uri
allHandlers := append(g.getMiddlewares(), handlers...)
g.engine.DELETE(uri, allHandlers...)
}
func (g *Group) GET(uri string, handlers ...HandlerFunc) {
uri = g.getTotalPath() + uri
allHandlers := append(g.getMiddlewares(), handlers...)
g.engine.GET(uri, allHandlers...)
}
func (g *Group) getMiddlewares() []HandlerFunc {
if g.parent == nil {
return g.middlewares
}
return append(g.parent.getMiddlewares(), g.middlewares...)
}
func (g *Group) Use(middlewares ...HandlerFunc) {
g.middlewares = append(g.middlewares, middlewares...)
}