notification

package
v0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Overview

Package notification defines tenant-scoped notification messages, notifier decorators, in-memory delivery, SMTP email delivery, Amazon SES API v2 delivery, Resend email API delivery, and webhook delivery.

Index

Constants

View Source
const (
	// ChannelWebhook is the conventional notification channel for webhook delivery.
	ChannelWebhook = "webhook"

	// WebhookSignatureHeader is the default HMAC signature header.
	WebhookSignatureHeader = "X-GoTenancy-Signature"

	// WebhookTimestampHeader is the default HMAC timestamp header.
	WebhookTimestampHeader = "X-GoTenancy-Timestamp"
)
View Source
const (
	// ChannelEmail is the conventional notification channel for email delivery.
	ChannelEmail = "email"
)

Variables

View Source
var (
	ErrInvalidMessage        = errors.New("gotenancy/notification: invalid message")
	ErrInvalidNotifierConfig = errors.New("gotenancy/notification: invalid notifier config")
	ErrInvalidResendConfig   = errors.New("gotenancy/notification: invalid resend config")
	ErrInvalidSESConfig      = errors.New("gotenancy/notification: invalid ses config")
	ErrInvalidSMTPConfig     = errors.New("gotenancy/notification: invalid smtp config")
	ErrInvalidWebhookConfig  = errors.New("gotenancy/notification: invalid webhook config")
	ErrNilNotifier           = errors.New("gotenancy/notification: nil notifier")
	ErrResendDelivery        = errors.New("gotenancy/notification: resend delivery failed")
	ErrSESDelivery           = errors.New("gotenancy/notification: ses delivery failed")
	ErrUnsupportedChannel    = errors.New("gotenancy/notification: unsupported channel")
	ErrWebhookDelivery       = errors.New("gotenancy/notification: webhook delivery failed")
	ErrTLSRequired           = errors.New("gotenancy/notification: tls required")
)

Functions

func DefaultRetryIf added in v0.1.6

func DefaultRetryIf(err error) bool

DefaultRetryIf reports whether err should be retried by default.

Types

type ChannelRouter added in v0.1.6

type ChannelRouter struct {
	// contains filtered or unexported fields
}

ChannelRouter dispatches messages to a notifier registered for message.Channel.

func NewChannelRouter added in v0.1.6

func NewChannelRouter(routes map[string]Notifier) (*ChannelRouter, error)

NewChannelRouter creates a channel-based router.

func (*ChannelRouter) Send added in v0.1.6

func (router *ChannelRouter) Send(ctx context.Context, message Message) error

Send dispatches message to its channel notifier.

type FanoutNotifier added in v0.1.6

type FanoutNotifier struct {
	// contains filtered or unexported fields
}

FanoutNotifier sends each message to multiple notifiers in order.

func NewFanoutNotifier added in v0.1.6

func NewFanoutNotifier(notifiers ...Notifier) (*FanoutNotifier, error)

NewFanoutNotifier creates a sequential fanout notifier.

func (*FanoutNotifier) Send added in v0.1.6

func (notifier *FanoutNotifier) Send(ctx context.Context, message Message) error

Send delivers message to all configured notifiers and joins delivery errors.

type MemoryNotifier

type MemoryNotifier struct {
	// contains filtered or unexported fields
}

func NewMemoryNotifier

func NewMemoryNotifier() *MemoryNotifier

func (*MemoryNotifier) Messages

func (notifier *MemoryNotifier) Messages() []Message

func (*MemoryNotifier) Send

func (notifier *MemoryNotifier) Send(ctx context.Context, message Message) error

type Message

type Message struct {
	ID       string
	TenantID types.TenantID
	Channel  string
	To       string
	Subject  string
	Body     string
	Metadata map[string]string
	Tags     map[string]string
}

func (Message) Clone added in v0.1.6

func (message Message) Clone() Message

Clone returns a deep copy of message metadata and provider-visible tags.

func (Message) Validate added in v0.1.6

func (message Message) Validate() error

Validate verifies the fields required by all notification senders.

type Notifier

type Notifier interface {
	Send(ctx context.Context, message Message) error
}

type NotifierFunc added in v0.1.6

type NotifierFunc func(ctx context.Context, message Message) error

NotifierFunc adapts a function to Notifier.

func (NotifierFunc) Send added in v0.1.6

func (fn NotifierFunc) Send(ctx context.Context, message Message) error

Send calls fn.

type ResendBodyFormat added in v0.1.6

type ResendBodyFormat string

ResendBodyFormat controls how Message.Body is mapped to Resend.

const (
	// ResendBodyText sends Message.Body as the Resend text body.
	ResendBodyText ResendBodyFormat = "text"

	// ResendBodyHTML sends Message.Body as the Resend html body.
	ResendBodyHTML ResendBodyFormat = "html"
)

type ResendConfig added in v0.1.6

