webhooks

package
v0.0.0-...-e12325a Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// 投递状态复用 outbox 词表。
	StatusQueued  = outbox.StatusQueued
	StatusSending = outbox.StatusSending
	StatusSent    = outbox.StatusSent
	StatusFailed  = outbox.StatusFailed
	StatusSkipped = outbox.StatusSkipped
	StatusDead    = outbox.StatusDead

	// DefaultMaxAttempts River + 本地 attempt 预算。
	DefaultMaxAttempts = 5
)

Variables

View Source
var (
	ErrEndpointNotFound = errors.New("webhooks: endpoint not found")
	ErrDeliveryNotFound = errors.New("webhooks: delivery not found")
	ErrInvalidEndpoint  = errors.New("webhooks: invalid endpoint")
	ErrInvalidURL       = errors.New("webhooks: invalid target url")
)

Functions

func FormatEndpointError

func FormatEndpointError(err error) error

func FormatError

func FormatError(err error) string

Types

type BridgePublisher

type BridgePublisher struct {
	Inner  appevents.Publisher
	Fanout *Service
}

BridgePublisher 在 extension runtime 发事件后扇出 webhook(不改动插件投递语义)。

func (BridgePublisher) Emit

type CreateDeliveryInput

type CreateDeliveryInput struct {
	EndpointID    int64
	EventName     string
	EventID       string
	CorrelationID string
	Payload       json.RawMessage
}

type CreateEndpointInput

type CreateEndpointInput struct {
	Name        string
	TargetURL   string
	Secret      string
	Events      []string
	Enabled     *bool
	Description string
}

type Delivery

type Delivery struct {
	ID              int64           `json:"id"`
	EndpointID      int64           `json:"endpointId"`
	EventName       string          `json:"eventName"`
	EventID         string          `json:"eventId,omitempty"`
	CorrelationID   string          `json:"correlationId,omitempty"`
	Payload         json.RawMessage `json:"payload,omitempty"`
	Status          string          `json:"status"`
	AttemptCount    int             `json:"attemptCount"`
	HTTPStatus      int             `json:"httpStatus,omitempty"`
	ResponseSnippet string          `json:"responseSnippet,omitempty"`
	Reason          string          `json:"reason,omitempty"`
	ErrorSummary    string          `json:"errorSummary,omitempty"`
	CreatedAt       time.Time       `json:"createdAt"`
	UpdatedAt       time.Time       `json:"updatedAt"`
	CompletedAt     *time.Time      `json:"completedAt,omitempty"`
}

type DeliveryUpdate

type DeliveryUpdate struct {
	ID              int64
	Status          string
	AttemptCount    int
	HTTPStatus      int
	ResponseSnippet string
	Reason          string
	ErrorSummary    string
}

type Endpoint

type Endpoint struct {
	ID           int64  `json:"id"`
	Name         string `json:"name"`
	TargetURL    string `json:"targetUrl"`
	SecretMasked string `json:"secretMasked"`
	// HasSecret 表示是否已配置签名密钥(不回传明文)。
	HasSecret   bool      `json:"hasSecret"`
	Events      []string  `json:"events"`
	Enabled     bool      `json:"enabled"`
	Description string    `json:"description,omitempty"`
	CreatedAt   time.Time `json:"createdAt"`
	UpdatedAt   time.Time `json:"updatedAt"`
}

Endpoint 是管理员配置的出站订阅。

func PublicEndpoint

func PublicEndpoint(record EndpointRecord) Endpoint

PublicEndpoint 去掉 secret 明文后再返回 API。

type EndpointRecord

type EndpointRecord struct {
	Endpoint
	Secret string
}

EndpointRecord 内部行(含 secret 明文,仅 store/worker 使用)。

type PostgresStore

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

func NewPostgresStore

func NewPostgresStore(pool *pgxpool.Pool) *PostgresStore

func (*PostgresStore) CreateDeliveryTx

func (s *PostgresStore) CreateDeliveryTx(ctx context.Context, tx pgx.Tx, input CreateDeliveryInput) (Delivery, error)

func (*PostgresStore) CreateEndpoint

func (s *PostgresStore) CreateEndpoint(ctx context.Context, input CreateEndpointInput) (EndpointRecord, error)

func (*PostgresStore) DeleteEndpoint

func (s *PostgresStore) DeleteEndpoint(ctx context.Context, id int64) error

func (*PostgresStore) GetDelivery

func (s *PostgresStore) GetDelivery(ctx context.Context, id int64) (Delivery, error)

func (*PostgresStore) GetEndpoint

func (s *PostgresStore) GetEndpoint(ctx context.Context, id int64) (EndpointRecord, error)

func (*PostgresStore) ListDeliveries

