-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.go
43 lines (36 loc) · 943 Bytes
/
pipeline.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
package bingo_router
type Pipeline struct {
send *Context // 穿过管道的上下文
through []MiddlewareHandle // 中间件数组
current int // 当前执行到第几个中间件
}
// new(Pipeline).send(context).through(middleware).then(function(context){})
func (p *Pipeline) Send(context *Context) *Pipeline {
p.send = context
return p
}
func (p *Pipeline) Through(middlewares []MiddlewareHandle) *Pipeline {
p.through = middlewares
return p
}
func (p *Pipeline) Exec() {
if len(p.through) > p.current {
m := p.through[p.current]
p.current += 1
m(p.send, func(c *Context) {
p.Exec()
})
}
}
// 这里是路由的最后一站
func (p *Pipeline) Then(then func(context *Context)) {
// 按照顺序执行
// 将then作为最后一站的中间件
var m MiddlewareHandle
m = func(c *Context, next func(c *Context)) {
then(c)
next(c)
}
p.through = append(p.through, m)
p.Exec()
}