From f911b9ff16bb1505ad2009b55a0071c514f9603e Mon Sep 17 00:00:00 2001 From: mkelcik Date: Thu, 4 May 2023 17:28:25 +0200 Subject: [PATCH] Refactor --- main.go | 17 +------------ notifications/types.go | 38 ++++++++++++++++++++++++++--- notifications/webhook.go | 26 ++++---------------- notifications/webhook_test.go | 46 ----------------------------------- 4 files changed, 41 insertions(+), 86 deletions(-) delete mode 100644 notifications/webhook_test.go diff --git a/main.go b/main.go index e7061e4..027c408 100644 --- a/main.go +++ b/main.go @@ -18,21 +18,6 @@ type PublicIpResolver interface { ResolvePublicIp(ctx context.Context) (net.IP, error) } -func getNotifiers(tags []string) notifications.Notifiers { - out := notifications.Notifiers{} - for _, t := range tags { - if initFn, ok := notifications.Available[t]; ok { - notifier, err := initFn() - if err != nil { - log.Println(err) - continue - } - out = append(out, notifier) - } - } - return out -} - func getResolver(resolverName string) (PublicIpResolver, string) { switch resolverName { // HERE add another resolver if needed @@ -67,7 +52,7 @@ func main() { log.Fatal(err) } - notifiers := getNotifiers(config.Notifiers) + notifiers := notifications.GetNotifiers(config.Notifiers) // public ip resolver publicIpResolver, resolverTag := getResolver(config.PublicIpResolverTag) diff --git a/notifications/types.go b/notifications/types.go index 26357a6..b41a73e 100644 --- a/notifications/types.go +++ b/notifications/types.go @@ -3,12 +3,18 @@ package notifications import ( "context" "errors" + "fmt" "log" "net" "net/http" + "strings" "time" ) +const ( + configDelimiter = "@" +) + type Notifiers []Notifier func (n Notifiers) NotifyWithLog(ctx context.Context, notification Notification) error { @@ -16,6 +22,7 @@ func (n Notifiers) NotifyWithLog(ctx context.Context, notification Notification) for _, notifier := range n { if err := notifier.Notify(ctx, notification); err != nil { outErr = errors.Join(outErr, err) + continue } log.Printf("Notification sent via %s\n", notifier.Tag()) } @@ -30,9 +37,19 @@ type Notification struct { Domain string `json:"domain"` } -var Available = map[string]func() (Notifier, error){ - webhookTag: func() (Notifier, error) { - return NewWebhookNotification(NewWebhookConfigFromEnv(), &http.Client{ +func (n Notification) ToSlice() []string { + return []string{n.OldIp.String(), n.NewIp.String(), n.CheckedAt.Format(time.RFC3339), n.ResolverTag, n.Domain} +} + +var Available = map[string]func(string) (Notifier, error){ + webhookTag: func(config string) (Notifier, error) { + parts := strings.Split(config, configDelimiter) + + if len(parts) < 2 { + return nil, fmt.Errorf("wrong webhook config, missing url part") + } + + return NewWebhookNotification(WebhookConfig{Url: parts[1]}, &http.Client{ Timeout: 10 * time.Second, }), nil }, @@ -42,3 +59,18 @@ type Notifier interface { Tag() string Notify(ctx context.Context, notification Notification) error } + +func GetNotifiers(tags []string) Notifiers { + out := Notifiers{} + for _, t := range tags { + if initFn, ok := Available[strings.Split(t, configDelimiter)[0]]; ok { + notifier, err := initFn(t) + if err != nil { + log.Println(err) + continue + } + out = append(out, notifier) + } + } + return out +} diff --git a/notifications/webhook.go b/notifications/webhook.go index 1f0b34b..6a5e192 100644 --- a/notifications/webhook.go +++ b/notifications/webhook.go @@ -7,15 +7,10 @@ import ( "fmt" "io" "net/http" - "os" - "strings" ) const ( - webhookTag = "webhook" - webhookRequestTypeJson = "JSON" - envWebhookUrl = "WEBHOOK_RL" - envWebhookRequestType = "WEBHOOK_REQ_TYPE" + webhookTag = "webhook" ) type Doer interface { @@ -23,15 +18,7 @@ type Doer interface { } type WebhookConfig struct { - Url string - Json bool -} - -func NewWebhookConfigFromEnv() WebhookConfig { - return WebhookConfig{ - Url: os.Getenv(envWebhookUrl), - Json: strings.ToUpper(os.Getenv(envWebhookRequestType)) == webhookRequestTypeJson, - } + Url string } type WebhookNotification struct { @@ -48,12 +35,9 @@ func NewWebhookNotification(config WebhookConfig, client Doer) *WebhookNotificat } 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 + out := bytes.NewBuffer(nil) + if err := json.NewEncoder(out).Encode(notification); err != nil { + return nil, fmt.Errorf("error encoding json notification body: %w", err) } return out, nil } diff --git a/notifications/webhook_test.go b/notifications/webhook_test.go deleted file mode 100644 index 648e60e..0000000 --- a/notifications/webhook_test.go +++ /dev/null @@ -1,46 +0,0 @@ -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) - } - }) - } -}