-
Notifications
You must be signed in to change notification settings - Fork 10
/
util.go
155 lines (128 loc) · 3.57 KB
/
util.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
package twig
// 组装器
type Assembler interface {
Register
PluginHelper
}
type target struct {
Register
PluginHelper
}
func newTarget(r Register, twig *Twig) Assembler {
return &target{
Register: r,
PluginHelper: twig,
}
}
// Mouter 接口用于模块化设置路由
type Mounter interface {
Mount(Assembler)
}
type MountFunc func(Assembler)
func (m MountFunc) Mount(target Assembler) {
m(target)
}
// Conf Twig路由配置工具
type Conf struct {
target Assembler
}
func config(r Register, twig *Twig) *Conf {
return &Conf{
target: newTarget(r, twig),
}
}
// Config 配置工具,对Register或者Assembler进行配置
// 参数为Register,需要注意的是:Config函数最低要求是Register
// 如果传入的是Register,那么调用插件方法时,会发生panic
// 这个不是bug, Config的设计如此
// 如果传入的是Assembler,那么可以进行plugin操作
func Config(r Register) *Conf {
if a, ok := r.(Assembler); ok {
return &Conf{
target: a,
}
}
return &Conf{
target: newTarget(r, nil),
}
}
// Use 当前Register增加中间件
func (c *Conf) Use(m ...MiddlewareFunc) *Conf {
c.Use(m...)
return c
}
// AddHandler 增加Handler
func (c *Conf) AddHandler(method, path string, handler HandlerFunc, m ...MiddlewareFunc) *Conf {
c.target.AddHandler(method, path, handler, m...)
return c
}
func (c *Conf) Get(path string, handler HandlerFunc, m ...MiddlewareFunc) *Conf {
return c.AddHandler(GET, path, handler, m...)
}
func (c *Conf) Post(path string, handler HandlerFunc, m ...MiddlewareFunc) *Conf {
return c.AddHandler(POST, path, handler, m...)
}
func (c *Conf) Delete(path string, handler HandlerFunc, m ...MiddlewareFunc) *Conf {
return c.AddHandler(DELETE, path, handler, m...)
}
func (c *Conf) Put(path string, handler HandlerFunc, m ...MiddlewareFunc) *Conf {
return c.AddHandler(PUT, path, handler, m...)
}
func (c *Conf) Patch(path string, handler HandlerFunc, m ...MiddlewareFunc) *Conf {
return c.AddHandler(PATCH, path, handler, m...)
}
func (c *Conf) Head(path string, handler HandlerFunc, m ...MiddlewareFunc) *Conf {
return c.AddHandler(HEAD, path, handler, m...)
}
func (c *Conf) Options(path string, handler HandlerFunc, m ...MiddlewareFunc) *Conf {
return c.AddHandler(OPTIONS, path, handler, m...)
}
func (c *Conf) Trace(path string, handler HandlerFunc, m ...MiddlewareFunc) *Conf {
return c.AddHandler(TRACE, path, handler, m...)
}
// Mount 挂载Mounter到当前Assembler
func (c *Conf) Mount(mount Mounter) *Conf {
mount.Mount(c.target)
return c
}
// Static 增加静态路由
// path为web路径,file为文件系统的filepath
// 如果filepath为目录,则默认index.html
// 通常path 应该以*结尾
// 而filepath 应该从根目录开始
func (c *Conf) Static(path, filepath string, m ...MiddlewareFunc) *Conf {
return c.Get(path, Static(filepath), m...)
}
// Group 配置路由组
func (c *Conf) Group(path string, mf MountFunc) *Conf {
mf(newGroup(c.target, path))
return c
}
/*
web.Conf().
Group("/api", func(r twig.Assembler) {
twig.Config(r).
Post("/addUser", func(c twig.Ctx) error {
...
})
})
*/
// Group 提供路由分组支持
type group struct {
prefix string
m []MiddlewareFunc
Assembler
}
func newGroup(assembler Assembler, prefix string) *group {
return &group{
prefix: prefix,
Assembler: assembler,
}
}
func (g *group) Use(mid ...MiddlewareFunc) {
g.m = append(g.m, mid...)
}
func (g *group) AddHandler(method, path string, h HandlerFunc, m ...MiddlewareFunc) {
handler := Merge(h, g.m)
g.Assembler.AddHandler(method, g.prefix+path, handler, m...)
}