forked from krakend/krakend-amqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer.go
129 lines (112 loc) · 2.87 KB
/
producer.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package amqp
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"strconv"
"time"
"github.com/streadway/amqp"
"github.com/luraproject/lura/config"
"github.com/luraproject/lura/proxy"
)
const producerNamespace = "github.com/devopsfaith/krakend-amqp/produce"
var errNoProducerCfgDefined = errors.New("no amqp producer defined")
func getProducerConfig(remote *config.Backend) (*producerCfg, error) {
v, ok := remote.ExtraConfig[producerNamespace]
if !ok {
return nil, errNoProducerCfgDefined
}
b, _ := json.Marshal(v)
cfg := &producerCfg{}
err := json.Unmarshal(b, cfg)
return cfg, err
}
type producerCfg struct {
queueCfg
Mandatory bool `json:"mandatory"`
Immediate bool `json:"immediate"`
ExpirationKey string `json:"exp_key"`
ReplyToKey string `json:"reply_to_key"`
MessageIdKey string `json:"msg_id_key"`
PriorityKey string `json:"priority_key"`
RoutingKey string `json:"routing_key"`
}
func (f backendFactory) initProducer(ctx context.Context, remote *config.Backend) (proxy.Proxy, error) {
if len(remote.Host) < 1 {
return proxy.NoopProxy, errNoBackendHostDefined
}
dns := remote.Host[0]
cfg, err := getProducerConfig(remote)
if err != nil {
f.logger.Debug(fmt.Sprintf("AMQP: %s: %s", dns, err.Error()))
return proxy.NoopProxy, err
}
ch, close, err := f.newChannel(dns)
if err != nil {
f.logger.Error(fmt.Sprintf("AMQP: getting the channel for %s/%s: %s", dns, cfg.Name, err.Error()))
return proxy.NoopProxy, err
}
err = ch.ExchangeDeclare(
cfg.Exchange, // name
"topic", // type
cfg.Durable,
cfg.Delete,
cfg.Exclusive,
cfg.NoWait,
nil,
)
if err != nil {
f.logger.Error(fmt.Sprintf("AMQP: declaring the exchange for %s/%s: %s", dns, cfg.Name, err.Error()))
close()
return proxy.NoopProxy, err
}
go func() {
<-ctx.Done()
close()
}()
return func(ctx context.Context, r *proxy.Request) (*proxy.Response, error) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
contentType := ""
headers := amqp.Table{}
for k, vs := range r.Headers {
headerValues := make([]interface{}, len(vs))
for k, v := range vs {
headerValues[k] = v
}
headers[k] = headerValues
}
pub := amqp.Publishing{
Headers: headers,
ContentType: contentType,
Body: body,
Timestamp: time.Now(),
Expiration: r.Params[cfg.ExpirationKey],
ReplyTo: r.Params[cfg.ReplyToKey],
MessageId: r.Params[cfg.MessageIdKey],
}
if len(r.Headers["Content-Type"]) > 0 {
pub.ContentType = r.Headers["Content-Type"][0]
}
if v, ok := r.Params[cfg.PriorityKey]; ok {
if i, err := strconv.Atoi(v); err == nil {
pub.Priority = uint8(i)
}
}
err = ch.Publish(
cfg.Exchange,
r.Params[cfg.RoutingKey],
cfg.Mandatory,
cfg.Immediate,
pub,
)
if err != nil {
return nil, err
}
return &proxy.Response{IsComplete: true}, nil
}, nil
}