-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathroute.go
More file actions
235 lines (210 loc) · 5.05 KB
/
Copy pathroute.go
File metadata and controls
235 lines (210 loc) · 5.05 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
226
227
228
229
230
231
232
233
234
235
package xmux
import (
"net/http"
"github.com/hyahm/xmux/helper"
)
// 初始化临时使用, 最后会合并到 router
type Route struct {
// 组里面也包括路由 后面的其实还是patter和handle, 还没到handle, 这里的key是个method
new bool
handle http.Handler // handle
module *module // 增加的 modules
delmodule map[string]struct{} // 删除的modules
// url string // 路由的path
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
}
func (rt *Route) Prefix(prefix string) *Route {
if !rt.new {
panic("can not support init")
}
rt.prefixs = append(rt.prefixs, prefix)
return rt
}
func (rt *Route) DelPrefix(prefixs ...string) *Route {
if !rt.new {
panic("can not support init")
}
for _, prefix := range prefixs {
rt.delprefix[prefix] = struct{}{}
}
return rt
}
func (rt *Route) GetHeader() map[string]string {
if !rt.new {
panic("can not support init")
}
return rt.header
}
// 这个路由的注释, 使用swagger加上这个字段才能显示执行的窗口
func (rt *Route) SwaggerSummary(summary string) *Route {
if !rt.new {
panic("can not support init")
}
rt.summary = summary
return rt
}
func (rt *Route) BindResponse(response interface{}) *Route {
if !rt.new {
panic("can not support init")
}
rt.responseData = response
rt.bindResponseData = true
return rt
}
func (rt *Route) AddPageKeys(pagekeys ...string) *Route {
if !rt.new {
panic("can not support init")
}
// 退出文档的组
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.new {
panic("can not support init")
}
if rt.delPageKeys == nil {
rt.delPageKeys = make(map[string]struct{})
}
for _, key := range pagekeys {
rt.delPageKeys[key] = struct{}{}
}
return rt
}
// 数据绑定
func (rt *Route) Bind(s interface{}) *Route {
if !rt.new {
panic("can not support init")
}
rt.dataSource = s
return rt
}
// json数据绑定
func (rt *Route) BindJson(s interface{}) *Route {
if !rt.new {
panic("can not support init")
}
// 接口补充说明
rt.dataSource = s
rt.bindType = jsonT
return rt
}
func (rt *Route) BindByContentType(s interface{}) *Route {
if !rt.new {
panic("can not support init")
}
// 接口补充说明
rt.dataSource = s
rt.bindType = headT
return rt
}
func (rt *Route) BindForm(s interface{}) *Route {
if !rt.new {
panic("can not support init")
}
// 接口补充说明
rt.dataSource = s
rt.bindType = formT
return rt
}
// yaml数据绑定
func (rt *Route) BindYaml(s interface{}) *Route {
if !rt.new {
panic("can not support init")
}
// 接口补充说明
rt.dataSource = s
rt.bindType = yamlT
return rt
}
// xml数据绑定
func (rt *Route) BindXml(s interface{}) *Route {
if !rt.new {
panic("can not support init")
}
// 接口补充说明
rt.dataSource = s
rt.bindType = xmlT
return rt
}
// 组里面也包括路由 后面的其实还是patter和handle
func (rt *Route) SetHeader(k, v string) *Route {
if !rt.new {
panic("can not support init")
}
if rt.header == nil {
rt.header = map[string]string{}
}
rt.header[k] = v
return rt
}
func (rt *Route) DelHeader(dh ...string) *Route {
if !rt.new {
panic("can not support init")
}
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.new {
panic("can not support init")
}
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) DelModule(handles ...func(http.ResponseWriter, *http.Request) bool) *Route {
if !rt.new {
panic("can not support init")
}
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) SwaggerAddParameter(pt Parameter) *Route {
if !rt.new {
panic("can not support init")
}
rt.query = append(rt.query, pt)
// if rt.delmodule == nil {
// rt.delmodule = make(map[string]struct{})
// }
// for _, handle := range handles {
// rt.delmodule[GetFuncName(handle)] = struct{}{}
// }
// rt.module.delete(rt.delmodule)
return rt
}