-
Notifications
You must be signed in to change notification settings - Fork 1
/
fakedata.go
81 lines (69 loc) · 1.97 KB
/
fakedata.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
package main
import (
"fmt"
"io/ioutil"
"log"
"github.com/streadway/amqp"
)
func main() {
rabbitConfig := readRabbitConf()
conn := connectRabbit(rabbitConfig)
defer conn.Close()
ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer ch.Close()
content, err := ioutil.ReadFile(rabbitConfig.filename)
failOnError(err, "failed to read data file")
rabbitArtifacts := setupRabbitMqTopicsAndQueues(ch, rabbitConfig.queriesExchange, rabbitConfig.queriesQueue, rabbitConfig.queriesRoutingKey)
msgs, consumeErr := ch.Consume(
rabbitArtifacts.queriesQueueName,
"",
false,
false,
false,
false,
nil,
)
failOnError(consumeErr, "failed to consume messages from queue")
forever := make(chan bool)
answersToSend := make(chan rabbitMqDestination)
log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
go func() {
for msg := range msgs {
rabbitMqDest, err := extractDestinationAndRoutingKeyFromReplyTo(msg.ReplyTo)
logOnError(err, "failed to parse reply-to: %s")
if err != nil {
err = msg.Nack(false, false)
logOnError(err, fmt.Sprintf("failed to NACK message to %s", rabbitMqDest))
} else {
log.Println(fmt.Sprintf("received a query message and will send response to %s", rabbitMqDest))
answersToSend <- rabbitMqDest
err = msg.Ack(false)
logOnError(err, fmt.Sprintf("failed to ACK message to to %s", rabbitMqDest))
}
}
}()
go func(channel *amqp.Channel, body []byte) {
for {
rabbitDest := <-answersToSend
sendErr := channel.Publish(rabbitDest.destination, rabbitDest.routingKey, false, false,
amqp.Publishing{
ContentType: "text/plain",
Body: body,
})
logOnError(sendErr, "failed to send reply message:")
}
}(ch, content)
<-forever
}
func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
panic(fmt.Sprintf("%s: %s", msg, err))
}
}
func logOnError(err error, msg string) {
if err != nil {
log.Printf("%s: %s\n", msg, err)
}
}