message broker sifatida redis qo'shildi
This commit is contained in:
@@ -2,14 +2,12 @@ package consumer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/JscorpTech/notification/internal/broker"
|
||||
"github.com/JscorpTech/notification/internal/domain"
|
||||
"github.com/JscorpTech/notification/internal/notifier"
|
||||
"github.com/JscorpTech/notification/internal/rabbitmq"
|
||||
"github.com/streadway/amqp"
|
||||
)
|
||||
|
||||
type notificationConsumer struct {
|
||||
@@ -23,39 +21,26 @@ func NewNotificationConsumer(ctx context.Context) domain.NotificationConsumerPor
|
||||
}
|
||||
|
||||
func (n *notificationConsumer) Start() {
|
||||
conn, ch, err := rabbitmq.Connect()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
||||
brokerName := os.Getenv("BROKER")
|
||||
if brokerName == "" {
|
||||
brokerName = "redis"
|
||||
}
|
||||
defer conn.Close()
|
||||
defer ch.Close()
|
||||
|
||||
exchangeName := "notification"
|
||||
queueName := "notification"
|
||||
routingKey := "notification"
|
||||
|
||||
ch.ExchangeDeclare(exchangeName, "direct", true, false, false, false, nil)
|
||||
q, _ := ch.QueueDeclare(queueName, true, false, false, false, nil)
|
||||
ch.QueueBind(q.Name, routingKey, exchangeName, false, nil)
|
||||
|
||||
msgs, _ := ch.Consume(q.Name, "", true, false, false, false, nil)
|
||||
|
||||
go func() {
|
||||
for msg := range msgs {
|
||||
go n.Handler(msg)
|
||||
}
|
||||
}()
|
||||
|
||||
var brokerService domain.BrokerPort
|
||||
switch brokerName {
|
||||
case "redis":
|
||||
brokerService = broker.NewRedisBroker(n.Ctx)
|
||||
case "rabbitmq":
|
||||
brokerService = broker.NewRabbitMQBroker(n.Ctx)
|
||||
default:
|
||||
brokerService = broker.NewRedisBroker(n.Ctx)
|
||||
}
|
||||
brokerService.Subscribe(os.Getenv("TOPIC"), n.Handler)
|
||||
fmt.Println("🚀 Server started. Ctrl+C to quit.")
|
||||
select {}
|
||||
}
|
||||
|
||||
func (n *notificationConsumer) Handler(msg amqp.Delivery) {
|
||||
var notification domain.NotificationMsg
|
||||
err := json.Unmarshal(msg.Body, ¬ification)
|
||||
if err != nil {
|
||||
fmt.Print(err.Error())
|
||||
}
|
||||
func (n *notificationConsumer) Handler(notification domain.NotificationMsg) {
|
||||
var ntf domain.NotifierPort
|
||||
switch notification.Type {
|
||||
case "sms":
|
||||
|
||||
Reference in New Issue
Block a user