-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter_group.go
65 lines (54 loc) · 1.53 KB
/
router_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
package equeue
type Router interface {
Routes
Group(string, ...HandlerFunc) *RouterGroup
}
type Routes interface {
Use(handlers ...HandlerFunc) Routes
Handle(topic string, subscriptionName string, maxWorker int, handlers ...HandlerFunc) Routes
}
type RouterGroup struct {
Handlers HandlersChain
baseTopic string
engine *Engine
root bool
}
var _ Router = new(RouterGroup)
func (g *RouterGroup) Use(middleware ...HandlerFunc) Routes {
g.Handlers = append(g.Handlers, middleware...)
return g.returnObj()
}
func (g *RouterGroup) Group(relativeTopic string, handlers ...HandlerFunc) *RouterGroup {
return &RouterGroup{
Handlers: g.combineHandlers(handlers),
baseTopic: g.absoluteTopic(relativeTopic),
engine: g.engine,
}
}
func (g *RouterGroup) Handle(relativeTopic string, subscriptionName string, maxWorker int, handlers ...HandlerFunc) Routes {
g.engine.addRoute(
g.absoluteTopic(relativeTopic),
subscriptionName,
maxWorker,
g.combineHandlers(handlers))
return g.returnObj()
}
func (g *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain {
finalSize := len(g.Handlers) + len(handlers)
if finalSize >= int(abortIndex) {
panic("too many handlers")
}
mergedHandlers := make(HandlersChain, finalSize)
copy(mergedHandlers, g.Handlers)
copy(mergedHandlers[len(g.Handlers):], handlers)
return mergedHandlers
}
func (g *RouterGroup) absoluteTopic(relativeTopic string) string {
return g.baseTopic + relativeTopic
}
func (g *RouterGroup) returnObj() Routes {
if g.root {
return g.engine
}
return g
}