webhooks

package
v0.46.1 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT, MIT Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package webhooks is notifier registrations and their delivery log.

── THE COMPOSABILITY SEAM IS THE `type` COLUMN ──────────────────────────────────────────────────

A registration is `{ type, name, events[], config{} }`. `type` selects a delivery module (see package notify) and OWNS the shape of `config`. Adding Slack later is a new entry in the notifier registry and a new `config` validator; not a migration, not a schema change, not a new table. `config` is stored as opaque JSON here and validated in the module that understands it.

── THE SIGNING SECRET IS STORED REVERSIBLY, DELIBERATELY ────────────────────────────────────────

Sessions and tokens in package auth are SHA-256'd because they are INBOUND credentials. A signing secret is OUTBOUND: this process must compute `HMAC(secret, body)` on every delivery, so it needs the plaintext. The precedent is package registries, not auth: reversible at rest, protected by the 0700 directory and 0600 database file, and — the part enforced here — no function in this package returns it except SecretOf, and only notify calls that. List and Get project columns explicitly.

── WHY DELIVERIES ARE PERSISTED AND BOUNDED ─────────────────────────────────────────────────────

jobs keeps the last 50 in memory and evicts, so a retry that re-read a job minutes later could find it gone. Everything a delivery needs is captured at emit time and written here; Prune caps the rows per notifier.

Index

Constants

View Source
const MaxDeliveriesPerNotifier = 200

MaxDeliveriesPerNotifier: enough to debug a failing endpoint, bounded on disk.

Variables

This section is empty.

Functions

func IsError

func IsError(err error) bool

IsError reports whether err is a *Error.

Types

type CreateArgs

type CreateArgs struct {
	Type   string
	Name   string
	Config *omap.Map
	Events []string
	// ValidateConfig is supplied by the notifier registry so this package never learns what a
	// Slack config looks like.
	ValidateConfig func(kind string, c *omap.Map) error
	// Signs: whether this type signs. A type whose config already carries its credential gets no
	// secret — minting one anyway would leave a live credential nothing can use or rotate.
	Signs bool
}

CreateArgs is a registration.

type Delivery

type Delivery struct {
	ID         int64
	NotifierID int64
	EventID    string
	Event      string
	Payload    *string
}

Delivery is one delivery with its stored payload — the read behind redelivery. Payload is nil for rows written before 0.25.0 and for drops that never had an envelope; the caller must refuse those by name rather than inventing an event to replay.

type DeliveryRow

type DeliveryRow struct {
	ID           int64   `json:"id"`
	NotifierID   int64   `json:"notifierId"`
	EventID      string  `json:"eventId"`
	Event        string  `json:"event"`
	Status       string  `json:"status"`
	Attempts     int64   `json:"attempts"`
	ResponseCode *int64  `json:"responseCode"`
	Error        *string `json:"error"`
	CreatedAt    int64   `json:"createdAt"`
	UpdatedAt    int64   `json:"updatedAt"`
}

DeliveryRow is one delivery as listed.

type Error

type Error struct{ Msg string }

Error is a refused registration. The API maps it to 400.

func (*Error) Error

func (e *Error) Error() string

type NotifierRow

type NotifierRow struct {
	ID     int64     `json:"id"`
	Type   string    `json:"type"`
	Name   string    `json:"name"`
	Config *omap.Map `json:"config"`
	// Events is event names, or the single entry "*" meaning everything including future events.
	Events     []string `json:"events"`
	Enabled    bool     `json:"enabled"`
	CreatedAt  int64    `json:"createdAt"`
	LastStatus *string  `json:"lastStatus"`
	LastAt     *int64   `json:"lastAt"`
}

NotifierRow is what a caller may see. Note the absence of `secret` — that is the security property, in the type.

type PublicConfig

type PublicConfig func(kind string, c *omap.Map) *omap.Map

PublicConfig masks credential-bearing config fields for display. Injected rather than imported, so this package still knows nothing about what a webhook is.

type Result

type Result struct {
	Status       string
	Attempts     int64
	ResponseCode *int64
	Error        string
}

Result is a finished delivery. Status is "ok" or "failed".

