mirror of
https://github.com/Awuqing/BackupX.git
synced 2026-07-08 01:41:22 +08:00
first commit
This commit is contained in:
55
server/internal/notify/webhook.go
Normal file
55
server/internal/notify/webhook.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type WebhookNotifier struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewWebhookNotifier() *WebhookNotifier {
|
||||
return &WebhookNotifier{client: &http.Client{Timeout: 10 * time.Second}}
|
||||
}
|
||||
func (n *WebhookNotifier) Type() string { return "webhook" }
|
||||
func (n *WebhookNotifier) SensitiveFields() []string { return []string{"secret"} }
|
||||
|
||||
func (n *WebhookNotifier) Validate(config map[string]any) error {
|
||||
if strings.TrimSpace(asString(config["url"])) == "" {
|
||||
return fmt.Errorf("webhook url is required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *WebhookNotifier) Send(ctx context.Context, config map[string]any, message Message) error {
|
||||
if err := n.Validate(config); err != nil {
|
||||
return err
|
||||
}
|
||||
body, err := json.Marshal(map[string]any{"title": message.Title, "body": message.Body, "fields": message.Fields})
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal webhook payload: %w", err)
|
||||
}
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimSpace(asString(config["url"])), bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return fmt.Errorf("create webhook request: %w", err)
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
if secret := strings.TrimSpace(asString(config["secret"])); secret != "" {
|
||||
request.Header.Set("X-BackupX-Secret", secret)
|
||||
}
|
||||
response, err := n.client.Do(request)
|
||||
if err != nil {
|
||||
return fmt.Errorf("send webhook request: %w", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode >= http.StatusBadRequest {
|
||||
return fmt.Errorf("webhook response status: %s", response.Status)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user