-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (43 loc) · 1.2 KB
/
main.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
package main
import (
"encoding/json"
"log"
"time"
"github.com/google/uuid"
)
func main() {
log.Println("Starting CrowdControl Coin bridge.")
defer func() {
log.Println("Shutting down.")
}()
settings := LoadSettings()
log.Println("Settings: ")
log.Println(settings)
repo := ConnectToDatabase(settings)
stream := ConnectToRabbitMQ(settings)
log.Println("Setup complete. Starting message handling loop.")
ListenAndServe(stream, repo)
log.Println("End of message queue.")
}
func ListenAndServe(stream RabbitStream, repo Repository) {
for msg := range stream.messages {
log.Println("Incoming message.")
content := make(map[string]interface{})
err := json.Unmarshal(msg.Body, &content)
if err != nil {
//Failed to unserialize the event. So the message is busted and needs to be discarded.
log.Println("Discarding invalid message from AMQP.")
msg.Nack(false, false)
}
event := Event{
Id: uuid.New(),
Recieved: time.Now().UTC(),
Exchange: msg.Exchange,
RoutingKey: msg.RoutingKey,
Content: content,
}
log.Printf("Storing message from %s (%s): %s", event.Exchange, event.RoutingKey, string(msg.Body))
repo.InsertEvent(event)
msg.Ack(false)
}
}