|
| 1 | +package webhooks |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + |
| 8 | + "github.com/pocketbase/pocketbase" |
| 9 | +) |
| 10 | + |
| 11 | +// WebhookRouter handles the routing of incoming webhooks to their appropriate handlers |
| 12 | +type WebhookRouter struct { |
| 13 | + app *pocketbase.PocketBase |
| 14 | + handlers map[string][]WebhookHandler |
| 15 | + mu sync.RWMutex |
| 16 | +} |
| 17 | + |
| 18 | +// WebhookHandler defines the interface for webhook handlers |
| 19 | +type WebhookHandler interface { |
| 20 | + // Handle processes the webhook payload |
| 21 | + Handle(payload *WebhookPayload) error |
| 22 | + // ShouldHandle determines if this handler should process the webhook |
| 23 | + ShouldHandle(payload *WebhookPayload) bool |
| 24 | +} |
| 25 | + |
| 26 | +// NewWebhookRouter creates a new WebhookRouter instance |
| 27 | +func NewWebhookRouter(app *pocketbase.PocketBase) *WebhookRouter { |
| 28 | + return &WebhookRouter{ |
| 29 | + app: app, |
| 30 | + handlers: make(map[string][]WebhookHandler), |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// RegisterHandler registers a new webhook handler for a specific collection |
| 35 | +func (r *WebhookRouter) RegisterHandler(collection string, handler WebhookHandler) { |
| 36 | + r.mu.Lock() |
| 37 | + defer r.mu.Unlock() |
| 38 | + |
| 39 | + if r.handlers[collection] == nil { |
| 40 | + r.handlers[collection] = make([]WebhookHandler, 0) |
| 41 | + } |
| 42 | + r.handlers[collection] = append(r.handlers[collection], handler) |
| 43 | +} |
| 44 | + |
| 45 | +// Route processes an incoming webhook and routes it to appropriate handlers |
| 46 | +func (r *WebhookRouter) Route(payload *WebhookPayload) error { |
| 47 | + r.mu.RLock() |
| 48 | + handlers := r.handlers[payload.Collection] |
| 49 | + r.mu.RUnlock() |
| 50 | + |
| 51 | + var errors []error |
| 52 | + for _, handler := range handlers { |
| 53 | + if handler.ShouldHandle(payload) { |
| 54 | + if err := handler.Handle(payload); err != nil { |
| 55 | + errors = append(errors, fmt.Errorf("handler error: %w", err)) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if len(errors) > 0 { |
| 61 | + return fmt.Errorf("webhook routing errors: %v", errors) |
| 62 | + } |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +// Example handler for logging webhooks |
| 67 | +type LoggingWebhookHandler struct{} |
| 68 | + |
| 69 | +func (h *LoggingWebhookHandler) Handle(payload *WebhookPayload) error { |
| 70 | + // Log the webhook payload |
| 71 | + jsonData, _ := json.MarshalIndent(payload, "", " ") |
| 72 | + fmt.Printf("Received webhook: %s\n", string(jsonData)) |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +func (h *LoggingWebhookHandler) ShouldHandle(payload *WebhookPayload) bool { |
| 77 | + return true // Handle all webhooks |
| 78 | +} |
| 79 | + |
| 80 | +// Example handler for specific collection changes |
| 81 | +type CollectionChangeHandler struct { |
| 82 | + collection string |
| 83 | + action string |
| 84 | +} |
| 85 | + |
| 86 | +func NewCollectionChangeHandler(collection, action string) *CollectionChangeHandler { |
| 87 | + return &CollectionChangeHandler{ |
| 88 | + collection: collection, |
| 89 | + action: action, |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func (h *CollectionChangeHandler) Handle(payload *WebhookPayload) error { |
| 94 | + // Handle the specific collection change |
| 95 | + fmt.Printf("Handling %s action for collection %s\n", payload.Action, payload.Collection) |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func (h *CollectionChangeHandler) ShouldHandle(payload *WebhookPayload) bool { |
| 100 | + return payload.Collection == h.collection && payload.Action == h.action |
| 101 | +} |
0 commit comments