100 lines
1.7 KiB
Go
100 lines
1.7 KiB
Go
package event
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
|
)
|
|
|
|
type Consumer struct {
|
|
conn *amqp.Connection
|
|
queueName string
|
|
}
|
|
|
|
func NewConsumer(conn *amqp.Connection) (Consumer, error) {
|
|
consumer := Consumer{
|
|
conn: conn,
|
|
}
|
|
|
|
err := consumer.setup()
|
|
if err != nil {
|
|
return Consumer{}, err
|
|
}
|
|
return consumer, err
|
|
}
|
|
|
|
func (consumer *Consumer) setup() error {
|
|
channel, err := consumer.conn.Channel()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return declaireExchange(channel)
|
|
}
|
|
|
|
type Payload struct {
|
|
Name string `json:"name"`
|
|
Data string `json:"data"`
|
|
}
|
|
|
|
func (consumer *Consumer) Listen(topics []string) error {
|
|
ch, err := consumer.conn.Channel()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer ch.Close()
|
|
|
|
q, err := declaireRandomQueue(ch)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, s := range topics {
|
|
ch.QueueBind(q.Name, s, "logs_topic", false, nil)
|
|
}
|
|
|
|
messages, err := ch.Consume(q.Name, "", true, false, false, false, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
forever := make(chan bool)
|
|
go func() {
|
|
for d := range messages {
|
|
var payload Payload
|
|
json.Unmarshal(d.Body, &payload)
|
|
|
|
go handlePayload(payload)
|
|
}
|
|
}()
|
|
|
|
fmt.Printf("Waiting for message [Exchange, Queue] [logs_topic, %s]\n", q.Name)
|
|
<-forever
|
|
return nil
|
|
}
|
|
|
|
func handlePayload(payload Payload) {
|
|
switch payload.Name {
|
|
case "log", "event":
|
|
// log whatever we get
|
|
err := logEvent(payload)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
case "auth":
|
|
// authenticate
|
|
// You can have as many cases as you want, as long as you write the logic.
|
|
default:
|
|
err := logEvent(payload)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func logEvent(entry Payload) error {
|
|
return nil
|
|
}
|