Documentation
¶
Index ¶
- Constants
- func HTTPStatus(err error) int
- type CFResend
- func (c *CFResend) BaseURL() string
- func (c *CFResend) Client() *resend.Client
- func (c *CFResend) From() string
- func (c *CFResend) GetDependencies() []string
- func (c *CFResend) GetInitOrderStage() cf.Stage
- func (c *CFResend) Health(ctx context.Context) error
- func (c *CFResend) Init(ctx context.Context, fw *cf.CaerusFramework) error
- func (c *CFResend) Metrics() []cf_observability.Metric
- func (c *CFResend) Name() string
- func (c *CFResend) OnConfigReload(source string, cfg any)
- func (c *CFResend) RegisterConfigSources(conf any) error
- func (c *CFResend) Send(ctx context.Context, m Mail) (string, error)
- func (c *CFResend) Shutdown(ctx context.Context) error
- type Mail
- type Option
- func WithAPIKey(apiKey string) Option
- func WithBaseURL(baseURL string) Option
- func WithConfig(cfg ResendConfig) Option
- func WithConfigSource(name, path string, opts ...SourceOption) Option
- func WithFromAddress(from string) Option
- func WithHTTPClient(hc *http.Client) Option
- func WithLogger(logger *slog.Logger) Option
- func WithName(name string) Option
- func WithTimeout(d time.Duration) Option
- type ResendConfig
- type SendError
- type SourceOption
Constants ¶
const ( // ComponentName is the framework component name for the resend component. // It is the identifier other components use in GetDependencies to require // resend. ComponentName = "resend" // ComponentStage is the stage data-layer components initialize in. It is // not a built-in bootstrap stage; AddComponent registers it automatically // the first time a component declares it. ComponentStage = cf.Stage("data") )
Variables ¶
This section is empty.
Functions ¶
func HTTPStatus ¶ added in v0.0.6
HTTPStatus returns the Resend HTTP status from err when err is (or wraps) a SendError. It returns 0 for nil, validation errors, network failures, and SDK errors that did not go through Send.
Types ¶
type CFResend ¶
type CFResend struct {
// contains filtered or unexported fields
}
CFResend is the caerus-framework-resend component. It wraps the Resend SDK client and hands out live accessors (Client, From) to peers.
func (*CFResend) BaseURL ¶
BaseURL returns the configured Resend API endpoint (empty = the SDK default).
func (*CFResend) Client ¶
Client returns the live Resend SDK client. It is non-nil after a successful Init and nil before Init or after Shutdown. Call it per use rather than caching the pointer; the component swaps the client on config reload.
func (*CFResend) From ¶
From returns the configured soft-default sender (from_address / WithFromAddress). Empty means every Send must set Mail.From.
func (*CFResend) GetDependencies ¶
GetDependencies implements cf.Dependencies. The component logs through the framework logs component, and depends on configuration when WithConfigSource is set.
func (*CFResend) GetInitOrderStage ¶
GetInitOrderStage implements cf.CaerusComponent.
func (*CFResend) Health ¶
Health implements cf.HealthProvider. Resend exposes no liveness endpoint, so health reflects that a client is initialized (nil before Init or after Shutdown). Connectivity is verified on each Send error.
func (*CFResend) Init ¶
Init implements cf.CaerusComponent. It builds the Resend client from the option-set or configuration-source credentials. An empty API key fails startup (fail-fast) so a misconfigured mailer never silently swallows sends.
func (*CFResend) Metrics ¶
func (c *CFResend) Metrics() []cf_observability.Metric
Metrics implements cf_observability.MetricsProvider. It reports the resend client's identity, per-sender send traffic and latency; before Init or after Shutdown it returns nil, so the observability component skips it (lazy pickup).
resend_info is a snapshot descriptor gauge (value 1) carrying the live configured sender identity; it is the "component is initialized" marker. Traffic counters are bucketed by the actual sender of each email (the resolved req.From, or the configured default), so the from label is accurate per send and a from_address change never relabels history.
func (*CFResend) Name ¶
Name implements cf.CaerusComponent. Returns the custom name set via WithName, or the default ComponentName ("resend") if no custom name was set.
func (*CFResend) OnConfigReload ¶
OnConfigReload implements cf.ConfigReloader. It rebuilds the client from the bound configuration source. The fresh value is delivered as cfg but the client is rebuilt from the source so the translation stays in one place. On failure the previous client is kept.
func (*CFResend) RegisterConfigSources ¶
RegisterConfigSources implements cf.ConfigSourceRegistrar. The framework calls it during argv absorption; it registers this component's configuration source (name, path, env prefix, format, Owner) with the configuration component. No-op when no source is bound.
func (*CFResend) Send ¶
Send sends m through Resend and returns the provider message id.
From: from_address / WithFromAddress is a soft default when Mail.From is empty. If both are empty, or the resolved From or any To address does not parse (`net/mail.ParseAddress`), Send fails. HTML and Text may both be set; at least one must be non-empty.
If the first HTTP status is 429 or 5xx, Send waits (Retry-After, capped at 1s) and tries once more while ctx is live. 4xx other than 429 and network errors are not retried.
type Mail ¶ added in v0.0.6
type Mail struct {
// From overrides from_address when non-empty. Empty (or whitespace)
// uses the configured soft default. The resolved value must parse as
// an RFC 5322 address (`net/mail.ParseAddress`).
From string
To []string
Subject string
HTML string
Text string
ReplyTo string
// Tags are Resend metadata (name → value). Empty names are skipped.
Tags map[string]string
// IdempotencyKey is sent as the Idempotency-Key header when non-empty.
IdempotencyKey string
}
Mail is the Caerus send DTO (SES-simple shape). Apps pass this to Send without importing resend-go. Attachments, Cc/Bcc, headers, and scheduled send stay on Client() for callers that opt into the SDK.
type Option ¶
type Option func(*options)
Option configures the resend component at construction time.
func WithAPIKey ¶
WithAPIKey sets the Resend API key directly (tests, embedded use). Prefer WithConfigSource for production so the key rotates via config reload.
func WithBaseURL ¶
WithBaseURL overrides the Resend API endpoint. Empty uses the SDK default.
func WithConfig ¶
func WithConfig(cfg ResendConfig) Option
WithConfig sets a static configuration snapshot. Non-zero fields of cfg override the values set by the convenience options. Prefer WithConfigSource when using caerus-framework-configuration with hot-reload.
func WithConfigSource ¶
func WithConfigSource(name, path string, opts ...SourceOption) Option
WithConfigSource binds this component to a named configuration source and registers that source with the configuration component (via the framework's ConfigSourceRegistrar pass during argv absorption). The module owns the Source: the config type, the default EnvPrefix and its Owner (Name(), so named instances reload correctly). main only points the instance at where the config lives.
cf_resend.New(cf_resend.WithConfigSource("resend", "config/resend.json"))
cf_resend.New(cf_resend.WithConfigSource("mailer", "/etc/app/mailer.yaml",
cf_resend.WithSourceFormat(cf_configuration.FormatYAML)))
A path of "" registers an env-only (fileless) source when the EnvPrefix is non-empty. The path CLI override stays --<source-name> (ParseFlags). Declares a dependency on "configuration".
func WithFromAddress ¶
WithFromAddress sets the soft-default sender. Send uses it when Mail.From is empty. A non-empty Mail.From overrides this call only. Both empty, or a value that does not parse as an email address, is an error.
func WithHTTPClient ¶
WithHTTPClient overrides the HTTP client used for Resend calls. Useful for tests (a stub RoundTripper) and for sharing a process-wide transport. The component does not close a client it did not create.
func WithLogger ¶
WithLogger overrides the logger used for component diagnostics. By default the component logs through the framework logs component (declared in GetDependencies); WithLogger is an explicit override for tests and embedded use and wins over the framework logger. slog.Default() remains the fallback only when neither is available.
func WithName ¶
WithName sets a custom component name, allowing multiple resend instances in the same process. The default name is "resend" (ComponentName). Use this when you need several mail senders (e.g. branded vs system) in one binary. Retrieve named instances with GetByName[*CFResend](fw, "mailer").
func WithTimeout ¶
WithTimeout sets the per-send HTTP timeout (default 10s).
type ResendConfig ¶
type ResendConfig struct {
// APIKey is the Resend API key (resend.com, or a self-hosted instance).
APIKey string `json:"api_key" yaml:"api_key" env:"API_KEY" secret:"redact"`
// FromAddress is the soft-default sender (e.g. "noreply@example.com"
// or `Name <noreply@example.com>`). Send uses it when Mail.From is empty.
FromAddress string `json:"from_address" yaml:"from_address" env:"FROM_ADDRESS"`
// BaseURL overrides the Resend API endpoint. Empty uses the SDK default.
// Useful for self-hosted instances or tests that stub the API.
BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty" env:"BASE_URL"`
// TimeoutSec bounds each Resend HTTP call (default 10s).
TimeoutSec float64 `json:"timeout_sec,omitempty" yaml:"timeout_sec,omitempty" env:"TIMEOUT_SEC"`
}
ResendConfig is the file/env-drivable configuration. Load it through the configuration component (caerus-framework-configuration) and pass it via WithConfigSource; both JSON and YAML tags are provided.
type SendError ¶ added in v0.0.6
type SendError struct {
// contains filtered or unexported fields
}
SendError is returned by Send when the Resend HTTP call failed. The SDK error string does not carry a status code; HTTPStatus is filled from the component transport (the same source as resend_emails_failed_total).
Status 0 means a transport/network failure or an unknown status. Client() bypass (Emails.Send without going through Send) is not wrapped.
func (*SendError) HTTPStatus ¶ added in v0.0.6
HTTPStatus is the Resend API status for this send, or 0 if the request never got an HTTP response.
type SourceOption ¶
type SourceOption func(*sourceOptions)
SourceOption configures the self-registered configuration source created by WithConfigSource.
func WithSourceEnvPrefix ¶
func WithSourceEnvPrefix(prefix string) SourceOption
WithSourceEnvPrefix sets the environment overlay prefix for the source (default: the uppercase source name with "-" replaced by "_", plus "_"). An empty prefix disables env overlay.
func WithSourceFormat ¶
func WithSourceFormat(f cf_configuration.Format) SourceOption
WithSourceFormat forces the file format instead of inferring it from the path extension (".yaml"/".yml" → YAML; anything else JSON).