-
Notifications
You must be signed in to change notification settings - Fork 8
/
server_middleware.go
59 lines (44 loc) · 1.33 KB
/
server_middleware.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
package amqprpc
/*
ServerMiddlewareFunc represent a function that can be used as a middleware.
For example:
func myMiddle(next HandlerFunc) HandlerFunc {
// Preinitialization of middleware here.
return func(ctx context.Context, rw *ResponseWriter d amqp.Delivery) {
// Before handler execution here.
// Execute the handler.
next(ctx, rw, d)
// After execution here.
}
}
s := New("url")
// Add middleware to specific handler.
s.Bind(DirectBinding("foobar", myMiddle(HandlerFunc)))
// Add middleware to all handlers on the server.
s.AddMiddleware(myMiddle)
*/
type ServerMiddlewareFunc func(next HandlerFunc) HandlerFunc
/*
ServerMiddlewareChain will attatch all given middlewares to your HandlerFunc.
The middlewares will be executed in the same order as your input.
For example:
s := New("url")
s.Bind(DirectBinding(
"foobar",
ServerMiddlewareChain(
myHandler,
middlewareOne,
middlewareTwo,
middlewareThree,
),
))
*/
func ServerMiddlewareChain(next HandlerFunc, m ...ServerMiddlewareFunc) HandlerFunc {
if len(m) == 0 {
// The middleware chain is done. All middlewares have been applied.
return next
}
// Nest the middlewares so that we attatch them in order.
// The first middleware will have the second middleware applied, and so on.
return m[0](ServerMiddlewareChain(next, m[1:]...))
}