feat: Notification service qo'shildi va RabbitMQ integratsiya qilindi
This commit is contained in:
13
internal/config/config.go
Normal file
13
internal/config/config.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func GetEnv(key, fallback string) string {
|
||||
val := os.Getenv(key)
|
||||
if val == "" {
|
||||
return fallback
|
||||
}
|
||||
return val
|
||||
}
|
||||
62
internal/consumer/notification.go
Normal file
62
internal/consumer/notification.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package consumer
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"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{}
|
||||
|
||||
func NewNotificationConsumer() domain.NotificationConsumerPort {
|
||||
return ¬ificationConsumer{}
|
||||
}
|
||||
|
||||
func (n *notificationConsumer) Start() {
|
||||
conn, ch, err := rabbitmq.Connect()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
}()
|
||||
|
||||
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())
|
||||
}
|
||||
var ntf domain.NotifierPort
|
||||
switch notification.Type {
|
||||
case "sms":
|
||||
ntf = notifier.NewSmsNotifier()
|
||||
case "email":
|
||||
ntf = notifier.NewEmailNotifier()
|
||||
}
|
||||
ntf.SendMessage(notification.To, notification.Message)
|
||||
}
|
||||
18
internal/domain/notification.go
Normal file
18
internal/domain/notification.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package domain
|
||||
|
||||
import "github.com/streadway/amqp"
|
||||
|
||||
type NotificationConsumerPort interface {
|
||||
Start()
|
||||
Handler(amqp.Delivery)
|
||||
}
|
||||
|
||||
type NotifierPort interface {
|
||||
SendMessage([]string, string)
|
||||
}
|
||||
|
||||
type NotificationMsg struct {
|
||||
Type string `json:"type"`
|
||||
Message string `json:"message"`
|
||||
To []string `json:"to"`
|
||||
}
|
||||
16
internal/notifier/email.go
Normal file
16
internal/notifier/email.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"github.com/JscorpTech/notification/internal/domain"
|
||||
"github.com/k0kubun/pp/v3"
|
||||
)
|
||||
|
||||
type emailNotifier struct{}
|
||||
|
||||
func NewEmailNotifier() domain.NotifierPort {
|
||||
return &emailNotifier{}
|
||||
}
|
||||
|
||||
func (n *emailNotifier) SendMessage(to []string, body string) {
|
||||
pp.Print(to, body)
|
||||
}
|
||||
16
internal/notifier/sms.go
Normal file
16
internal/notifier/sms.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"github.com/JscorpTech/notification/internal/domain"
|
||||
"github.com/k0kubun/pp/v3"
|
||||
)
|
||||
|
||||
type smsNotifier struct{}
|
||||
|
||||
func NewSmsNotifier() domain.NotifierPort {
|
||||
return &smsNotifier{}
|
||||
}
|
||||
|
||||
func (n *smsNotifier) SendMessage(to []string, body string) {
|
||||
pp.Print(to, body)
|
||||
}
|
||||
21
internal/rabbitmq/connection.go
Normal file
21
internal/rabbitmq/connection.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package rabbitmq
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/streadway/amqp"
|
||||
)
|
||||
|
||||
func Connect() (*amqp.Connection, *amqp.Channel, error) {
|
||||
conn, err := amqp.Dial("amqp://guest:guest@127.0.0.1:5672/")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
ch, err := conn.Channel()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
log.Println("🐇 Connected to RabbitMQ")
|
||||
|
||||
return conn, ch, nil
|
||||
}
|
||||
Reference in New Issue
Block a user