-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrouter.go
333 lines (269 loc) · 7.32 KB
/
router.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package yago
import (
"log"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
)
// http
type HttpHandlerFunc func(c *Ctx)
type HttpRouter struct {
Group *HttpGroupRouter
Path string
Method string
Actions []HttpHandlerFunc
Metadata interface{}
}
func (h *HttpRouter) WithMetadata(md interface{}) *HttpRouter {
h.Metadata = md
return h
}
func SetHttpNoRouter(action HttpHandlerFunc) {
httpNoRouterHandler = action
}
type HttpGlobalMiddleware []HttpHandlerFunc
func HttpUse(middleware ...HttpHandlerFunc) {
httpGlobalMiddleware.Use(middleware...)
}
func (r *HttpGlobalMiddleware) Use(middleware ...HttpHandlerFunc) {
*r = append(*r, middleware...)
}
var (
httpGlobalMiddleware HttpGlobalMiddleware
httpGroupRouterMap = make(map[string]*HttpGroupRouter)
httpNoRouterHandler HttpHandlerFunc
)
func (h *HttpRouter) Url() string {
url := h.Path
p := h.Group
for p != nil {
if p.Prefix != "/" {
url = p.Prefix + url
}
p = p.Parent
}
return url
}
// http group router
type HttpGroupRouter struct {
Prefix string
GinGroup *gin.RouterGroup
Middlewares []HttpHandlerFunc
HttpRouterList []*HttpRouter
Parent *HttpGroupRouter
Children map[string]*HttpGroupRouter
}
func NewHttpGroupRouter(prefix string, middleware ...HttpHandlerFunc) *HttpGroupRouter {
if len(prefix) == 0 {
log.Panic("http group router name can not be empty")
}
if group, ok := httpGroupRouterMap[prefix]; ok {
return group
}
httpGroupRouterMap[prefix] = &HttpGroupRouter{
Prefix: prefix,
Middlewares: middleware,
}
return httpGroupRouterMap[prefix]
}
func (g *HttpGroupRouter) Group(prefix string, middleware ...HttpHandlerFunc) *HttpGroupRouter {
if len(prefix) == 0 {
log.Panic("http sub group router name can not be empty")
}
if group, ok := g.Children[prefix]; ok {
return group
}
group := &HttpGroupRouter{
Prefix: prefix,
Parent: g,
Middlewares: middleware,
}
if g.Children == nil {
g.Children = make(map[string]*HttpGroupRouter)
}
g.Children[prefix] = group
return group
}
func getHttpRoutersWithSubGroup(g *HttpGroupRouter) []*HttpRouter {
var subRouterList []*HttpRouter
subRouterList = append(subRouterList, g.HttpRouterList...)
if len(g.Children) > 0 {
for _, sub := range g.Children {
subRouterList = append(subRouterList, getHttpRoutersWithSubGroup(sub)...)
}
}
return subRouterList
}
func GetHttpRouters() []*HttpRouter {
var routerList []*HttpRouter
for _, v := range httpGroupRouterMap {
routerList = append(routerList, getHttpRoutersWithSubGroup(v)...)
}
return routerList
}
func (g *HttpGroupRouter) Use(middlewares ...HttpHandlerFunc) {
g.Middlewares = append(g.Middlewares, middlewares...)
}
func (g *HttpGroupRouter) addHttpRouter(url, method string, actions ...HttpHandlerFunc) *HttpRouter {
router := &HttpRouter{
Path: url,
Method: method,
Group: g,
Actions: actions,
}
g.HttpRouterList = append(g.HttpRouterList, router)
return router
}
func (g *HttpGroupRouter) Get(url string, actions ...HttpHandlerFunc) *HttpRouter {
return g.addHttpRouter(url, http.MethodGet, actions...)
}
func (g *HttpGroupRouter) Post(url string, actions ...HttpHandlerFunc) *HttpRouter {
return g.addHttpRouter(url, http.MethodPost, actions...)
}
func (g *HttpGroupRouter) Put(url string, actions ...HttpHandlerFunc) *HttpRouter {
return g.addHttpRouter(url, http.MethodPut, actions...)
}
func (g *HttpGroupRouter) Delete(url string, actions ...HttpHandlerFunc) *HttpRouter {
return g.addHttpRouter(url, http.MethodDelete, actions...)
}
func (g *HttpGroupRouter) Patch(url string, actions ...HttpHandlerFunc) *HttpRouter {
return g.addHttpRouter(url, http.MethodPatch, actions...)
}
func (g *HttpGroupRouter) Head(url string, actions ...HttpHandlerFunc) *HttpRouter {
return g.addHttpRouter(url, http.MethodHead, actions...)
}
func (g *HttpGroupRouter) Options(url string, actions ...HttpHandlerFunc) *HttpRouter {
return g.addHttpRouter(url, http.MethodOptions, actions...)
}
func (g *HttpGroupRouter) Any(url string, actions ...HttpHandlerFunc) *HttpRouter {
return g.addHttpRouter(url, "Any", actions...)
}
// task
type TaskHandlerFunc func()
type TaskRouter struct {
Spec string
Action TaskHandlerFunc
}
var TaskRouterList []*TaskRouter
func AddTaskRouter(spec string, action TaskHandlerFunc) {
TaskRouterList = append(TaskRouterList, &TaskRouter{spec, action})
}
// cmd
type CmdHandlerFunc func(cmd *cobra.Command, args []string)
type ICmdArg interface {
SetFlag(cmd *cobra.Command)
}
func markFlagRequired(required bool, cmd *cobra.Command, name string) {
if required {
if err := cmd.MarkFlagRequired(name); err != nil {
log.Printf("cmd arg %s mark flag failed: %s", name, err.Error())
}
}
}
type CmdStringArg struct {
Name string
Shorthand string
Usage string
Required bool
Value string
}
func (c CmdStringArg) SetFlag(cmd *cobra.Command) {
cmd.Flags().StringP(c.Name, c.Shorthand, c.Value, c.Usage)
markFlagRequired(c.Required, cmd, c.Name)
}
type CmdStringSliceArg struct {
Name string
Shorthand string
Usage string
Required bool
Value []string
}
func (c CmdStringSliceArg) SetFlag(cmd *cobra.Command) {
cmd.Flags().StringSliceP(c.Name, c.Shorthand, c.Value, c.Usage)
markFlagRequired(c.Required, cmd, c.Name)
}
type CmdBoolArg struct {
Name string
Shorthand string
Usage string
Required bool
Value bool
}
func (c CmdBoolArg) SetFlag(cmd *cobra.Command) {
cmd.Flags().BoolP(c.Name, c.Shorthand, c.Value, c.Usage)
markFlagRequired(c.Required, cmd, c.Name)
}
type CmdIntArg struct {
Name string
Shorthand string
Usage string
Required bool
Value int
}
func (c CmdIntArg) SetFlag(cmd *cobra.Command) {
cmd.Flags().IntP(c.Name, c.Shorthand, c.Value, c.Usage)
markFlagRequired(c.Required, cmd, c.Name)
}
type CmdIntSliceArg struct {
Name string
Shorthand string
Usage string
Required bool
Value []int
}
func (c CmdIntSliceArg) SetFlag(cmd *cobra.Command) {
cmd.Flags().IntSliceP(c.Name, c.Shorthand, c.Value, c.Usage)
markFlagRequired(c.Required, cmd, c.Name)
}
type CmdInt64Arg struct {
Name string
Shorthand string
Usage string
Required bool
Value int64
}
func (c CmdInt64Arg) SetFlag(cmd *cobra.Command) {
cmd.Flags().Int64P(c.Name, c.Shorthand, c.Value, c.Usage)
markFlagRequired(c.Required, cmd, c.Name)
}
type CmdDurationArg struct {
Name string
Shorthand string
Usage string
Required bool
Value time.Duration
}
func (c CmdDurationArg) SetFlag(cmd *cobra.Command) {
cmd.Flags().DurationP(c.Name, c.Shorthand, c.Value, c.Usage)
markFlagRequired(c.Required, cmd, c.Name)
}
type CmdFloat64Arg struct {
Name string
Shorthand string
Usage string
Required bool
Value float64
}
func (c CmdFloat64Arg) SetFlag(cmd *cobra.Command) {
cmd.Flags().Float64P(c.Name, c.Shorthand, c.Value, c.Usage)
markFlagRequired(c.Required, cmd, c.Name)
}
type CmdRouter struct {
Use string
Short string
Action CmdHandlerFunc
Args []ICmdArg
}
var CmdRouterMap = make(map[string]*CmdRouter)
func AddCmdRouter(use, short string, action CmdHandlerFunc, args ...ICmdArg) {
cmdSlice := strings.Split(use, "/")
if len(cmdSlice) == 0 {
return
}
if _, ok := CmdRouterMap[use]; ok {
log.Panicf("http router duplicate : %s", use)
}
CmdRouterMap[use] = &CmdRouter{use, short, action, args}
}