service backend
All checks were successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/nats Pipeline was successful

This commit is contained in:
Stephan D
2025-11-07 18:35:26 +01:00
parent 20e8f9acc4
commit 62a6631b9a
537 changed files with 48453 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
package messaging
import (
me "github.com/tech/sendico/pkg/messaging/envelope"
"github.com/tech/sendico/pkg/model"
)
type Broker interface {
Publish(envelope me.Envelope) error
Subscribe(event model.NotificationEvent) (<-chan me.Envelope, error)
Unsubscribe(event model.NotificationEvent, subChan <-chan me.Envelope) error
}

View File

@@ -0,0 +1,14 @@
package messaging
import "github.com/tech/sendico/pkg/model"
type BrokerBus string
const (
BBInProcess BrokerBus = "in-process"
BBNats BrokerBus = "NATS"
)
type (
Config = model.DriverConfig[BrokerBus]
)

View File

@@ -0,0 +1,6 @@
package messaging
type Consumer interface {
ConsumeMessages(handleFunc MessageHandlerT) error
Close()
}

View File

@@ -0,0 +1,28 @@
package messaging
import (
"time"
"github.com/google/uuid"
messagingimp "github.com/tech/sendico/pkg/messaging/internal/envelope"
md "github.com/tech/sendico/pkg/messaging/message"
"github.com/tech/sendico/pkg/model"
)
type Envelope interface {
md.Message
GetTimeStamp() time.Time
GetMessageId() uuid.UUID
GetData() []byte
GetSender() string
GetSignature() model.NotificationEvent
Wrap([]byte) ([]byte, error)
}
func Deserialize(data []byte) (Envelope, error) {
return messagingimp.DeserializeImp(data)
}
func CreateEnvelope(sender string, event model.NotificationEvent) Envelope {
return messagingimp.CreateEnvelopeImp(sender, event)
}

View File

@@ -0,0 +1,19 @@
package messaging
import (
"github.com/tech/sendico/pkg/merrors"
mb "github.com/tech/sendico/pkg/messaging/broker"
mbip "github.com/tech/sendico/pkg/messaging/inprocess"
mbn "github.com/tech/sendico/pkg/messaging/natsb"
"github.com/tech/sendico/pkg/mlogger"
)
func CreateMessagingBroker(logger mlogger.Logger, config *Config) (mb.Broker, error) {
if config.Driver == BBInProcess {
return mbip.NewInProcessBroker(logger, config.Settings)
}
if config.Driver == BBNats {
return mbn.NewNATSBroker(logger, config.Settings)
}
return nil, merrors.InvalidArgument("Unknown messaging broker type: " + string(config.Driver))
}

View File

@@ -0,0 +1,9 @@
package messaging
import (
"context"
me "github.com/tech/sendico/pkg/messaging/envelope"
)
type MessageHandlerT = func(ctx context.Context, envelope me.Envelope) error

View File

@@ -0,0 +1,18 @@
package messaging
import (
mb "github.com/tech/sendico/pkg/messaging/broker"
"github.com/tech/sendico/pkg/messaging/internal/inprocess"
ipc "github.com/tech/sendico/pkg/messaging/internal/inprocess/config"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/model"
"github.com/mitchellh/mapstructure"
)
func NewInProcessBroker(logger mlogger.Logger, config model.SettingsT) (mb.Broker, error) {
var conf ipc.MessagingConfig
if err := mapstructure.Decode(config, &conf); err != nil {
return nil, err
}
return inprocess.NewInProcessBroker(logger, conf.BufferSize)
}

1
api/pkg/messaging/internal/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
generated

View File

