webhook

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package webhook provides a notify.Router that delivers notification events to configured HTTP endpoints via signed JSON POST requests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func VerifySignature

func VerifySignature(secret []byte, timestamp string, body []byte, sigHeader string, tolerance time.Duration) bool

VerifySignature verifies an incoming webhook request's HMAC-SHA256 signature and rejects requests whose timestamp falls outside the given tolerance window. sigHeader should be the raw value of the X-Mailbox-Signature header (e.g. "sha256=abc..."). Use a tolerance of 5*time.Minute to match industry convention (Stripe, GitHub, Slack). Pass 0 to skip the timestamp check (not recommended for production).

Types

type EndpointConfig

type EndpointConfig struct {
	// URL is the HTTP endpoint that receives POST requests.
	URL string

	// Headers are additional HTTP headers sent with every request to this endpoint.
	Headers map[string]string

	// Events restricts delivery to the listed event types.
	// When empty, all event types are delivered.
	Events []string
}

EndpointConfig describes a single webhook destination.

type Option

type Option func(*options)

Option configures a webhook Router.

func WithEndpoints

func WithEndpoints(endpoints ...EndpointConfig) Option

WithEndpoints appends webhook destinations. Multiple calls accumulate.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets a custom logger.

func WithMaxRetries

func WithMaxRetries(n int) Option

WithMaxRetries sets the maximum number of retries after the initial attempt. Total attempts = 1 + n. Default is 3 (one initial attempt plus three retries).

func WithSigningKey

func WithSigningKey(key []byte) Option

WithSigningKey sets the HMAC-SHA256 signing secret. When set, each request includes X-Mailbox-Signature and X-Mailbox-Timestamp headers. Use VerifySignature on the receiving end to authenticate the request.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the per-request HTTP timeout. Default is 10s.

type Router

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

Router implements notify.Router by POSTing JSON-encoded events to configured HTTP endpoints. It delivers to all endpoints whose event filter matches, regardless of the RoutingInfo passed by the notifier.

func New

func New(opts ...Option) *Router

New creates a new webhook Router.

func (*Router) Route

func (r *Router) Route(ctx context.Context, _ notify.RoutingInfo, evt notify.Event) error

Route delivers the event to all configured endpoints whose event filter matches. RoutingInfo is ignored — webhook delivery is always broadcast to all endpoints.

If no endpoints match the event type, Route returns nil (nothing to do). If at least one endpoint matches and all of them fail after retries, Route returns a combined error so the notifier can fall back to store persistence. Partial success (some endpoints succeed, others fail) returns nil; failures are logged at Warn level.

Jump to

Keyboard shortcuts

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