-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
67 lines (60 loc) · 1.54 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
package mserver
// Grouper 前缀分组
// 实现HttpMethod方法
type Grouper interface {
Post(path string, handler HandleFunc)
Put(path string, handler HandleFunc)
Get(path string, handler HandleFunc)
Delete(path string, handler HandleFunc)
Any(path string, handler HandleFunc)
// Group 实现嵌套
Group(string string) Grouper
}
var _ Grouper = &Group{}
type Group struct {
core *Core
prefix string // 这个group的通用前缀
parent *Group // 指向上一个ServerGroup
}
func NewGroup(core *Core, prefix string) *Group {
return &Group{
core: core,
parent: nil,
prefix: prefix,
}
}
func (g *Group) Post(path string, handler HandleFunc) {
path = g.getAbsolutePrefix() + path
g.core.Post(path, handler)
}
func (g *Group) Put(path string, handler HandleFunc) {
path = g.getAbsolutePrefix() + path
g.core.Put(path, handler)
}
func (g *Group) Get(path string, handler HandleFunc) {
path = g.getAbsolutePrefix() + path
g.core.Get(path, handler)
}
func (g *Group) Delete(path string, handler HandleFunc) {
path = g.getAbsolutePrefix() + path
g.core.Delete(path, handler)
}
func (g *Group) Any(path string, handler HandleFunc) {
path = g.getAbsolutePrefix() + path
g.core.Any(path, handler)
}
func (g *Group) Group(uri string) Grouper {
childGroup := NewGroup(g.core, uri)
childGroup.parent = g
return childGroup
}
// 获取当前group的绝对路径
func (g *Group) getAbsolutePrefix() string {
if g.parent == nil {
return g.prefix
}
return g.parent.getAbsolutePrefix() + g.prefix
}
func (g *Group) getCore() *Core {
return g.core
}