-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmiddleware.go
77 lines (61 loc) · 1.66 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package gorabbitmq
import (
"context"
"github.com/Noobygames/amqp"
)
type (
// Context is the context, that is passed to the handler and middleware
// functions
Context interface {
Delivery() amqp.Delivery
DeliveryContext() context.Context
Nack(multiple bool, requeue bool) error
Ack(multiple bool) error
Body() []byte
Queue() Queue
Set(key interface{}, value interface{})
Value(key interface{}) interface{}
}
// MiddlewareFunc defines a function to process middleware.
MiddlewareFunc func(HandlerFunc) HandlerFunc
// HandlerFunc defines a function to serve RabbitMQ deliveries.
HandlerFunc func(Context) error
gorabbitmqContext struct {
ctx context.Context
delivery amqp.Delivery
queue Queue
values map[interface{}]interface{}
}
)
func newContext(ctx context.Context, delivery amqp.Delivery, q Queue) Context {
return &gorabbitmqContext{
ctx: ctx,
delivery: delivery,
queue: q,
values: make(map[interface{}]interface{}),
}
}
func (gc *gorabbitmqContext) Queue() Queue {
return gc.queue
}
func (gc *gorabbitmqContext) DeliveryContext() context.Context {
return gc.ctx
}
func (gc *gorabbitmqContext) Delivery() amqp.Delivery {
return gc.delivery
}
func (gc *gorabbitmqContext) Nack(multiple bool, requeue bool) error {
return gc.delivery.Nack(multiple, requeue)
}
func (gc *gorabbitmqContext) Ack(multiple bool) error {
return gc.delivery.Ack(multiple)
}
func (gc *gorabbitmqContext) Body() []byte {
return gc.delivery.Body
}
func (gc *gorabbitmqContext) Set(key interface{}, value interface{}) {
gc.values[key] = value
}
func (gc *gorabbitmqContext) Value(key interface{}) interface{} {
return gc.values[key]
}