You've already forked ddns-updater
Refactor
This commit is contained in:
17
main.go
17
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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user