0
0
Fork 0
mirror of https://github.com/crazy-max/diun.git synced 2025-01-27 01:08:50 +00:00
crazy-max_diun/internal/notif/amqp/client.go

93 lines
1.7 KiB
Go

package amqp
import (
"fmt"
"github.com/crazy-max/diun/v4/internal/model"
"github.com/crazy-max/diun/v4/internal/msg"
"github.com/crazy-max/diun/v4/internal/notif/notifier"
"github.com/crazy-max/diun/v4/pkg/utl"
amqp "github.com/rabbitmq/amqp091-go"
)
// Client represents an active amqp notification object
type Client struct {
*notifier.Notifier
cfg *model.NotifAmqp
meta model.Meta
}
// New creates a new amqp notification instance
func New(config *model.NotifAmqp, meta model.Meta) notifier.Notifier {
return notifier.Notifier{
Handler: &Client{
cfg: config,
meta: meta,
},
}
}
// Name returns notifier's name
func (c *Client) Name() string {
return "amqp"
}
// Send creates and sends a amqp notification with an entry
func (c *Client) Send(entry model.NotifEntry) error {
username, err := utl.GetSecret(c.cfg.Username, c.cfg.UsernameFile)
if err != nil {
return err
}
password, err := utl.GetSecret(c.cfg.Password, c.cfg.PasswordFile)
if err != nil {
return err
}
conn, err := amqp.Dial(fmt.Sprintf("amqp://%s:%s@%s:%d/", username, password, c.cfg.Host, c.cfg.Port))
if err != nil {
return err
}
defer conn.Close()
ch, err := conn.Channel()
if err != nil {
return err
}
defer ch.Close()
q, err := ch.QueueDeclare(
c.cfg.Queue,
false,
false,
false,
false,
nil,
)
if err != nil {
return err
}
message, err := msg.New(msg.Options{
Meta: c.meta,
Entry: entry,
})
if err != nil {
return err
}
body, err := message.RenderJSON()
if err != nil {
return err
}
return ch.Publish(
c.cfg.Exchange,
q.Name,
false,
false,
amqp.Publishing{
ContentType: "application/json",
Body: body,
})
}