diff options
| author | Ibrahim Konsowa <imkonsowa@gmail.com> | 2025-07-10 20:22:05 +0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-10 09:22:05 -0700 |
| commit | 93bbaa1fb486f95ff40f5891057f8415bdf3fc27 (patch) | |
| tree | 7365c496d06e00c594fcac35e706e29c507bce04 /weed/notification/webhook/http.go | |
| parent | 804979d68bdc1763878b91741e22c913fba62d73 (diff) | |
| download | seaweedfs-93bbaa1fb486f95ff40f5891057f8415bdf3fc27.tar.xz seaweedfs-93bbaa1fb486f95ff40f5891057f8415bdf3fc27.zip | |
[Notifications] Support webhook notifications (#6962)
Add webhook notification support
Diffstat (limited to 'weed/notification/webhook/http.go')
| -rw-r--r-- | weed/notification/webhook/http.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/weed/notification/webhook/http.go b/weed/notification/webhook/http.go new file mode 100644 index 000000000..13b7f30d9 --- /dev/null +++ b/weed/notification/webhook/http.go @@ -0,0 +1,57 @@ +package webhook + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + + util_http "github.com/seaweedfs/seaweedfs/weed/util/http" + "google.golang.org/protobuf/proto" +) + +type httpClient struct { + endpoint string + token string +} + +func newHTTPClient(cfg *config) (*httpClient, error) { + return &httpClient{ + endpoint: cfg.endpoint, + token: cfg.authBearerToken, + }, nil +} + +func (h *httpClient) sendMessage(key string, message proto.Message) error { + payload := map[string]interface{}{ + "key": key, + "message": message, + } + + jsonData, err := json.Marshal(payload) + if err != nil { + return fmt.Errorf("failed to marshal message: %v", err) + } + + req, err := http.NewRequest(http.MethodPost, h.endpoint, bytes.NewBuffer(jsonData)) + if err != nil { + return fmt.Errorf("failed to create request: %v", err) + } + + req.Header.Set("Content-Type", "application/json") + if h.token != "" { + req.Header.Set("Authorization", "Bearer "+h.token) + } + + resp, err := util_http.Do(req) + if err != nil { + return fmt.Errorf("failed to send request: %v", err) + } + defer resp.Body.Close() + + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return fmt.Errorf("webhook returned status code: %d", resp.StatusCode) + } + + return nil +} |
