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
- Variables
- func DefaultRetryIf(err error) bool
- type ChannelRouter
- type FanoutNotifier
- type MemoryNotifier
- type Message
- type Notifier
- type NotifierFunc
- type ResendBodyFormat
- type ResendConfig
- type ResendNotifier
- type ResendResult
- type ResendStatusError
- type RetryConfig
- type RetryNotifier
- type SESBodyFormat
- type SESConfig
- type SESDeliveryError
- type SESNotifier
- type SESResult
- type SESSender
- type SMTPConfig
- type SMTPNotifier
- type SMTPTLSMode
- type TimeoutNotifier
- type WebhookConfig
- type WebhookNotifier
- type WebhookPayload
- type WebhookStatusError
Constants ¶
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" )
const (
// ChannelEmail is the conventional notification channel for email delivery.
ChannelEmail = "email"
)
Variables ¶
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
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.
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.
type MemoryNotifier ¶
type MemoryNotifier struct {
// contains filtered or unexported fields
}
func NewMemoryNotifier ¶
func NewMemoryNotifier() *MemoryNotifier
func (*MemoryNotifier) Messages ¶
func (notifier *MemoryNotifier) Messages() []Message
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
}
type NotifierFunc ¶ added in v0.1.6
NotifierFunc adapts a function to Notifier.
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
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.
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
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.
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.
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.
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.
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
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.