type Webhooks

type Webhooks struct {
	Store *store.Store
	// contains filtered or unexported fields
}

Webhooks is the notifiers and deliveries tables. Owner: the server.

func New

func New(s *store.Store, publicConfig PublicConfig) *Webhooks

New wraps a store. publicConfig nil = identity.

func (*Webhooks) Create

func (w *Webhooks) Create(a CreateArgs) (NotifierRow, *string, error)

Create registers a notifier. Returns the row AND the generated signing secret (nil for a non-signing type) — the only time the secret leaves this package toward a caller.

func (*Webhooks) Deliveries

func (w *Webhooks) Deliveries(notifierID int64, limit int64) ([]DeliveryRow, error)

Deliveries lists the newest first. limit is clamped to 1..500.

func (*Webhooks) DeliveryWithPayload

func (w *Webhooks) DeliveryWithPayload(id int64) (*Delivery, error)

DeliveryWithPayload is nil when unknown.

func (*Webhooks) FinishDelivery

func (w *Webhooks) FinishDelivery(id int64, r Result) error

FinishDelivery records the outcome. Only the first 300 chars of the error's first line are kept: a wall of HTML from an error page belongs in nobody's database.

func (*Webhooks) Get

func (w *Webhooks) Get(id int64) (*NotifierRow, error)

Get is one masked row; nil when unknown.

func (*Webhooks) List

func (w *Webhooks) List() ([]NotifierRow, error)

List is every row, with credential-bearing config fields masked.

SubscribedTo deliberately does NOT go through the mask — the dispatcher needs the real values to make the request. Conflating the two is how a masked value ends up POSTed to a masked URL.

func (*Webhooks) NoteResult

func (w *Webhooks) NoteResult(notifierID int64, status string) error

NoteResult mirrors the last outcome onto the notifier, so a list view answers "is this thing working".

func (*Webhooks) Prune

func (w *Webhooks) Prune(notifierID int64) error

Prune caps the log per notifier. Called after each delivery.

`status != 'pending'` is not decoration: a delivery retrying against a slow endpoint lives for up to 21 seconds with its row already inserted; a burst of newer deliveries would push it out of the keep-set and DELETE it, and the later FinishDelivery would update zero rows.

func (*Webhooks) RawConfigOf

func (w *Webhooks) RawConfigOf(id int64) (*omap.Map, error)

RawConfigOf is the UNMASKED config, for delivery only — the test route and the dispatcher.

func (*Webhooks) Remove

func (w *Webhooks) Remove(id int64) (bool, error)

Remove deletes a notifier; deliveries cascade.

func (*Webhooks) SecretOf

func (w *Webhooks) SecretOf(id int64) (*string, error)

SecretOf is the signing secret, for package notify only. Named to be conspicuous in a diff: a second caller is the moment to ask whether it should exist. nil when unknown.

func (*Webhooks) SetEnabled

func (w *Webhooks) SetEnabled(id int64, enabled bool) (bool, error)

SetEnabled flips the flag; false when unknown.

func (*Webhooks) SettleInterrupted added in v0.46.0

func (w *Webhooks) SettleInterrupted() error

SettleInterrupted fails every delivery still `pending` — called once at boot, before the dispatcher starts, so nothing can be in flight. A restart or a recovered panic leaves such a row behind; Prune never removes a pending row, so without this it would read "pending" forever.

func (*Webhooks) StartDelivery

func (w *Webhooks) StartDelivery(notifierID int64, eventID, event string, payload []byte) (int64, error)

StartDelivery opens a row. Everything needed is captured NOW — the source job may be evicted later. payload is the whole envelope as sent (nil = none), and is what makes REDELIVERY possible.

func (*Webhooks) SubscribedTo

func (w *Webhooks) SubscribedTo(event string) ([]NotifierRow, error)

SubscribedTo is every enabled notifier subscribed to this event, unmasked. The dispatcher's only read path. Filtered here rather than with a LIKE on the JSON: the list is tiny and a LIKE would match 'job.failed' inside 'job.failed_x'.

Jump to

Keyboard shortcuts

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