processHTTP sends a webhook notification via HTTP POST request. Parameters: - data NewWebhook: The webhook notification data to send. - client *http.Client: The HTTP client to use for the request. Returns: - error: An error if the request or processing fails.
(data NewWebhook, client *http.Client)
| 73 | // Returns: |
| 74 | // - error: An error if the request or processing fails. |
| 75 | func processHTTP(data NewWebhook, client *http.Client) error { |
| 76 | conf, err := config.Fetch() |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | payloadBytes, err := json.Marshal(data) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | |
| 86 | secret := conf.Server.SecretKey |
| 87 | |
| 88 | timestamp := strconv.FormatInt(time.Now().Unix(), 10) |
| 89 | |
| 90 | signatureData := timestamp + "." + string(payloadBytes) |
| 91 | mac := hmac.New(sha256.New, []byte(secret)) |
| 92 | mac.Write([]byte(signatureData)) |
| 93 | signature := hex.EncodeToString(mac.Sum(nil)) |
| 94 | |
| 95 | req, err := http.NewRequest( |
| 96 | "POST", |
| 97 | conf.Notification.Webhook.Url, |
| 98 | bytes.NewBuffer(payloadBytes), |
| 99 | ) |
| 100 | if err != nil { |
| 101 | return err |
| 102 | } |
| 103 | |
| 104 | req.Header.Set("Content-Type", "application/json") |
| 105 | req.Header.Set("X-LedgerForge-Signature", signature) |
| 106 | req.Header.Set("X-LedgerForge-Timestamp", timestamp) |
| 107 | |
| 108 | for key, value := range conf.Notification.Webhook.Headers { |
| 109 | req.Header.Set(key, value) |
| 110 | } |
| 111 | |
| 112 | resp, err := client.Do(req) |
| 113 | if err != nil { |
| 114 | return err |
| 115 | } |
| 116 | defer func() { _ = resp.Body.Close() }() |
| 117 | |
| 118 | if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 119 | logrus.Warnf("Webhook failed with status %d", resp.StatusCode) |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | return nil |
| 124 | } |
| 125 | |
| 126 | // SendWebhook enqueues a webhook notification task using the LedgerForge instance's asynq client. |
| 127 | // |