update handler (#2)
* 🔧 Remove commented-out regex patterns and initialization in config. * ✨ Add NATS message publisher with configuration 📩🚀 * ✨ Add new PlainTextMarshaler and refactor handler in nats package.
This commit is contained in:
@@ -15,6 +15,7 @@ var MyConfig *Config
|
||||
type Config struct {
|
||||
Nats NatsConfig
|
||||
Subscription SubscriptionConfig
|
||||
Publisher PublisherConfig
|
||||
Timeouts TimeoutsConfig
|
||||
Hasura HasuraConfig
|
||||
}
|
||||
@@ -30,6 +31,10 @@ type SubscriptionConfig struct {
|
||||
QueueGroup string `mapstructure:"queue_group"`
|
||||
}
|
||||
|
||||
type PublisherConfig struct {
|
||||
Topic string `mapstructure:"topic"`
|
||||
}
|
||||
|
||||
type TimeoutsConfig struct {
|
||||
Server time.Duration `mapstructure:"server"`
|
||||
ReconnectWait time.Duration `mapstructure:"reconnect_wait"`
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package handlers
|
||||
package nats
|
||||
|
||||
import (
|
||||
"caatsm/internal/config"
|
||||
@@ -6,13 +6,11 @@ import (
|
||||
"caatsm/internal/parsers"
|
||||
"caatsm/internal/repository"
|
||||
"caatsm/pkg/utils"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/ThreeDotsLabs/watermill"
|
||||
"github.com/ThreeDotsLabs/watermill/message"
|
||||
nc "github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
type MessageHandler struct {
|
||||
@@ -44,6 +42,7 @@ func (handler *MessageHandler) HandleMessage(msg *message.Message) error {
|
||||
log.Infof("parsed [%s]: %v\n", msg.UUID, parsed.ToString())
|
||||
}
|
||||
handler.SaveMessage(parsed, msg.UUID)
|
||||
handler.Publish(parsed)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -57,16 +56,8 @@ func (n *MessageHandler) SaveMessage(parsed *domain.ParsedMessage, uuid string)
|
||||
}
|
||||
}
|
||||
|
||||
type PlainTextMarshaler struct{}
|
||||
|
||||
func (m *PlainTextMarshaler) Marshal(topic string, msg nc.Msg) ([]byte, error) {
|
||||
return msg.Data, nil
|
||||
}
|
||||
|
||||
func (m *PlainTextMarshaler) Unmarshal(newMsg *nc.Msg) (*message.Message, error) {
|
||||
if newMsg == nil {
|
||||
return nil, errors.New("empty message")
|
||||
func (n *MessageHandler) Publish(parsed *domain.ParsedMessage) {
|
||||
if err := Publish(n.config, parsed); err != nil {
|
||||
utils.GetSugaredLogger().Error("error publishing message", err, map[string]interface{}{"message": parsed})
|
||||
}
|
||||
msg := message.NewMessage(watermill.NewUUID(), newMsg.Data)
|
||||
return msg, nil
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package nats
|
||||
|
||||
import (
|
||||
"caatsm/internal/config"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/ThreeDotsLabs/watermill"
|
||||
"github.com/ThreeDotsLabs/watermill-nats/v2/pkg/nats"
|
||||
"github.com/ThreeDotsLabs/watermill/message"
|
||||
nc "github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
func Publish(config *config.Config, parsedMessage interface{}) error {
|
||||
logger := watermill.NewStdLogger(false, false)
|
||||
options := []nc.Option{
|
||||
nc.RetryOnFailedConnect(true),
|
||||
nc.Timeout(config.Timeouts.Server),
|
||||
nc.ReconnectWait(config.Timeouts.ReconnectWait),
|
||||
}
|
||||
jsConfig := nats.JetStreamConfig{Disabled: true}
|
||||
|
||||
publisher, err := nats.NewPublisher(
|
||||
nats.PublisherConfig{
|
||||
URL: config.Nats.URL,
|
||||
NatsOptions: options,
|
||||
JetStream: jsConfig,
|
||||
},
|
||||
logger,
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
logger.Info("NATS server connected", map[string]interface{}{"url": config.Nats.URL})
|
||||
logger.Info("Publishing message to NATS topic", map[string]interface{}{"topic": config.Publisher.Topic})
|
||||
|
||||
messageText, err := json.Marshal(parsedMessage)
|
||||
if err != nil {
|
||||
logger.Error("Failed to marshal message", err, map[string]interface{}{"message": parsedMessage})
|
||||
}
|
||||
msg := message.NewMessage(watermill.NewUUID(), []byte(messageText))
|
||||
err = publisher.Publish(config.Publisher.Topic, msg)
|
||||
if err != nil {
|
||||
logger.Error("Failed to publish message to NATS topic", err, map[string]interface{}{"topic": config.Publisher.Topic})
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+19
-3
@@ -2,15 +2,16 @@ package nats
|
||||
|
||||
import (
|
||||
"caatsm/internal/config"
|
||||
"caatsm/internal/handlers"
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/ThreeDotsLabs/watermill"
|
||||
"github.com/ThreeDotsLabs/watermill-nats/v2/pkg/nats"
|
||||
"github.com/ThreeDotsLabs/watermill/message"
|
||||
nc "github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
func Subscribe(config *config.Config, marshaler *handlers.PlainTextMarshaler, handler *handlers.MessageHandler) {
|
||||
func Subscribe(config *config.Config, marshaler *PlainTextMarshaler) {
|
||||
logger := watermill.NewStdLogger(false, false)
|
||||
options := []nc.Option{
|
||||
nc.RetryOnFailedConnect(true),
|
||||
@@ -44,8 +45,9 @@ func Subscribe(config *config.Config, marshaler *handlers.PlainTextMarshaler, ha
|
||||
return
|
||||
}
|
||||
|
||||
handlers := New(config)
|
||||
for msg := range messages {
|
||||
if err := handler.HandleMessage(msg); err == nil {
|
||||
if err := handlers.HandleMessage(msg); err == nil {
|
||||
msg.Ack()
|
||||
} else {
|
||||
logger.Error("Failed to handle message", err, map[string]interface{}{"message": msg})
|
||||
@@ -53,3 +55,17 @@ func Subscribe(config *config.Config, marshaler *handlers.PlainTextMarshaler, ha
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type PlainTextMarshaler struct{}
|
||||
|
||||
func (m *PlainTextMarshaler) Marshal(topic string, msg nc.Msg) ([]byte, error) {
|
||||
return msg.Data, nil
|
||||
}
|
||||
|
||||
func (m *PlainTextMarshaler) Unmarshal(newMsg *nc.Msg) (*message.Message, error) {
|
||||
if newMsg == nil {
|
||||
return nil, errors.New("empty message")
|
||||
}
|
||||
msg := message.NewMessage(watermill.NewUUID(), newMsg.Data)
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user