This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messenger.go
139 lines (118 loc) · 3.48 KB
/
messenger.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
130
131
132
133
134
135
136
137
138
139
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"github.com/streadway/amqp"
)
// Checksum used in the message
type Checksum struct {
Type string `json:"type"`
Value string `json:"value"`
}
// The Event struct
type Event struct {
Operation string `json:"operation"`
Username string `json:"user"`
Filepath string `json:"filepath"`
Filesize int64 `json:"filesize"`
Checksum []interface{} `json:"encrypted_checksums"`
}
// Messenger is an interface for sending messages for different file events
type Messenger interface {
SendMessage(message Event) error
}
// AMQPMessenger is a Messenger that sends messages to a local AMQP broker
type AMQPMessenger struct {
connection *amqp.Connection
channel *amqp.Channel
exchange string
routingKey string
confirmsChan <-chan amqp.Confirmation
}
// NewAMQPMessenger creates a new messenger that can communicate with a backend
// amqp server.
func NewAMQPMessenger(c BrokerConfig, tlsConfig *tls.Config) *AMQPMessenger {
brokerURI := buildMqURI(c.host, c.port, c.user, c.password, c.vhost, c.ssl)
var connection *amqp.Connection
var channel *amqp.Channel
var err error
log.Debugf("connecting to broker with <%s>", brokerURI)
if c.ssl {
connection, err = amqp.DialTLS(brokerURI, tlsConfig)
} else {
connection, err = amqp.Dial(brokerURI)
}
if err != nil {
log.Panicf("brokerErrMsg 1: %s", err)
}
channel, err = connection.Channel()
if err != nil {
log.Panicf("brokerErrMsg 2: %s", err)
}
log.Debug("enabling publishing confirms.")
if err = channel.Confirm(false); err != nil {
log.Fatalf("channel could not be put into confirm mode: %s", err)
}
if err = channel.ExchangeDeclarePassive(
c.exchange, // name
"topic", // type
true, // durable
false, // auto-deleted
false, // internal
false, // noWait
nil, // arguments
); err != nil {
log.Fatalf("exchange declare: %s", err)
}
confirmsChan := make(chan amqp.Confirmation, 1)
if err := channel.Confirm(false); err != nil {
close(confirmsChan)
log.Fatalf("Channel could not be put into confirm mode: %s\n", err)
}
return &AMQPMessenger{connection, channel, c.exchange, c.routingKey, channel.NotifyPublish(confirmsChan)}
}
// SendMessage sends message to RabbitMQ if the upload is finished
func (m *AMQPMessenger) SendMessage(message Event) error {
body, e := json.Marshal(message)
if e != nil {
log.Fatalf("%s", e)
}
corrID, _ := uuid.NewRandom()
err := m.channel.Publish(
m.exchange,
m.routingKey,
false, // mandatory
false, // immediate
amqp.Publishing{
Headers: amqp.Table{},
ContentEncoding: "UTF-8",
ContentType: "application/json",
DeliveryMode: amqp.Persistent,
CorrelationId: corrID.String(),
Priority: 0, // 0-9
Body: []byte(body),
},
)
if err != nil {
return err
}
confirmed := <-m.confirmsChan
if !confirmed.Ack {
return fmt.Errorf("failed delivery of delivery tag: %d", confirmed.DeliveryTag)
}
log.Debugf("confirmed delivery with delivery tag: %d", confirmed.DeliveryTag)
return nil
}
// BuildMqURI builds the MQ URI
func buildMqURI(mqHost, mqPort, mqUser, mqPassword, mqVhost string, ssl bool) string {
brokerURI := ""
if ssl {
brokerURI = "amqps://" + mqUser + ":" + mqPassword + "@" + mqHost + ":" + mqPort + mqVhost
} else {
brokerURI = "amqp://" + mqUser + ":" + mqPassword + "@" + mqHost + ":" + mqPort + mqVhost
}
return brokerURI
}