-
Notifications
You must be signed in to change notification settings - Fork 43
/
interceptor.go
37 lines (30 loc) · 895 Bytes
/
interceptor.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
package neo
type Next func()
type appliable interface {
apply(ctx *Ctx, fns []appliable, current int)
}
// Composing multiple middlewares into one chained function.
func compose(fns []appliable) func(*Ctx) {
return func(ctx *Ctx) {
fns[0].apply(ctx, fns, 0)
}
}
// Representing middleware handler.
// It accepts Neo Context and Next function for calling next middleware in chain.
type Middleware func(*Ctx, Next)
func (m Middleware) apply(ctx *Ctx, fns []appliable, current int) {
m(ctx, func() {
current++
if len(fns) > current {
fns[current].apply(ctx, fns, current)
}
})
}
// contains list of functions for intercepting requests before and after route handle call
type interceptor struct {
middlewares []appliable
}
// Adding new middleware into chain of middlewares.
func (m *interceptor) Use(fn Middleware) {
m.middlewares = append(m.middlewares, appliable(&fn))
}