func (s *PostgresStore) ListDeliveries(ctx context.Context, endpointID int64, limit int) ([]Delivery, error)

func (*PostgresStore) ListEnabledEndpointsForEvent

func (s *PostgresStore) ListEnabledEndpointsForEvent(ctx context.Context, eventName string) ([]EndpointRecord, error)

func (*PostgresStore) ListEndpoints

func (s *PostgresStore) ListEndpoints(ctx context.Context) ([]EndpointRecord, error)

func (*PostgresStore) UpdateDelivery

func (s *PostgresStore) UpdateDelivery(ctx context.Context, input DeliveryUpdate) error

func (*PostgresStore) UpdateEndpoint

func (s *PostgresStore) UpdateEndpoint(ctx context.Context, id int64, input UpdateEndpointInput) (EndpointRecord, error)

func (*PostgresStore) UpdateEndpointSecret

func (s *PostgresStore) UpdateEndpointSecret(ctx context.Context, id int64, encrypted string) error

UpdateEndpointSecret 懒迁移:仅更新 secret 列(明文 → 密文)。

type Service

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

Service 管理端点配置,并把 core observe 事件扇出为 webhook 投递。

func NewService

func NewService(store Store, pool *pgxpool.Pool, jobs TxEnqueuer) *Service

func (*Service) CreateEndpoint

func (s *Service) CreateEndpoint(ctx context.Context, actor identity.Actor, input CreateEndpointInput) (Endpoint, error)

func (*Service) DecryptEndpointSecret

func (s *Service) DecryptEndpointSecret(stored string) (string, error)

DecryptEndpointSecret 投递时解密 secret;错误密钥 fail closed。

func (*Service) DeleteEndpoint

func (s *Service) DeleteEndpoint(ctx context.Context, actor identity.Actor, id int64) error

func (*Service) Fanout

func (s *Service) Fanout(ctx context.Context, envelope appevents.Envelope)

Fanout 将 observe 事件写入各订阅端点的投递记录并入队(异步,不阻塞业务)。

func (*Service) ListDeliveries

func (s *Service) ListDeliveries(ctx context.Context, actor identity.Actor, endpointID int64, limit int) ([]Delivery, error)

func (*Service) ListEndpoints

func (s *Service) ListEndpoints(ctx context.Context, actor identity.Actor) ([]Endpoint, error)

func (*Service) MaybeMigrateSecret

func (s *Service) MaybeMigrateSecret(stored string) (encrypted string, ok bool)

MaybeMigrateSecret 若 stored 为明文且 cipher 启用,返回应写回的密文。

func (*Service) UpdateEndpoint

func (s *Service) UpdateEndpoint(ctx context.Context, actor identity.Actor, id int64, input UpdateEndpointInput) (Endpoint, error)

func (*Service) WithAllowHTTP

func (s *Service) WithAllowHTTP(allow bool) *Service

WithAllowHTTP 开发/测试可显式允许 http webhook 目标(生产勿开)。

func (*Service) WithCipher

func (s *Service) WithCipher(c *crypto.OptionCipher) *Service

WithCipher 注入 AES-GCM 加密器用于 webhook secret 静态加密。

type Store

type Store interface {
	ListEndpoints(ctx context.Context) ([]EndpointRecord, error)
	GetEndpoint(ctx context.Context, id int64) (EndpointRecord, error)
	CreateEndpoint(ctx context.Context, input CreateEndpointInput) (EndpointRecord, error)
	UpdateEndpoint(ctx context.Context, id int64, input UpdateEndpointInput) (EndpointRecord, error)
	DeleteEndpoint(ctx context.Context, id int64) error

	CreateDeliveryTx(ctx context.Context, tx pgx.Tx, input CreateDeliveryInput) (Delivery, error)
	GetDelivery(ctx context.Context, id int64) (Delivery, error)
	UpdateDelivery(ctx context.Context, input DeliveryUpdate) error
	ListDeliveries(ctx context.Context, endpointID int64, limit int) ([]Delivery, error)
	ListEnabledEndpointsForEvent(ctx context.Context, eventName string) ([]EndpointRecord, error)
}

Store 持久化端点与投递记录。

type TxEnqueuer

type TxEnqueuer interface {
	EnqueueTx(context.Context, pgx.Tx, river.JobArgs, supportjobs.EnqueueOptions) (*rivertype.JobInsertResult, error)
}

type UpdateEndpointInput

type UpdateEndpointInput struct {
	Name        *string
	TargetURL   *string
	Secret      *string // 空字符串表示不改;非空则轮换
	Events      []string
	Enabled     *bool
	Description *string
	ClearSecret bool
}

Jump to

Keyboard shortcuts

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