You've already forked ddns-updater
Initial Notifiers implementation
This commit is contained in:
44
notifications/types.go
Normal file
44
notifications/types.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Notifiers []Notifier
|
||||
|
||||
func (n Notifiers) NotifyWithLog(ctx context.Context, notification Notification) error {
|
||||
var outErr error
|
||||
for _, notifier := range n {
|
||||
if err := notifier.Notify(ctx, notification); err != nil {
|
||||
outErr = errors.Join(outErr, err)
|
||||
}
|
||||
log.Printf("Notification sent via %s\n", notifier.Tag())
|
||||
}
|
||||
return outErr
|
||||
}
|
||||
|
||||
type Notification struct {
|
||||
OldIp net.IP `json:"old_ip,omitempty"`
|
||||
NewIp net.IP `json:"new_ip"`
|
||||
CheckedAt time.Time `json:"checked_at"`
|
||||
ResolverTag string `json:"resolver_tag"`
|
||||
Domain string `json:"domain"`
|
||||
}
|
||||
|
||||
var Available = map[string]func() (Notifier, error){
|
||||
webhookTag: func() (Notifier, error) {
|
||||
return NewWebhookNotification(NewWebhookConfigFromEnv(), &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}), nil
|
||||
},
|
||||
}
|
||||
|
||||
type Notifier interface {
|
||||
Tag() string
|
||||
Notify(ctx context.Context, notification Notification) error
|
||||
}
|
||||
82
notifications/webhook.go
Normal file
82
notifications/webhook.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
webhookTag = "webhook"
|
||||
webhookRequestTypeJson = "JSON"
|
||||
envWebhookUrl = "WEBHOOK_RL"
|
||||
envWebhookRequestType = "WEBHOOK_REQ_TYPE"
|
||||
)
|
||||
|
||||
type Doer interface {
|
||||
Do(*http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
type WebhookConfig struct {
|
||||
Url string
|
||||
Json bool
|
||||
}
|
||||
|
||||
func NewWebhookConfigFromEnv() WebhookConfig {
|
||||
return WebhookConfig{
|
||||
Url: os.Getenv(envWebhookUrl),
|
||||
Json: strings.ToUpper(os.Getenv(envWebhookRequestType)) == webhookRequestTypeJson,
|
||||
}
|
||||
}
|
||||
|
||||
type WebhookNotification struct {
|
||||
config WebhookConfig
|
||||
client Doer
|
||||
}
|
||||
|
||||
func (w WebhookNotification) Tag() string {
|
||||
return webhookTag
|
||||
}
|
||||
|
||||
func NewWebhookNotification(config WebhookConfig, client Doer) *WebhookNotification {
|
||||
return &WebhookNotification{config: config, client: client}
|
||||
}
|
||||
|
||||
func (w WebhookNotification) getRequestBody(notification Notification) (io.Reader, error) {
|
||||
out := bytes.NewBuffer(notification.NewIp)
|
||||
if w.config.Json {
|
||||
if err := json.NewEncoder(out).Encode(notification); err != nil {
|
||||
return nil, fmt.Errorf("error encoding notification body: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (w WebhookNotification) Notify(ctx context.Context, notification Notification) error {
|
||||
body, err := w.getRequestBody(notification)
|
||||
if err != nil {
|
||||
return fmt.Errorf("WebhookNotification::NotifyWithLog error: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, w.config.Url, body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("WebhookNotification::NotifyWithLog error creating request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := w.client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("WebhookNotification::NotifyWithLog error while sending notification: %w", err)
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
|
||||
if resp.StatusCode >= 300 {
|
||||
return fmt.Errorf("WebhookNotification::NotifyWithLog unexpected non 2xx code %d returned", resp.StatusCode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
46
notifications/webhook_test.go
Normal file
46
notifications/webhook_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"io"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWebhookNotification_getRequestBody(t *testing.T) {
|
||||
type fields struct {
|
||||
config WebhookConfig
|
||||
}
|
||||
type args struct {
|
||||
notification Notification
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want io.Reader
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "text",
|
||||
fields: fields{},
|
||||
args: args{},
|
||||
want: nil,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
w := WebhookNotification{
|
||||
config: tt.fields.config,
|
||||
}
|
||||
got, err := w.getRequestBody(tt.args.notification)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("getRequestBody() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("getRequestBody() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user