@@ -0,0 +1,101 @@
package messagingimp
import (
"time"
"github.com/google/uuid"
"github.com/tech/sendico/pkg/merrors"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
"github.com/tech/sendico/pkg/model"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
type EnvelopeImp struct {
uid uuid.UUID
dateTime time.Time
data []byte
sender string
signature model.NotificationEvent
}
func (e *EnvelopeImp) GetTimeStamp() time.Time {
return e.dateTime
}
func (e *EnvelopeImp) GetMessageId() uuid.UUID {
return e.uid
}
func (e *EnvelopeImp) GetSender() string {
return e.sender
}
func (e *EnvelopeImp) GetData() []byte {
return e.data
}
func (e *EnvelopeImp) GetSignature() model.NotificationEvent {
return e.signature
}
func (e *EnvelopeImp) Serialize() ([]byte, error) {
if e.data == nil {
return nil, merrors.Internal("Envelope data is not initialized")
}
msg := gmessaging.Envelope{
Event: &gmessaging.NotificationEvent{
Type: e.signature.StringType(),
Action: e.signature.StringAction(),
},
MessageData: e.data,
Metadata: &gmessaging.EventMetadata{
MessageId: e.uid.String(),
Sender: e.sender,
Timestamp: timestamppb.New(e.dateTime),
},
}
return proto.Marshal(&msg)
}
func (e *EnvelopeImp) Wrap(data []byte) ([]byte, error) {
e.data = data
return e.Serialize()
}
func DeserializeImp(data []byte) (*EnvelopeImp, error) {
var envelope gmessaging.Envelope
if err := proto.Unmarshal(data, &envelope); err != nil {
return nil, err
}
var e EnvelopeImp
var err error
if e.uid, err = uuid.Parse(envelope.Metadata.MessageId); err != nil {
return nil, err
}
if envelope.Metadata.Timestamp != nil {
e.dateTime = envelope.Metadata.Timestamp.AsTime()
} else {
e.dateTime = time.Now()
}
if e.signature, err = model.StringToNotificationEvent(envelope.Event.Type, envelope.Event.Action); err != nil {
return nil, err
}
e.data = envelope.MessageData
e.sender = envelope.Metadata.Sender
return &e, nil
}
func CreateEnvelopeImp(sender string, signature model.NotificationEvent) *EnvelopeImp {
return &EnvelopeImp{
dateTime: time.Now(),
sender: sender,
uid: uuid.New(),
signature: signature,
}
}

View File

@@ -0,0 +1,87 @@
package inprocess
import (
"fmt"
"sync"
"github.com/tech/sendico/pkg/merrors"
me "github.com/tech/sendico/pkg/messaging/envelope"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/model"
"github.com/tech/sendico/pkg/mutil/mzap"
"go.uber.org/zap"
)
type MessageBroker struct {
logger mlogger.Logger
subscribers map[string][]chan me.Envelope
lock sync.RWMutex
bufferSize int
}
func (b *MessageBroker) Publish(envelope me.Envelope) error {
topic := envelope.GetSignature().ToString()
b.logger.Debug("Publishing message", mzap.Envelope(envelope))
b.lock.RLock()
defer b.lock.RUnlock()
if subs, ok := b.subscribers[topic]; ok {
for _, sub := range subs {
select {
case sub <- envelope:
default:
}
}
return nil
}
b.logger.Warn("Topic not found", mzap.Envelope(envelope))
return merrors.NoMessagingTopic(topic)
}
func (b *MessageBroker) Subscribe(event model.NotificationEvent) (<-chan me.Envelope, error) {
topic := event.ToString()
b.logger.Info("New topic subscriber", zap.String("topic", topic))
ch := make(chan me.Envelope, b.bufferSize) // Buffered channel to avoid blocking publishers
{
b.lock.Lock()
defer b.lock.Unlock()
b.subscribers[topic] = append(b.subscribers[topic], ch)
}
return ch, nil
}
func (b *MessageBroker) Unsubscribe(event model.NotificationEvent, subChan <-chan me.Envelope) error {
topic := event.ToString()
b.logger.Info("Unsubscribing topic", zap.String("topic", topic))
b.lock.Lock()
defer b.lock.Unlock()
subs, ok := b.subscribers[topic]
if !ok {
b.logger.Debug("No subscribers for topic", zap.String("topic", topic))
return nil
}
for i, ch := range subs {
if ch == subChan {
b.subscribers[topic] = append(subs[:i], subs[i+1:]...)
close(ch)
return nil
}
}
b.logger.Warn("No topic found", zap.String("topic", topic))
return merrors.NoMessagingTopic(topic)
}
func NewInProcessBroker(logger mlogger.Logger, bufferSize int) (*MessageBroker, error) {
if bufferSize < 1 {
return nil, merrors.InvalidArgument(fmt.Sprintf("Invelid buffer size %d. It must be greater than 1", bufferSize))
}
logger.Info("Created in-process logger", zap.Int("buffer_size", bufferSize))
return &MessageBroker{
logger: logger.Named("in_process"),
subscribers: make(map[string][]chan me.Envelope),
bufferSize: bufferSize,
}, nil
}

View File

@@ -0,0 +1,5 @@
package inprocess
type MessagingConfig struct {
BufferSize int `mapstructure:"buffer_size" yaml:"buffer_size"`
}

View File

@@ -0,0 +1,86 @@
package natsb
import (
me "github.com/tech/sendico/pkg/messaging/envelope"
"github.com/tech/sendico/pkg/model"
"github.com/tech/sendico/pkg/mutil/mzap"
"go.uber.org/zap"
)
func (b *NatsBroker) Publish(envelope me.Envelope) error {
subject := envelope.GetSignature().ToString()
b.logger.Debug("Publishing message", mzap.Envelope(envelope))
// Serialize the message
data, err := envelope.Serialize()
if err != nil {
b.logger.Error("Failed to serialize message", zap.Error(err), mzap.Envelope(envelope))
return err
}
if err := b.nc.Publish(subject, data); err != nil {
b.logger.Error("Error publishing message", zap.Error(err), mzap.Envelope(envelope))
return err
}
b.logger.Debug("Message published", zap.String("subject", subject))
return nil
}
// Subscribe subscribes to a NATS subject and returns a channel for messages
func (b *NatsBroker) Subscribe(event model.NotificationEvent) (<-chan me.Envelope, error) {
subject := event.ToString()
b.logger.Info("Subscribing to subject", zap.String("subject", subject))
// Create a bidirectional channel to send messages to
messageChan := make(chan me.Envelope)
b.mu.Lock()
defer b.mu.Unlock()
topicSub, exists := b.topicSubs[subject]
if !exists {
var err error
topicSub, err = NewTopicSubscription(b.logger, b.nc, subject)
if err != nil {
return nil, err
}
b.topicSubs[subject] = topicSub
}
// Add the consumer's channel to the topic subscription
topicSub.AddConsumer(messageChan)
// Return the channel as a receive-only channel
return messageChan, nil
}
// Unsubscribe unsubscribes a consumer from a NATS subject
func (b *NatsBroker) Unsubscribe(event model.NotificationEvent, messageChan <-chan me.Envelope) error {
subject := event.ToString()
b.logger.Info("Unsubscribing from subject", zap.String("subject", subject))
b.mu.Lock()
topicSub, exists := b.topicSubs[subject]
b.mu.Unlock()
if !exists {
b.logger.Warn("No subscription found for subject", zap.String("subject", subject))
return nil
}
// Remove the consumer's channel from the topic subscription
topicSub.RemoveConsumer(messageChan)
if !topicSub.HasConsumers() {
if err := topicSub.Unsubscribe(); err != nil {
b.logger.Error("Error unsubscribing from subject", zap.String("subject", subject), zap.Error(err))
return err
}
b.mu.Lock()
delete(b.topicSubs, subject)
b.mu.Unlock()
}
b.logger.Info("Unsubscribed from subject", zap.String("subject", subject))
return nil
}

View File

@@ -0,0 +1,113 @@
package natsb
import (
"fmt"
"net"
"net/url"
"os"
"strconv"
"sync"
"time"
"github.com/nats-io/nats.go"
"github.com/tech/sendico/pkg/merrors"
nc "github.com/tech/sendico/pkg/messaging/internal/natsb/config"
"github.com/tech/sendico/pkg/mlogger"
"go.uber.org/zap"
)
type natsSubscriotions = map[string]*TopicSubscription
type NatsBroker struct {
nc *nats.Conn
logger *zap.Logger
topicSubs natsSubscriotions
mu sync.Mutex
}
type envConfig struct {
User, Password, Host string
Port int
}
// loadEnv gathers and validates connection details from environment variables
// listed in the Settings struct. Invalid or missing values surface as a typed
// InvalidArgument error so callers can decide how to handle them.
func loadEnv(settings *nc.Settings, l *zap.Logger) (*envConfig, error) {
get := func(key, label string) (string, error) {
if v := os.Getenv(key); v != "" {
return v, nil
}
l.Error(fmt.Sprintf("NATS %s not found in environment", label), zap.String("env_var", key))
return "", merrors.InvalidArgument(fmt.Sprintf("NATS %s not found in environment variable: %s", label, key))
}
user, err := get(settings.UsernameEnv, "user name")
if err != nil {
return nil, err
}
password, err := get(settings.PasswordEnv, "password")
if err != nil {
return nil, err
}
host, err := get(settings.HostEnv, "host")
if err != nil {
return nil, err
}
portStr, err := get(settings.PortEnv, "port")
if err != nil {
return nil, err
}
port, err := strconv.Atoi(portStr)
if err != nil || port <= 0 || port > 65535 {
l.Error("Invalid NATS port value", zap.String("port", portStr))
return nil, merrors.InvalidArgument("Invalid NATS port: " + portStr)
}
return &envConfig{
User: user,
Password: password,
Host: host,
Port: port,
}, nil
}
func NewNatsBroker(logger mlogger.Logger, settings *nc.Settings) (*NatsBroker, error) {
l := logger.Named("broker")
// Helper function to get environment variables
cfg, err := loadEnv(settings, l)
if err != nil {
return nil, err
}
u := &url.URL{
Scheme: "nats",
Host: net.JoinHostPort(cfg.Host, strconv.Itoa(cfg.Port)),
}
natsURL := u.String()
opts := []nats.Option{
nats.Name(settings.NATSName),
nats.MaxReconnects(settings.MaxReconnects),
nats.ReconnectWait(time.Duration(settings.ReconnectWait) * time.Second),
nats.UserInfo(cfg.User, cfg.Password),
}
res := &NatsBroker{
logger: l.Named("nats"),
topicSubs: natsSubscriotions{},
}
if res.nc, err = nats.Connect(natsURL, opts...); err != nil {
l.Error("Failed to connect to NATS", zap.String("url", natsURL), zap.Error(err))
return nil, err
}
logger.Info("Connected to NATS", zap.String("broker", settings.NATSName),
zap.String("url", fmt.Sprintf("nats://%s@%s", cfg.User, net.JoinHostPort(cfg.Host, strconv.Itoa(cfg.Port)))))
return res, nil
}

View File

@@ -0,0 +1,12 @@
package natsb
type Settings struct {
URLEnv string `mapstructure:"url_env" yaml:"url_env"`
HostEnv string `mapstructure:"host_env" yaml:"host_env"`
PortEnv string `mapstructure:"port_env" yaml:"port_env"`
UsernameEnv string `mapstructure:"username_env" yaml:"username_env"`
PasswordEnv string `mapstructure:"password_env" yaml:"password_env"`
NATSName string `mapstructure:"broker_name" yaml:"broker_name"`
MaxReconnects int `mapstructure:"max_reconnects" yaml:"max_reconnects"`
ReconnectWait int `mapstructure:"reconnect_wait" yaml:"reconnect_wait"`
}

View File

@@ -0,0 +1,78 @@
package natsb
import (
"sync"
"github.com/nats-io/nats.go"
me "github.com/tech/sendico/pkg/messaging/envelope"
"github.com/tech/sendico/pkg/mlogger"
"go.uber.org/zap"
)
type TopicSubscription struct {
sub *nats.Subscription
consumers map[<-chan me.Envelope]chan me.Envelope
mu sync.Mutex
logger mlogger.Logger
}
func NewTopicSubscription(logger mlogger.Logger, nc *nats.Conn, subject string) (*TopicSubscription, error) {
ts := &TopicSubscription{
consumers: make(map[<-chan me.Envelope]chan me.Envelope),
logger: logger.Named(subject),
}
sub, err := nc.Subscribe(subject, ts.handleMessage)
if err != nil {
logger.Error("Error subscribing to subject", zap.String("subject", subject), zap.Error(err))
return nil, err
}
ts.sub = sub
return ts, nil
}
func (ts *TopicSubscription) handleMessage(m *nats.Msg) {
ts.logger.Debug("Received message", zap.String("subject", m.Subject))
envelope, err := me.Deserialize(m.Data)
if err != nil {
ts.logger.Warn("Failed to deserialize message", zap.String("subject", m.Subject), zap.Error(err))
return // Do not push invalid data to the channels
}
ts.mu.Lock()
defer ts.mu.Unlock()
for _, c := range ts.consumers {
select {
case c <- envelope:
default:
ts.logger.Warn("Consumer is slow or not receiving messages", zap.String("subject", m.Subject))
}
}
}
func (ts *TopicSubscription) AddConsumer(messageChan chan me.Envelope) {
ts.mu.Lock()
ts.consumers[messageChan] = messageChan
ts.mu.Unlock()
}
func (ts *TopicSubscription) RemoveConsumer(messageChan <-chan me.Envelope) {
ts.mu.Lock()
if c, ok := ts.consumers[messageChan]; ok {
delete(ts.consumers, messageChan)
close(c)
}
ts.mu.Unlock()
}
func (ts *TopicSubscription) HasConsumers() bool {
ts.mu.Lock()
defer ts.mu.Unlock()
return len(ts.consumers) > 0
}
func (ts *TopicSubscription) Unsubscribe() error {
return ts.sub.Drain()
}

View File

@@ -0,0 +1,37 @@
package notifications
import (
messaging "github.com/tech/sendico/pkg/messaging/envelope"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
"github.com/tech/sendico/pkg/model"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
"go.mongodb.org/mongo-driver/bson/primitive"
"google.golang.org/protobuf/proto"
)
type AccountNotification struct {
messaging.Envelope
accountRef primitive.ObjectID
}
func (acn *AccountNotification) Serialize() ([]byte, error) {
var msg gmessaging.AccountCreatedEvent
msg.AccountRef = acn.accountRef.Hex()
data, err := proto.Marshal(&msg)
if err != nil {
return nil, err
}
return acn.Envelope.Wrap(data)
}
func NewAccountNotification(action nm.NotificationAction) model.NotificationEvent {
return model.NewNotification(mservice.Accounts, action)
}
func NewAccountImp(sender string, accountRef primitive.ObjectID, action nm.NotificationAction) messaging.Envelope {
return &AccountNotification{
Envelope: messaging.CreateEnvelope(sender, NewAccountNotification(action)),
accountRef: accountRef,
}
}

View File

@@ -0,0 +1,40 @@
package notifications
import (
messaging "github.com/tech/sendico/pkg/messaging/envelope"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
"github.com/tech/sendico/pkg/model"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
"go.mongodb.org/mongo-driver/bson/primitive"
"google.golang.org/protobuf/proto"
)
type PasswordResetNotification struct {
messaging.Envelope
accountRef primitive.ObjectID
resetToken string
}
func (prn *PasswordResetNotification) Serialize() ([]byte, error) {
var msg gmessaging.PasswordResetEvent
msg.AccountRef = prn.accountRef.Hex()
msg.ResetToken = prn.resetToken
data, err := proto.Marshal(&msg)
if err != nil {
return nil, err
}
return prn.Envelope.Wrap(data)
}
func NewPasswordResetNotification(action nm.NotificationAction) model.NotificationEvent {
return model.NewNotification(mservice.Accounts, action)
}
func NewPasswordResetImp(sender string, accountRef primitive.ObjectID, resetToken string, action nm.NotificationAction) messaging.Envelope {
return &PasswordResetNotification{
Envelope: messaging.CreateEnvelope(sender, NewPasswordResetNotification(action)),
accountRef: accountRef,
resetToken: resetToken,
}
}

View File

@@ -0,0 +1,57 @@
package notifications
import (
"context"
"github.com/tech/sendico/pkg/db/account"
me "github.com/tech/sendico/pkg/messaging/envelope"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
mah "github.com/tech/sendico/pkg/messaging/notifications/account/handler"
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/model"
nm "github.com/tech/sendico/pkg/model/notification"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
type PasswordResetNotificationProcessor struct {
logger mlogger.Logger
handler mah.PasswordResetHandler
db account.DB
event model.NotificationEvent
}
func (prnp *PasswordResetNotificationProcessor) Process(ctx context.Context, envelope me.Envelope) error {
var msg gmessaging.PasswordResetEvent
if err := proto.Unmarshal(envelope.GetData(), &msg); err != nil {
prnp.logger.Warn("Failed to unmarshall envelope", zap.Error(err), zap.String("topic", prnp.event.ToString()))
return err
}
accountRef, err := primitive.ObjectIDFromHex(msg.AccountRef)
if err != nil {
prnp.logger.Warn("Failed to restore object ID", zap.Error(err), zap.String("topic", prnp.event.ToString()), zap.String("account_ref", msg.AccountRef))
return err
}
var account model.Account
if err := prnp.db.Get(ctx, accountRef, &account); err != nil {
prnp.logger.Warn("Failed to fetch account", zap.Error(err), zap.String("topic", prnp.event.ToString()), zap.String("account_ref", msg.AccountRef))
return err
}
return prnp.handler(ctx, &account, msg.ResetToken)
}
func (prnp *PasswordResetNotificationProcessor) GetSubject() model.NotificationEvent {
return prnp.event
}
func NewPasswordResetMessageProcessor(logger mlogger.Logger, handler mah.PasswordResetHandler, db account.DB, action nm.NotificationAction) np.EnvelopeProcessor {
event := NewPasswordResetNotification(action)
return &PasswordResetNotificationProcessor{
logger: logger.Named("password_reset_processor"),
handler: handler,
db: db,
event: event,
}
}

View File

@@ -0,0 +1,57 @@
package notifications
import (
"context"
"github.com/tech/sendico/pkg/db/account"
me "github.com/tech/sendico/pkg/messaging/envelope"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
mah "github.com/tech/sendico/pkg/messaging/notifications/account/handler"
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/model"
nm "github.com/tech/sendico/pkg/model/notification"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
type AccoountNotificaionProcessor struct {
logger mlogger.Logger
handler mah.AccountHandler
db account.DB
event model.NotificationEvent
}
func (acnp *AccoountNotificaionProcessor) Process(ctx context.Context, envelope me.Envelope) error {
var msg gmessaging.AccountCreatedEvent
if err := proto.Unmarshal(envelope.GetData(), &msg); err != nil {
acnp.logger.Warn("Failed to unmarshall envelope", zap.Error(err), zap.String("topic", acnp.event.ToString()))
return err
}
accountRef, err := primitive.ObjectIDFromHex(msg.AccountRef)
if err != nil {
acnp.logger.Warn("Failed to restore object ID", zap.Error(err), zap.String("topic", acnp.event.ToString()), zap.String("account_ref", msg.AccountRef))
return err
}
var account model.Account
if err := acnp.db.Get(ctx, accountRef, &account); err != nil {
acnp.logger.Warn("Failed to fetch account", zap.Error(err), zap.String("topic", acnp.event.ToString()), zap.String("account_ref", msg.AccountRef))
return err
}
return acnp.handler(ctx, &account)
}
func (acnp *AccoountNotificaionProcessor) GetSubject() model.NotificationEvent {
return acnp.event
}
func NewAccountMessageProcessor(logger mlogger.Logger, handler mah.AccountHandler, db account.DB, action nm.NotificationAction) np.EnvelopeProcessor {
event := NewAccountNotification(action)
return &AccoountNotificaionProcessor{
logger: logger.Named("message_processor"),
handler: handler,
db: db,
event: event,
}
}

View File

@@ -0,0 +1,63 @@
package notifications
import (
"context"
"github.com/tech/sendico/pkg/db/account"
"github.com/tech/sendico/pkg/db/invitation"
mih "github.com/tech/sendico/pkg/messaging/notifications/invitation/handler"
no "github.com/tech/sendico/pkg/messaging/notifications/object"
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/model"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
"github.com/tech/sendico/pkg/mutil/mzap"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
)
type InvitationNotificaionProcessor struct {
np.EnvelopeProcessor
logger mlogger.Logger
handler mih.InvitationHandler
db invitation.DB
adb account.DB
}
func (ianp *InvitationNotificaionProcessor) onInvitation(
ctx context.Context,
objectType mservice.Type,
objectRef, actorAccountRef primitive.ObjectID,
action nm.NotificationAction,
) error {
var invitation model.Invitation
if err := ianp.db.Unprotected().Get(ctx, objectRef, &invitation); err != nil {
ianp.logger.Warn("Failed to fetch invitation object", zap.Error(err), mzap.ObjRef("object_ref", objectRef))
return err
}
var account model.Account
if err := ianp.adb.Get(ctx, actorAccountRef, &account); err != nil {
ianp.logger.Warn("Failed to fetch actor account", zap.Error(err), mzap.ObjRef("actor_account_ref", actorAccountRef))
return err
}
return ianp.handler(ctx, &account, &invitation)
}
func NewInvitationMessageProcessor(
logger mlogger.Logger,
handler mih.InvitationHandler,
db invitation.DB,
adb account.DB,
action nm.NotificationAction,
) np.EnvelopeProcessor {
l := logger.Named(mservice.Invitations)
res := &InvitationNotificaionProcessor{
logger: l,
db: db,
adb: adb,
handler: handler,
}
res.EnvelopeProcessor = no.NewObjectChangedMessageProcessor(l, mservice.Invitations, action, res.onInvitation)
return res
}

View File

@@ -0,0 +1,44 @@
package notifications
import (
messaging "github.com/tech/sendico/pkg/messaging/envelope"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
"github.com/tech/sendico/pkg/model"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
"google.golang.org/protobuf/proto"
)
type NResultNotification struct {
messaging.Envelope
result *model.NotificationResult
}
func (nrn *NResultNotification) Serialize() ([]byte, error) {
msg := gmessaging.NotificationSentEvent{
UserID: nrn.result.UserID,
Channel: nrn.result.Channel,
Locale: nrn.result.Locale,
TemplateID: nrn.result.TemplateID,
Status: &gmessaging.OperationResult{
IsSuccessful: nrn.result.Result.IsSuccessful,
ErrorDescription: nrn.result.Result.Error,
},
}
data, err := proto.Marshal(&msg)
if err != nil {
return nil, err
}
return nrn.Envelope.Wrap(data)
}
func NewNRNotification() model.NotificationEvent {
return model.NewNotification(mservice.Notifications, nm.NASent)
}
func NewNResultNotification(sender string, result *model.NotificationResult) messaging.Envelope {
return &NResultNotification{
Envelope: messaging.CreateEnvelope(sender, NewNRNotification()),
result: result,
}
}

View File

@@ -0,0 +1,53 @@
package notifications
import (
"context"
me "github.com/tech/sendico/pkg/messaging/envelope"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
nh "github.com/tech/sendico/pkg/messaging/notifications/notification/handler"
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/model"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
type NResultNotificaionProcessor struct {
logger mlogger.Logger
handler nh.NResultHandler
event model.NotificationEvent
}
func (nrp *NResultNotificaionProcessor) Process(ctx context.Context, envelope me.Envelope) error {
var msg gmessaging.NotificationSentEvent
if err := proto.Unmarshal(envelope.GetData(), &msg); err != nil {
nrp.logger.Warn("Failed to unmarshall envelope", zap.Error(err), zap.String("topic", nrp.event.ToString()))
return err
}
nresult := &model.NotificationResult{
AmpliEvent: model.AmpliEvent{
UserID: msg.UserID,
},
Channel: msg.Channel,
TemplateID: msg.TemplateID,
Locale: msg.Locale,
Result: model.OperationResult{
IsSuccessful: msg.Status.IsSuccessful,
Error: msg.Status.ErrorDescription,
},
}
return nrp.handler(ctx, nresult)
}
func (nrp *NResultNotificaionProcessor) GetSubject() model.NotificationEvent {
return nrp.event
}
func NewAccountMessageProcessor(logger mlogger.Logger, handler nh.NResultHandler) np.EnvelopeProcessor {
return &NResultNotificaionProcessor{
logger: logger.Named("message_processor"),
handler: handler,
event: NewNRNotification(),
}
}

View File

@@ -0,0 +1,46 @@
package notifications
import (
messaging "github.com/tech/sendico/pkg/messaging/envelope"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
"github.com/tech/sendico/pkg/model"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
"go.mongodb.org/mongo-driver/bson/primitive"
"google.golang.org/protobuf/proto"
)
type ObjectNotification struct {
messaging.Envelope
actorAccountRef primitive.ObjectID
objectRef primitive.ObjectID
}
func (acn *ObjectNotification) Serialize() ([]byte, error) {
var msg gmessaging.ObjectUpdatedEvent
msg.ActorAccountRef = acn.actorAccountRef.Hex()
msg.ObjectRef = acn.objectRef.Hex()
data, err := proto.Marshal(&msg)
if err != nil {
return nil, err
}
return acn.Envelope.Wrap(data)
}
func NewObjectNotification(t mservice.Type, action nm.NotificationAction) model.NotificationEvent {
return model.NewNotification(t, action)
}
func NewObjectImp(
sender string,
actorAccountRef primitive.ObjectID,
objectType mservice.Type,
objectRef primitive.ObjectID,
action nm.NotificationAction,
) messaging.Envelope {
return &ObjectNotification{
Envelope: messaging.CreateEnvelope(sender, NewObjectNotification(objectType, action)),
actorAccountRef: actorAccountRef,
objectRef: objectRef,
}
}

View File

@@ -0,0 +1,55 @@
package notifications
import (
"context"
me "github.com/tech/sendico/pkg/messaging/envelope"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
moh "github.com/tech/sendico/pkg/messaging/notifications/object/handler"
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/model"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
type ObjectNotificaionProcessor struct {
logger mlogger.Logger
handler moh.ObjectUpdateHandler
event model.NotificationEvent
}
func (ounp *ObjectNotificaionProcessor) Process(ctx context.Context, envelope me.Envelope) error {
var msg gmessaging.ObjectUpdatedEvent
if err := proto.Unmarshal(envelope.GetData(), &msg); err != nil {
ounp.logger.Warn("Failed to unmarshall envelope", zap.Error(err), zap.String("topic", ounp.event.ToString()))
return err
}
actorAccountRef, err := primitive.ObjectIDFromHex(msg.ActorAccountRef)
if err != nil {
ounp.logger.Warn("Failed to restore actor account reference", zap.Error(err), zap.String("topic", ounp.event.ToString()), zap.String("actor_account_ref", msg.ActorAccountRef))
return err
}
objectRef, err := primitive.ObjectIDFromHex(msg.ObjectRef)
if err != nil {
ounp.logger.Warn("Failed to restore object reference", zap.Error(err), zap.String("topic", ounp.event.ToString()), zap.String("object_ref", msg.ObjectRef))
return err
}
return ounp.handler(ctx, envelope.GetSignature().GetType(), objectRef, actorAccountRef, envelope.GetSignature().GetAction())
}
func (acnp *ObjectNotificaionProcessor) GetSubject() model.NotificationEvent {
return acnp.event
}
func NewObjectChangeMessageProcessor(logger mlogger.Logger, handler moh.ObjectUpdateHandler, objectType mservice.Type, action nm.NotificationAction) np.EnvelopeProcessor {
return &ObjectNotificaionProcessor{
logger: logger.Named("message_processor"),
handler: handler,
event: NewObjectNotification(objectType, action),
}
}

View File

@@ -0,0 +1,26 @@
package messagingimp
import (
mb "github.com/tech/sendico/pkg/messaging/broker"
me "github.com/tech/sendico/pkg/messaging/envelope"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/mutil/mzap"
"go.uber.org/zap"
)
type ChannelProducer struct {
logger mlogger.Logger
broker mb.Broker
}
func (p *ChannelProducer) SendMessage(envelope me.Envelope) error {
// TODO: won't work with Kafka, need to serialize/deserialize
if err := p.broker.Publish(envelope); err != nil {
p.logger.Warn("Failed to publish message", zap.Error(err), mzap.Envelope(envelope))
}
return nil
}
func NewProducer(logger mlogger.Logger, broker mb.Broker) *ChannelProducer {
return &ChannelProducer{logger: logger.Named("producer"), broker: broker}
}

View File

@@ -0,0 +1,5 @@
package messaging
type Message = interface {
Serialize() ([]byte, error)
}

View File

@@ -0,0 +1,10 @@
package messaging
import (
notifications "github.com/tech/sendico/pkg/messaging/notifications/processor"
)
type Register interface {
Consumer(processor notifications.EnvelopeProcessor) error
Producer() Producer
}

View File

@@ -0,0 +1,18 @@
package messaging
import (
"github.com/mitchellh/mapstructure"
mb "github.com/tech/sendico/pkg/messaging/broker"
"github.com/tech/sendico/pkg/messaging/internal/natsb"
nc "github.com/tech/sendico/pkg/messaging/internal/natsb/config"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/model"
)
func NewNATSBroker(logger mlogger.Logger, config model.SettingsT) (mb.Broker, error) {
var conf nc.Settings
if err := mapstructure.Decode(config, &conf); err != nil {
return nil, err
}
return natsb.NewNatsBroker(logger, &conf)
}

View File

@@ -0,0 +1,16 @@
package notifications
import (
an "github.com/tech/sendico/pkg/messaging/internal/notifications/account"
messaging "github.com/tech/sendico/pkg/messaging/envelope"
nm "github.com/tech/sendico/pkg/model/notification"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func Account(sender string, accountRef primitive.ObjectID, action nm.NotificationAction) messaging.Envelope {
return an.NewAccountImp(sender, accountRef, action)
}
func AccountCreated(sender string, accountRef primitive.ObjectID) messaging.Envelope {
return Account(sender, accountRef, nm.NACreated)
}

View File

@@ -0,0 +1,11 @@
package notifications
import (
"context"
"github.com/tech/sendico/pkg/model"
)
type AccountHandler = func(context.Context, *model.Account) error
type PasswordResetHandler = func(context.Context, *model.Account, string) error

View File

@@ -0,0 +1,24 @@
package notifications
import (
"github.com/tech/sendico/pkg/db/account"
messaging "github.com/tech/sendico/pkg/messaging/envelope"
an "github.com/tech/sendico/pkg/messaging/internal/notifications/account"
mah "github.com/tech/sendico/pkg/messaging/notifications/account/handler"
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
"github.com/tech/sendico/pkg/mlogger"
nm "github.com/tech/sendico/pkg/model/notification"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func PasswordReset(sender string, accountRef primitive.ObjectID, resetToken string, action nm.NotificationAction) messaging.Envelope {
return an.NewPasswordResetImp(sender, accountRef, resetToken, action)
}
func PasswordResetRequested(sender string, accountRef primitive.ObjectID, resetToken string) messaging.Envelope {
return PasswordReset(sender, accountRef, resetToken, nm.NAPasswordReset)
}
func NewPasswordResetRequestedMessageProcessor(logger mlogger.Logger, db account.DB, handler mah.PasswordResetHandler) np.EnvelopeProcessor {
return an.NewPasswordResetMessageProcessor(logger, handler, db, nm.NAPasswordReset)
}

View File

@@ -0,0 +1,14 @@
package notifications
import (
"github.com/tech/sendico/pkg/db/account"
macp "github.com/tech/sendico/pkg/messaging/internal/notifications/account"
mah "github.com/tech/sendico/pkg/messaging/notifications/account/handler"
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
"github.com/tech/sendico/pkg/mlogger"
nm "github.com/tech/sendico/pkg/model/notification"
)
func NewAccountCreatedMessageProcessor(logger mlogger.Logger, db account.DB, handler mah.AccountHandler) np.EnvelopeProcessor {
return macp.NewAccountMessageProcessor(logger, handler, db, nm.NACreated)
}

View File

@@ -0,0 +1,9 @@
package notifications
import (
"context"
"github.com/tech/sendico/pkg/model"
)
type InvitationHandler = func(context.Context, *model.Account, *model.Invitation) error

View File

@@ -0,0 +1,43 @@
package notifications
import (
messaging "github.com/tech/sendico/pkg/messaging/envelope"
on "github.com/tech/sendico/pkg/messaging/notifications/object"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func InvitationCreated(
sender string,
actorAccountRef primitive.ObjectID,
objectType mservice.Type,
objectRef primitive.ObjectID,
) messaging.Envelope {
return on.ObjectCreated(sender, actorAccountRef, mservice.Invitations, objectRef)
}
func InvitationUpdated(
sender string,
actorAccountRef primitive.ObjectID,
objectRef primitive.ObjectID,
) messaging.Envelope {
return on.ObjectUpdated(sender, actorAccountRef, mservice.Invitations, objectRef)
}
func InvitationDeleted(
sender string,
actorAccountRef primitive.ObjectID,
objectRef primitive.ObjectID,
) messaging.Envelope {
return on.ObjectDeleted(sender, actorAccountRef, mservice.Invitations, objectRef)
}
func Invitation(
sender string,
actorAccountRef primitive.ObjectID,
objectRef primitive.ObjectID,
action nm.NotificationAction,
) messaging.Envelope {
return on.Object(sender, actorAccountRef, mservice.Invitations, objectRef, action)
}

View File

@@ -0,0 +1,30 @@
package notifications
import (
"github.com/tech/sendico/pkg/db/account"
"github.com/tech/sendico/pkg/db/invitation"
micp "github.com/tech/sendico/pkg/messaging/internal/notifications/invitation"
mih "github.com/tech/sendico/pkg/messaging/notifications/invitation/handler"
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
"github.com/tech/sendico/pkg/mlogger"
nm "github.com/tech/sendico/pkg/model/notification"
)
func NewInvitationMessageProcessor(
logger mlogger.Logger,
handler mih.InvitationHandler,
db invitation.DB,
adb account.DB,
action nm.NotificationAction,
) np.EnvelopeProcessor {
return micp.NewInvitationMessageProcessor(logger, handler, db, adb, action)
}
func NewInvitationCreatedProcessor(
logger mlogger.Logger,
handler mih.InvitationHandler,
db invitation.DB,
adb account.DB,
) np.EnvelopeProcessor {
return NewInvitationMessageProcessor(logger, handler, db, adb, nm.NACreated)
}

View File

@@ -0,0 +1,9 @@
package notifications
import (
"context"
"github.com/tech/sendico/pkg/model"
)
type NResultHandler = func(context.Context, *model.NotificationResult) error

View File

@@ -0,0 +1,11 @@
package notifications
import (
messaging "github.com/tech/sendico/pkg/messaging/envelope"
nn "github.com/tech/sendico/pkg/messaging/internal/notifications/notification"
"github.com/tech/sendico/pkg/model"
)
func NotificationSent(sender string, result *model.NotificationResult) messaging.Envelope {
return nn.NewNResultNotification(sender, result)
}

View File

@@ -0,0 +1,16 @@
package notifications
import (
"context"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type ObjectUpdateHandler = func(
ctx context.Context,
objectType mservice.Type,
objectRef, actorAccountRef primitive.ObjectID,
action nm.NotificationAction,
) error

View File

@@ -0,0 +1,46 @@
package notifications
import (
messaging "github.com/tech/sendico/pkg/messaging/envelope"
on "github.com/tech/sendico/pkg/messaging/internal/notifications/object"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func Object(
sender string,
actorAccountRef primitive.ObjectID,
objectType mservice.Type,
objectRef primitive.ObjectID,
action nm.NotificationAction,
) messaging.Envelope {
return on.NewObjectImp(sender, actorAccountRef, objectType, objectRef, action)
}
func ObjectCreated(
sender string,
actorAccountRef primitive.ObjectID,
objectType mservice.Type,
objectRef primitive.ObjectID,
) messaging.Envelope {
return Object(sender, actorAccountRef, objectType, objectRef, nm.NACreated)
}
func ObjectUpdated(
sender string,
actorAccountRef primitive.ObjectID,
objectType mservice.Type,
objectRef primitive.ObjectID,
) messaging.Envelope {
return Object(sender, actorAccountRef, objectType, objectRef, nm.NAUpdated)
}
func ObjectDeleted(
sender string,
actorAccountRef primitive.ObjectID,
objectType mservice.Type,
objectRef primitive.ObjectID,
) messaging.Envelope {
return Object(sender, actorAccountRef, objectType, objectRef, nm.NADeleted)
}

View File

@@ -0,0 +1,19 @@
package notifications
import (
mocp "github.com/tech/sendico/pkg/messaging/internal/notifications/object"
moh "github.com/tech/sendico/pkg/messaging/notifications/object/handler"
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
"github.com/tech/sendico/pkg/mlogger"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
)
func NewObjectChangedMessageProcessor(
logger mlogger.Logger,
objectType mservice.Type,
action nm.NotificationAction,
handler moh.ObjectUpdateHandler,
) np.EnvelopeProcessor {
return mocp.NewObjectChangeMessageProcessor(logger, handler, objectType, action)
}

View File

@@ -0,0 +1,13 @@
package notifications
import (
"context"
me "github.com/tech/sendico/pkg/messaging/envelope"
"github.com/tech/sendico/pkg/model"
)
type EnvelopeProcessor interface {
GetSubject() model.NotificationEvent
Process(ctx context.Context, envelope me.Envelope) error
}

View File

@@ -0,0 +1,7 @@
package messaging
import me "github.com/tech/sendico/pkg/messaging/envelope"
type Producer interface {
SendMessage(envelope me.Envelope) error
}

View File

@@ -0,0 +1,12 @@
package messaging
import (
"github.com/tech/sendico/pkg/messaging"
mb "github.com/tech/sendico/pkg/messaging/broker"
mp "github.com/tech/sendico/pkg/messaging/internal/producer"
"github.com/tech/sendico/pkg/mlogger"
)
func NewProducer(logger mlogger.Logger, broker mb.Broker) messaging.Producer {
return mp.NewProducer(logger, broker)
}