webhooks

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DeliveryStatusSucceeded = "SUCCEEDED"
	DeliveryStatusFailed    = "FAILED"
)

Variables

This section is empty.

Functions

func ListEventTypes

func ListEventTypes() []string

ListEventTypes returns the event names endpoints can subscribe to.

Types

type CreateEndpointRequest

type CreateEndpointRequest struct {
	Name     string   `json:"name"`
	URL      string   `json:"url"`
	Events   []string `json:"events"`
	Secret   string   `json:"secret,omitempty"`
	IsActive *bool    `json:"is_active,omitempty"`
}

CreateEndpointRequest creates a tenant webhook endpoint.

type Delivery

type Delivery struct {
	ID            string          `json:"id"`
	TenantID      string          `json:"tenant_id"`
	EndpointID    string          `json:"endpoint_id"`
	EventID       string          `json:"event_id"`
	EventType     string          `json:"event_type"`
	Status        string          `json:"status"`
	StatusCode    int             `json:"status_code,omitempty"`
	AttemptNumber int             `json:"attempt_number"`
	RequestBody   json.RawMessage `json:"request_body,omitempty"`
	ResponseBody  string          `json:"response_body,omitempty"`
	Error         string          `json:"error,omitempty"`
	DeliveredAt   time.Time       `json:"delivered_at"`
	CreatedAt     time.Time       `json:"created_at"`
}

Delivery records one webhook delivery attempt.

type DeliveryResult

type DeliveryResult struct {
	Event      Event      `json:"event"`
	Deliveries []Delivery `json:"deliveries"`
}

DeliveryResult is returned after dispatching a webhook event.

type Endpoint

type Endpoint struct {
	ID             string     `json:"id"`
	TenantID       string     `json:"tenant_id"`
	Name           string     `json:"name"`
	URL            string     `json:"url"`
	Events         []string   `json:"events"`
	Secret         string     `json:"-"`
	SecretSet      bool       `json:"secret_set"`
	IsActive       bool       `json:"is_active"`
	LastDeliveryAt *time.Time `json:"last_delivery_at,omitempty"`
	CreatedAt      time.Time  `json:"created_at"`
	UpdatedAt      time.Time  `json:"updated_at"`
}

Endpoint is a tenant-scoped outbound webhook target.

type Event

type Event struct {
	ID        string          `json:"id"`
	Type      string          `json:"type"`
	TenantID  string          `json:"tenant_id"`
	Data      json.RawMessage `json:"data"`
	CreatedAt time.Time       `json:"created_at"`
}

Event is the normalized payload delivered to webhook endpoints.

type GORMRepository

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

GORMRepository implements Repository with the shared ORM layer.

func NewGORMRepository

func NewGORMRepository(db *gorm.DB) *GORMRepository

NewGORMRepository creates an ORM-backed webhook repository.

func (*GORMRepository) CreateDelivery

func (r *GORMRepository) CreateDelivery(ctx context.Context, delivery *Delivery) error

CreateDelivery records a webhook delivery attempt.

func (*GORMRepository) CreateEndpoint

func (r *GORMRepository) CreateEndpoint(ctx context.Context, endpoint *Endpoint) error

CreateEndpoint stores a tenant webhook endpoint.

func (*GORMRepository) DeleteEndpoint

func (r *GORMRepository) DeleteEndpoint(ctx context.Context, tenantID, endpointID string) (int64, error)

DeleteEndpoint deletes a tenant webhook endpoint.

func (*GORMRepository) GetEndpoint

func (r *GORMRepository) GetEndpoint(ctx context.Context, tenantID, endpointID string) (*Endpoint, error)

GetEndpoint returns one tenant webhook endpoint.

func (*GORMRepository) ListDeliveries

func (r *GORMRepository) ListDeliveries(ctx context.Context, tenantID, endpointID string, limit int) ([]Delivery, error)

ListDeliveries returns recent delivery attempts for one endpoint.

func (*GORMRepository) ListEndpoints

func (r *GORMRepository) ListEndpoints(ctx context.Context, tenantID string, activeOnly bool) ([]Endpoint, error)

ListEndpoints returns tenant webhook endpoints.

func (*GORMRepository) UpdateEndpoint

func (r *GORMRepository) UpdateEndpoint(ctx context.Context, endpoint *Endpoint) error