type ResendConfig struct {
	APIKey            string
	From              string
	Channel           string
	Endpoint          string
	BodyFormat        ResendBodyFormat
	Timeout           time.Duration
	MaxResponseBytes  int64
	Client            *http.Client
	AllowInsecureHTTP bool
}

ResendConfig configures ResendNotifier.

type ResendNotifier added in v0.1.6

type ResendNotifier struct {
	// contains filtered or unexported fields
}

ResendNotifier sends email notifications through the Resend HTTP API.

func NewResendNotifier added in v0.1.6

func NewResendNotifier(config ResendConfig) (*ResendNotifier, error)

NewResendNotifier creates a Resend-backed email notifier.

func (*ResendNotifier) Send added in v0.1.6

func (notifier *ResendNotifier) Send(ctx context.Context, message Message) error

Send sends message through Resend and discards the returned provider ID.

func (*ResendNotifier) SendEmail added in v0.1.6

func (notifier *ResendNotifier) SendEmail(ctx context.Context, message Message) (ResendResult, error)

SendEmail sends message through Resend and returns the provider ID.

type ResendResult added in v0.1.6

type ResendResult struct {
	ID string `json:"id"`
}

ResendResult is the successful Resend API response.

type ResendStatusError added in v0.1.6

type ResendStatusError struct {
	StatusCode int
	Body       string
}

ResendStatusError describes a non-2xx Resend response.

func (*ResendStatusError) Error added in v0.1.6

func (err *ResendStatusError) Error() string

Error returns a safe delivery error without embedding provider response body.

func (*ResendStatusError) Retryable added in v0.1.6

func (err *ResendStatusError) Retryable() bool

Retryable reports whether the status is normally safe to retry.

func (*ResendStatusError) Unwrap added in v0.1.6

func (err *ResendStatusError) Unwrap() error

Unwrap returns the sentinel Resend delivery error.

type RetryConfig added in v0.1.6

type RetryConfig struct {
	Attempts   int
	Backoff    time.Duration
	MaxBackoff time.Duration
	RetryIf    func(error) bool
	Sleep      func(context.Context, time.Duration) error
}

RetryConfig configures RetryNotifier.

type RetryNotifier added in v0.1.6

type RetryNotifier struct {
	// contains filtered or unexported fields
}

RetryNotifier retries transient notification delivery failures.

func NewRetryNotifier added in v0.1.6

func NewRetryNotifier(notifier Notifier, config RetryConfig) (*RetryNotifier, error)

NewRetryNotifier wraps notifier with bounded retry behavior.

func (*RetryNotifier) Send added in v0.1.6

func (notifier *RetryNotifier) Send(ctx context.Context, message Message) error

Send retries message delivery according to config.

type SESBodyFormat added in v0.1.6

type SESBodyFormat string

SESBodyFormat controls how Message.Body is mapped to Amazon SES.

const (
	// SESBodyText sends Message.Body as the text body.
	SESBodyText SESBodyFormat = "text"

	// SESBodyHTML sends Message.Body as the HTML body.
	SESBodyHTML SESBodyFormat = "html"
)

type SESConfig added in v0.1.6

type SESConfig struct {
	Sender               SESSender
	From                 string
	Channel              string
	BodyFormat           SESBodyFormat
	Charset              string
	ConfigurationSetName string
	TenantName           string
	ReplyToAddresses     []string
}

SESConfig configures SESNotifier.

type SESDeliveryError added in v0.1.6

type SESDeliveryError struct {
	Code  string
	Fault string
	Retry bool
}

SESDeliveryError describes a failed Amazon SES send.

func (*SESDeliveryError) Error added in v0.1.6

func (err *SESDeliveryError) Error() string

Error returns a safe delivery error without embedding provider response text.

func (*SESDeliveryError) Retryable added in v0.1.6

func (err *SESDeliveryError) Retryable() bool

Retryable reports whether the SES failure is normally safe to retry.

func (*SESDeliveryError) RetryableError added in v0.1.6

func (err *SESDeliveryError) RetryableError() bool

Retryable reports whether the SES failure is normally safe to retry.

func (*SESDeliveryError) Unwrap added in v0.1.6

func (err *SESDeliveryError) Unwrap() error

Unwrap returns the sentinel SES delivery error.

type SESNotifier added in v0.1.6

type SESNotifier struct {
	// contains filtered or unexported fields
}

SESNotifier sends email notifications through Amazon SES API v2.

func NewSESNotifier added in v0.1.6

func NewSESNotifier(config SESConfig) (*SESNotifier, error)

NewSESNotifier creates an Amazon SES-backed email notifier.

func (*SESNotifier) Send added in v0.1.6

func (notifier *SESNotifier) Send(ctx context.Context, message Message) error

Send sends message through Amazon SES and discards the returned provider ID.

func (*SESNotifier) SendEmail added in v0.1.6

func (notifier *SESNotifier) SendEmail(ctx context.Context, message Message) (SESResult, error)

