Documentation
¶
Overview ¶
Package notification provides a unified abstraction for sending notifications across multiple channels (email, SMS, Slack, push, etc.).
Usage:
type WelcomeNotification struct {
User schema.User
}
func (n *WelcomeNotification) Via() []string { return []string{"mail"} }
func (n *WelcomeNotification) ToMail() *mail.Message {
return mail.NewMessage().
To(n.User.Email).
Subject("Welcome to Astra!").
Text("Hi " + n.User.Name + ", welcome!")
}
// Send:
notifier.Send(ctx, &WelcomeNotification{User: user})
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Channel ¶
type Channel interface {
// Name returns the channel identifier (e.g. "mail", "sms").
Name() string
// Send delivers the notification.
Send(ctx context.Context, n Notification) error
}
Channel is a pluggable delivery backend.
type DatabaseChannel ¶
type DatabaseChannel struct {
// contains filtered or unexported fields
}
DatabaseChannel persists notifications to a database via a user-supplied writer.
func NewDatabaseChannel ¶
func NewDatabaseChannel(writer DatabaseWriter) *DatabaseChannel
NewDatabaseChannel creates a DatabaseChannel.
func (*DatabaseChannel) Name ¶
func (c *DatabaseChannel) Name() string
func (*DatabaseChannel) Send ¶
func (c *DatabaseChannel) Send(ctx context.Context, n Notification) error
type DatabaseNotification ¶
type DatabaseNotification interface {
Notification
// ToDatabase returns the data to store in the notifications table.
ToDatabase() map[string]any
}
DatabaseNotification is implemented by notifications that persist to a DB.
type DatabaseWriter ¶
DatabaseWriter is implemented by the application's notification repository.
type MailChannel ¶
type MailChannel struct {
// contains filtered or unexported fields
}
MailChannel delivers notifications over email.
func NewMailChannel ¶
func NewMailChannel(mailer mail.Mailer) *MailChannel
NewMailChannel creates a MailChannel backed by the given Mailer.
func (*MailChannel) Name ¶
func (c *MailChannel) Name() string
func (*MailChannel) Send ¶
func (c *MailChannel) Send(ctx context.Context, n Notification) error
type MailableNotification ¶
type MailableNotification interface {
Notification
ToMail() *mail.Message
}
MailableNotification is implemented by notifications that send an email.
type Notification ¶
type Notification interface {
Via() []string
}
Notification is the interface every notification must implement. Via() returns the list of channels to send on (e.g. "mail", "sms", "slack").
type Notifier ¶
type Notifier struct {
// contains filtered or unexported fields
}
Notifier is the central notification dispatcher. Register channels via AddChannel, then call Send.
Example:
n := notification.New()
n.AddChannel(notification.NewMailChannel(mailer))
n.Send(ctx, &WelcomeNotification{User: user})
func (*Notifier) AddChannel ¶
AddChannel registers a delivery channel.