UpdateEndpoint updates a tenant webhook endpoint.

func (*GORMRepository) UpdateEndpointLastDelivery

func (r *GORMRepository) UpdateEndpointLastDelivery(ctx context.Context, tenantID, endpointID string, deliveredAt time.Time) error

UpdateEndpointLastDelivery stores the most recent attempted delivery time.

type Repository

type Repository interface {
	ListEndpoints(ctx context.Context, tenantID string, activeOnly bool) ([]Endpoint, error)
	GetEndpoint(ctx context.Context, tenantID, endpointID string) (*Endpoint, error)
	CreateEndpoint(ctx context.Context, endpoint *Endpoint) error
	UpdateEndpoint(ctx context.Context, endpoint *Endpoint) error
	DeleteEndpoint(ctx context.Context, tenantID, endpointID string) (int64, error)
	CreateDelivery(ctx context.Context, delivery *Delivery) error
	ListDeliveries(ctx context.Context, tenantID, endpointID string, limit int) ([]Delivery, error)
	UpdateEndpointLastDelivery(ctx context.Context, tenantID, endpointID string, deliveredAt time.Time) error
}

Repository defines outbound webhook persistence.

type Service

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

Service manages outbound webhook endpoints and delivery.

func NewService

func NewService(pool *pgxpool.Pool) *Service

NewService creates a webhook service backed by the ORM repository.

func NewServiceWithRepository

func NewServiceWithRepository(repo Repository, httpClient *http.Client) *Service

NewServiceWithRepository creates a webhook service with injected dependencies.

func (*Service) CreateEndpoint

func (s *Service) CreateEndpoint(ctx context.Context, tenantID string, req *CreateEndpointRequest) (*Endpoint, error)

CreateEndpoint creates a tenant webhook endpoint.

func (*Service) DeleteEndpoint

func (s *Service) DeleteEndpoint(ctx context.Context, tenantID, endpointID string) error

DeleteEndpoint deletes a tenant webhook endpoint.

func (*Service) Dispatch

func (s *Service) Dispatch(ctx context.Context, event Event) (*DeliveryResult, error)

Dispatch sends an event to every active endpoint subscribed to the event.

func (*Service) DispatchAsync

func (s *Service) DispatchAsync(event Event)

DispatchAsync sends an event without blocking the caller.

func (*Service) DispatchTest

func (s *Service) DispatchTest(ctx context.Context, tenantID, endpointID string, req *TestDeliveryRequest) (*DeliveryResult, error)

DispatchTest sends a deterministic event to one endpoint, regardless of subscription.

func (*Service) GetEndpoint

func (s *Service) GetEndpoint(ctx context.Context, tenantID, endpointID string) (*Endpoint, error)

GetEndpoint returns one tenant webhook endpoint.

func (*Service) ListDeliveries

func (s *Service) ListDeliveries(ctx context.Context, tenantID, endpointID string, limit int) ([]Delivery, error)

ListDeliveries returns recent webhook delivery attempts.

func (*Service) ListEndpoints

func (s *Service) ListEndpoints(ctx context.Context, tenantID string, activeOnly bool) ([]Endpoint, error)

ListEndpoints returns tenant webhook endpoints.

func (*Service) RegisterPluginHooks

func (s *Service) RegisterPluginHooks(registry *plugin.HookRegistry)

RegisterPluginHooks routes supported plugin events into outbound webhook delivery.

func (*Service) UpdateEndpoint

func (s *Service) UpdateEndpoint(ctx context.Context, tenantID, endpointID string, req *UpdateEndpointRequest) (*Endpoint, error)

UpdateEndpoint updates a tenant webhook endpoint.

type TestDeliveryRequest

type TestDeliveryRequest struct {
	EventType string          `json:"event_type,omitempty"`
	Payload   json.RawMessage `json:"payload,omitempty"`
}

TestDeliveryRequest triggers a deterministic test delivery to one endpoint.

type UpdateEndpointRequest

type UpdateEndpointRequest struct {
	Name     *string  `json:"name,omitempty"`
	URL      *string  `json:"url,omitempty"`
	Events   []string `json:"events,omitempty"`
	Secret   *string  `json:"secret,omitempty"`
	IsActive *bool    `json:"is_active,omitempty"`
}

UpdateEndpointRequest updates a tenant webhook endpoint.

Jump to

Keyboard shortcuts

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