SendEmail sends message through Amazon SES and returns the provider ID.

type SESResult added in v0.1.6

type SESResult struct {
	MessageID string
}

SESResult is the successful Amazon SES SendEmail response.

type SESSender added in v0.1.6

type SESSender interface {
	SendEmail(ctx context.Context, params *sesv2.SendEmailInput, optFns ...func(*sesv2.Options)) (*sesv2.SendEmailOutput, error)
}

SESSender is implemented by *sesv2.Client.

type SMTPConfig added in v0.1.6

type SMTPConfig struct {
	Host       string
	Port       int
	ServerName string
	Username   string
	Password   string
	From       string
	Channel    string
	TLSMode    SMTPTLSMode
	Timeout    time.Duration
}

SMTPConfig configures SMTPNotifier.

type SMTPNotifier added in v0.1.6

type SMTPNotifier struct {
	// contains filtered or unexported fields
}

SMTPNotifier sends email notifications through SMTP.

func NewSMTPNotifier added in v0.1.6

func NewSMTPNotifier(config SMTPConfig) (*SMTPNotifier, error)

NewSMTPNotifier creates an SMTP-backed notifier.

func (*SMTPNotifier) Send added in v0.1.6

func (notifier *SMTPNotifier) Send(ctx context.Context, message Message) (err error)

Send delivers a notification message through SMTP.

type SMTPTLSMode added in v0.1.6

type SMTPTLSMode string

SMTPTLSMode controls how SMTPNotifier secures the SMTP connection.

const (
	// SMTPTLSModeStartTLS starts plaintext, then requires STARTTLS before auth or delivery.
	SMTPTLSModeStartTLS SMTPTLSMode = "starttls"

	// SMTPTLSModeImplicitTLS uses TLS from the first byte, commonly on port 465.
	SMTPTLSModeImplicitTLS SMTPTLSMode = "tls"

	// SMTPTLSModeNone disables TLS. Use only for trusted local relays.
	SMTPTLSModeNone SMTPTLSMode = "none"
)

func (SMTPTLSMode) String added in v0.1.6

func (mode SMTPTLSMode) String() string

type TimeoutNotifier added in v0.1.6

type TimeoutNotifier struct {
	// contains filtered or unexported fields
}

TimeoutNotifier applies a per-message timeout to a notifier.

func NewTimeoutNotifier added in v0.1.6

func NewTimeoutNotifier(notifier Notifier, timeout time.Duration) (*TimeoutNotifier, error)

NewTimeoutNotifier wraps notifier with a send timeout.

func (*TimeoutNotifier) Send added in v0.1.6

func (notifier *TimeoutNotifier) Send(ctx context.Context, message Message) error

Send applies the configured timeout around the wrapped notifier.

type WebhookConfig added in v0.1.6

type WebhookConfig struct {
	Endpoint          string
	Channel           string
	Headers           map[string]string
	Secret            []byte
	SignatureHeader   string
	TimestampHeader   string
	Timeout           time.Duration
	MaxResponseBytes  int64
	Client            *http.Client
	Now               func() time.Time
	AllowInsecureHTTP bool
}

WebhookConfig configures WebhookNotifier.

type WebhookNotifier added in v0.1.6

type WebhookNotifier struct {
	// contains filtered or unexported fields
}

WebhookNotifier sends notifications to an HTTP endpoint as JSON.

func NewWebhookNotifier added in v0.1.6

func NewWebhookNotifier(config WebhookConfig) (*WebhookNotifier, error)

NewWebhookNotifier creates an HTTP webhook notifier.

func (*WebhookNotifier) Send added in v0.1.6

func (notifier *WebhookNotifier) Send(ctx context.Context, message Message) error

Send delivers message to the configured webhook endpoint.

type WebhookPayload added in v0.1.6

type WebhookPayload struct {
	ID       string            `json:"id,omitempty"`
	TenantID string            `json:"tenant_id"`
	Channel  string            `json:"channel"`
	To       string            `json:"to"`
	Subject  string            `json:"subject,omitempty"`
	Body     string            `json:"body,omitempty"`
	Metadata map[string]string `json:"metadata,omitempty"`
}

WebhookPayload is the JSON request body sent by WebhookNotifier.

type WebhookStatusError added in v0.1.6

type WebhookStatusError struct {
	StatusCode int
	Body       string
}

WebhookStatusError describes a non-2xx webhook response.

func (*WebhookStatusError) Error added in v0.1.6

func (err *WebhookStatusError) Error() string

Error returns a safe delivery error without embedding provider response body.

func (*WebhookStatusError) Retryable added in v0.1.6

func (err *WebhookStatusError) Retryable() bool

Retryable reports whether the status is normally safe to retry.

func (*WebhookStatusError) Unwrap added in v0.1.6

func (err *WebhookStatusError) Unwrap() error

Unwrap returns the sentinel webhook delivery error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL