-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathroute.go
More file actions
225 lines (193 loc) · 5.22 KB
/
Copy pathroute.go
File metadata and controls
225 lines (193 loc) · 5.22 KB
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
package xmux
import (
"net/http"
"regexp"
"github.com/hyahm/xmux/helper"
)
// 初始化临时使用, 最后会合并到 router
type route struct {
// 组里面也包括路由 后面的其实还是patter和handle, 还没到handle, 这里的key是个method
handle http.Handler // handle
module *module // 增加的 modules
postModule *module // 增加的 modules
delmodule map[string]struct{} // 删除的modules
delPostModule map[string]struct{} // 删除的modules
params []string // path正则名
pagekeys map[string]struct{} // 页面权限
delPageKeys map[string]struct{} // 删除的权限
header map[string]string // 请求头
delheader map[string]struct{} // 删除的请求头
methods []string // 删除的请求头
prefixs []string
delprefix map[string]struct{}
responseData interface{} // 接口返回实例
bindResponseData bool
summary string
bindType bindType // 数据绑定格式
dataSource interface{} // 数据源
query []Parameter
denyPrefix bool
middleware onion
regex *regexp.Regexp // 预编译的正则表达式
menuTree *MenuTree
url string
ignoreMenu bool
}
func (rt *route) Use(m ...Middleware) {
rt.middleware.Use(m...)
}
func (rt *route) SetMeta(mt Meta) *route {
rt.menuTree.Meta = mt
return rt
}
func (rt *route) Prefix(prefix string) *route {
rt.prefixs = append(rt.prefixs, prefix)
return rt
}
func (rt *route) DelPrefix(prefixs ...string) *route {
for _, prefix := range prefixs {
rt.delprefix[prefix] = struct{}{}
}
return rt
}
func (rt *route) GetHeader() map[string]string {
return rt.header
}
func (rt *route) DenyPrefix() {
rt.denyPrefix = true
}
// 这个路由的注释, 使用swagger加上这个字段才能显示执行的窗口
func (rt *route) SwaggerSummary(summary string) *route {
rt.summary = summary
return rt
}
func (rt *route) BindResponse(response interface{}) *route {
rt.bindResponseData = true
rt.responseData = response
// if response == nil {
// rt.bindResponseData = false
// return rt
// }
return rt
}
func (rt *route) AddPageKeys(pagekeys ...string) *route {
// 退出文档的组
for _, v := range pagekeys {
if rt.pagekeys == nil {
rt.pagekeys = make(map[string]struct{})
}
rt.pagekeys[v] = struct{}{}
}
return rt
}
func (rt *route) DelPageKeys(pagekeys ...string) *route {
if rt.delPageKeys == nil {
rt.delPageKeys = make(map[string]struct{})
}
for _, key := range pagekeys {
rt.delPageKeys[key] = struct{}{}
}
for _, v := range pagekeys {
delete(rt.pagekeys, v)
}
return rt
}
// 数据绑定
func (rt *route) Bind(s interface{}) *route {
rt.dataSource = s
return rt
}
// json数据绑定
func (rt *route) BindJson(s interface{}) *route {
// 接口补充说明
rt.dataSource = s
rt.bindType = jsonT
return rt
}
func (rt *route) BindByContentType(s interface{}) *route {
// 接口补充说明
rt.dataSource = s
rt.bindType = headT
return rt
}
func (rt *route) BindForm(s interface{}) *route {
// 接口补充说明
rt.dataSource = s
rt.bindType = formT
return rt
}
// yaml数据绑定
func (rt *route) BindYaml(s interface{}) *route {
// 接口补充说明
rt.dataSource = s
rt.bindType = yamlT
return rt
}
// xml数据绑定
func (rt *route) BindXml(s interface{}) *route {
// 接口补充说明
rt.dataSource = s
rt.bindType = xmlT
return rt
}
// 组里面也包括路由 后面的其实还是patter和handle
func (rt *route) SetHeader(k, v string) *route {
if rt.header == nil {
rt.header = map[string]string{}
}
rt.header[k] = v
return rt
}
func (rt *route) DelHeader(dh ...string) *route {
if rt.delheader == nil {
rt.delheader = make(map[string]struct{})
}
for _, v := range dh {
rt.delheader[v] = struct{}{}
}
return rt
}
func (rt *route) AddModule(handles ...func(http.ResponseWriter, *http.Request) bool) *route {
if rt.module == nil {
rt.module = &module{
filter: make(map[string]struct{}),
funcOrder: make([]func(w http.ResponseWriter, r *http.Request) bool, 0),
}
}
rt.module.add(handles...)
return rt
}
func (rt *route) AddPostModule(handles ...func(http.ResponseWriter, *http.Request) bool) *route {
if rt.postModule == nil {
rt.postModule = &module{
filter: make(map[string]struct{}),
funcOrder: make([]func(w http.ResponseWriter, r *http.Request) bool, 0),
}
}
rt.postModule.add(handles...)
return rt
}
func (rt *route) DelModule(handles ...func(http.ResponseWriter, *http.Request) bool) *route {
if rt.delmodule == nil {
rt.delmodule = make(map[string]struct{})
}
for _, handle := range handles {
rt.delmodule[helper.GetFuncName(handle)] = struct{}{}
}
rt.module.delete(rt.delmodule)
return rt
}
func (rt *route) DelPostModule(handles ...func(http.ResponseWriter, *http.Request) bool) *route {
if rt.delPostModule == nil {
rt.delPostModule = make(map[string]struct{})
}
for _, handle := range handles {
rt.delPostModule[helper.GetFuncName(handle)] = struct{}{}
}
rt.postModule.delete(rt.delPostModule)
return rt
}
func (rt *route) SwaggerAddParameter(pt Parameter) *route {
rt.query = append(rt.query, pt)
return rt
}