From 23874ef97534b341de3d4c1e70a2b9f105e284e9 Mon Sep 17 00:00:00 2001 From: ostiwe Date: Sun, 10 Aug 2025 22:19:33 +0300 Subject: [PATCH] feat(rabbitmq): Add rabbitmq module --- modules/rabbit/rabbit.go | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 modules/rabbit/rabbit.go diff --git a/modules/rabbit/rabbit.go b/modules/rabbit/rabbit.go new file mode 100644 index 0000000..7484c63 --- /dev/null +++ b/modules/rabbit/rabbit.go @@ -0,0 +1,66 @@ +package rabbit + +import ( + "os" + "time" + + "git.ostiwe.com/ostiwe-com/status/modules/log" + "git.ostiwe.com/ostiwe-com/status/pkg/rabbit" + amqp "github.com/rabbitmq/amqp091-go" + "github.com/sirupsen/logrus" +) + +var Channel *amqp.Channel + +func InitConnection() { + log.Global.Put(log.RABBIT, logrus.New()) + log.Global.Put(log.QUEUE, logrus.New()) + + rConn, err := rabbit.NewConnection(rabbit.ConnectionArgs{ + Host: os.Getenv("RABBIT_HOST"), + Password: os.Getenv("RABBIT_PASSWORD"), + User: os.Getenv("RABBIT_USER"), + Port: os.Getenv("RABBIT_PORT"), + ReconnectInterval: time.Second * 5, + Logger: log.Global.Get(log.RABBIT), + }, + ) + + if err != nil { + panic(err) + } + + rConn.IsAlive() + + if err = declareQueues(rConn); err != nil { + panic(err) + } + + channel, err := rConn.Channel() + if err != nil { + panic(err) + } + + Channel = channel +} + +func declareQueues(conn *rabbit.Connection) error { + channel, err := conn.Channel() + if err != nil { + return err + } + + _, err = channel.QueueDeclare( + "tasks", + true, + false, + false, + false, + nil, + ) + if err != nil { + return err + } + + return nil +}