84 lines
1.4 KiB
Go
84 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
|
)
|
|
|
|
type RabbitTransportMaster struct {
|
|
Url string
|
|
connection *amqp.Connection
|
|
//channel *amqp.Channel
|
|
}
|
|
|
|
const (
|
|
topic = "order-placed"
|
|
)
|
|
|
|
func (t *RabbitTransportMaster) Connect() error {
|
|
|
|
conn, err := amqp.DialConfig(t.Url, amqp.Config{
|
|
//Vhost: "/",
|
|
Properties: amqp.NewConnectionProperties(),
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
t.connection = conn
|
|
ch, err := conn.Channel()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer ch.Close()
|
|
if err := ch.ExchangeDeclare(
|
|
topic, // name
|
|
"topic", // type
|
|
true, // durable
|
|
false, // auto-delete
|
|
false, // internal
|
|
false, // noWait
|
|
nil, // arguments
|
|
); err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err = ch.QueueDeclare(
|
|
topic, // name of the queue
|
|
true, // durable
|
|
false, // delete when unused
|
|
false, // exclusive
|
|
false, // noWait
|
|
nil, // arguments
|
|
); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (t *RabbitTransportMaster) Close() error {
|
|
log.Println("Closing master channel")
|
|
return t.connection.Close()
|
|
//return t.channel.Close()
|
|
}
|
|
|
|
func (t *RabbitTransportMaster) OrderCompleted(data []byte) error {
|
|
ch, err := t.connection.Channel()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer ch.Close()
|
|
return ch.Publish(
|
|
topic,
|
|
topic,
|
|
true,
|
|
false,
|
|
amqp.Publishing{
|
|
ContentType: "application/json",
|
|
Body: data,
|
|
},
|
|
)
|
|
}
|