webhook

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 14 Imported by: 0

README

modules/webhook

webhook 模块已重构为 Codec + Transport 架构,旧 Converter 路径已彻底移除。

配置项

modules.Config{
  "endpoint": "https://example.com/hook",
  "timeout": "10s",
  "level": "info",   // debug/info/warn/error
  "codec": "default",// default/json/自定义
}

默认行为

  • 默认 Transport: HTTPTransport(POST JSON)
  • 默认 Codec: default

扩展点

  • 自定义 Codec: RegisterCodec(codec)
  • 自定义 Transport: 在 Option.Transport 注入实现

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterCodec added in v0.2.0

func RegisterCodec(codec Codec) error

Types

type Codec added in v0.2.0

type Codec interface {
	Name() string
	Encode(ctx context.Context, record *slog.Record, attrs []slog.Attr, groups []string) ([]byte, error)
}

Codec converts slog records to HTTP payload bytes.

func GetCodec added in v0.2.0

func GetCodec(name string) (Codec, bool)

type HTTPTransport added in v0.2.0

type HTTPTransport struct {
	Endpoint string
	Timeout  time.Duration
	Client   *http.Client
}

HTTPTransport is the default webhook transport.

func (*HTTPTransport) Send added in v0.2.0

func (t *HTTPTransport) Send(ctx context.Context, payload []byte) error

type Option

type Option struct {
	// log level (default: info)
	Level slog.Leveler

	// URL
	Endpoint string
	Timeout  time.Duration // default: 10s

	// optional: fetch attributes from context
	AttrFromContext []func(ctx context.Context) []slog.Attr

	// optional: codec and transport
	Codec     Codec
	Transport Transport
}

func (Option) NewWebhookHandler

func (o Option) NewWebhookHandler() slog.Handler

type Transport added in v0.2.0

type Transport interface {
	Send(ctx context.Context, payload []byte) error
}

Transport sends already-encoded payload bytes.

type WebhookAdapter

type WebhookAdapter struct {
	*modules.BaseModule
	// contains filtered or unexported fields
}

WebhookAdapter Webhook模块适配器

func NewWebhookAdapter

func NewWebhookAdapter() *WebhookAdapter

NewWebhookAdapter 创建Webhook适配器

func (*WebhookAdapter) Configure

func (w *WebhookAdapter) Configure(config modules.Config) error

Configure 配置Webhook模块

type WebhookHandler

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

func (*WebhookHandler) Enabled

func (h *WebhookHandler) Enabled(_ context.Context, level slog.Level) bool

func (*WebhookHandler) Handle

func (h *WebhookHandler) Handle(ctx context.Context, record slog.Record) error

func (*WebhookHandler) WithAttrs

func (h *WebhookHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*WebhookHandler) WithGroup

func (h *WebhookHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

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