|
| 1 | +// Copyright 2023 Blink Labs, LLC. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package webhook |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "net/http" |
| 23 | + |
| 24 | + "github.com/blinklabs-io/snek/event" |
| 25 | + "github.com/blinklabs-io/snek/input/chainsync" |
| 26 | + "github.com/blinklabs-io/snek/internal/logging" |
| 27 | + "github.com/blinklabs-io/snek/internal/version" |
| 28 | +) |
| 29 | + |
| 30 | +type WebhookOutput struct { |
| 31 | + errorChan chan error |
| 32 | + eventChan chan event.Event |
| 33 | + url string |
| 34 | +} |
| 35 | + |
| 36 | +func New(options ...WebhookOptionFunc) *WebhookOutput { |
| 37 | + w := &WebhookOutput{ |
| 38 | + errorChan: make(chan error), |
| 39 | + eventChan: make(chan event.Event, 10), |
| 40 | + url: "http://localhost:3000", |
| 41 | + } |
| 42 | + for _, option := range options { |
| 43 | + option(w) |
| 44 | + } |
| 45 | + return w |
| 46 | +} |
| 47 | + |
| 48 | +// Start the webhook output |
| 49 | +func (w *WebhookOutput) Start() error { |
| 50 | + logger := logging.GetLogger() |
| 51 | + logger.Infof("starting webhook server") |
| 52 | + go func() { |
| 53 | + for { |
| 54 | + evt, ok := <-w.eventChan |
| 55 | + // Channel has been closed, which means we're shutting down |
| 56 | + if !ok { |
| 57 | + return |
| 58 | + } |
| 59 | + payload := evt.Payload |
| 60 | + if payload == nil { |
| 61 | + panic(fmt.Errorf("ERROR: %v", payload)) |
| 62 | + } |
| 63 | + logger.Infof("debug: type: %s", evt.Type) |
| 64 | + switch evt.Type { |
| 65 | + case "chainsync.block": |
| 66 | + be := payload.(chainsync.BlockEvent) |
| 67 | + evt.Payload = be |
| 68 | + case "chainsync.rollback": |
| 69 | + re := payload.(chainsync.RollbackEvent) |
| 70 | + evt.Payload = re |
| 71 | + case "chainsync.transaction": |
| 72 | + te := payload.(chainsync.TransactionEvent) |
| 73 | + evt.Payload = te |
| 74 | + default: |
| 75 | + logger.Errorf("unknown event type: %s", evt.Type) |
| 76 | + return |
| 77 | + } |
| 78 | + // TODO: error handle |
| 79 | + err := SendWebhook(&evt, w.url) |
| 80 | + if err != nil { |
| 81 | + logger.Errorf("ERROR: %s", err) |
| 82 | + } |
| 83 | + } |
| 84 | + }() |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func SendWebhook(e *event.Event, url string) error { |
| 89 | + logger := logging.GetLogger() |
| 90 | + logger.Infof("sending event %s to %s", e.Type, url) |
| 91 | + data, err := json.Marshal(e) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("%s", err) |
| 94 | + } |
| 95 | + // Setup request |
| 96 | + req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data)) |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("%s", err) |
| 99 | + } |
| 100 | + req.Header.Add("Content-Type", "application/json") |
| 101 | + req.Header.Add("User-Agent", fmt.Sprintf("Snek/%s", version.GetVersionString())) |
| 102 | + // Send payload |
| 103 | + resp, err := http.DefaultClient.Do(req) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("%s", err) |
| 106 | + } |
| 107 | + respBody, err := io.ReadAll(resp.Body) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("%s", err) |
| 110 | + } |
| 111 | + defer resp.Body.Close() |
| 112 | + |
| 113 | + logger.Infof("sent: %s, payload: %s, body: %s, response: %s, status: %d", |
| 114 | + url, |
| 115 | + string(data), |
| 116 | + string(respBody), |
| 117 | + resp.Status, |
| 118 | + resp.StatusCode, |
| 119 | + ) |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +// Stop the embedded output |
| 124 | +func (w *WebhookOutput) Stop() error { |
| 125 | + close(w.eventChan) |
| 126 | + close(w.errorChan) |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +// ErrorChan returns the input error channel |
| 131 | +func (w *WebhookOutput) ErrorChan() chan error { |
| 132 | + return w.errorChan |
| 133 | +} |
| 134 | + |
| 135 | +// InputChan returns the input event channel |
| 136 | +func (w *WebhookOutput) InputChan() chan<- event.Event { |
| 137 | + return w.eventChan |
| 138 | +} |
| 139 | + |
| 140 | +// OutputChan always returns nil |
| 141 | +func (w *WebhookOutput) OutputChan() <-chan event.Event { |
| 142 | + return nil |
| 143 | +} |
0 commit comments