add simple webhook feature for peer, interface and user events (#398)

This commit is contained in:
Christoph
2025-04-19 21:29:26 +02:00
parent e75a32e4d0
commit 9354a1d9d3
14 changed files with 411 additions and 51 deletions

View File

@@ -0,0 +1,48 @@
package webhooks
import (
"bytes"
"encoding/json"
"io"
)
// WebhookData is the data structure for the webhook payload.
type WebhookData struct {
// Event is the event type (e.g. create, update, delete)
Event WebhookEvent `json:"event" example:"create"`
// Entity is the entity type (e.g. user, peer, interface)
Entity WebhookEntity `json:"entity" example:"user"`
// Identifier is the identifier of the entity
Identifier string `json:"identifier" example:"user-123"`
// Payload is the payload of the event
Payload any `json:"payload"`
}
// Serialize serializes the WebhookData to JSON and returns it as an io.Reader.
func (d *WebhookData) Serialize() (io.Reader, error) {
data, err := json.Marshal(d)
if err != nil {
return nil, err
}
return bytes.NewReader(data), nil
}
type WebhookEntity = string
const (
WebhookEntityUser WebhookEntity = "user"
WebhookEntityPeer WebhookEntity = "peer"
WebhookEntityInterface WebhookEntity = "interface"
)
type WebhookEvent = string
const (
WebhookEventCreate WebhookEvent = "create"
WebhookEventUpdate WebhookEvent = "update"
WebhookEventDelete WebhookEvent = "delete"
)