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)
|
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) {
|
func getResolver(resolverName string) (PublicIpResolver, string) {
|
||||||
switch resolverName {
|
switch resolverName {
|
||||||
// HERE add another resolver if needed
|
// HERE add another resolver if needed
|
||||||
@@ -67,7 +52,7 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
notifiers := getNotifiers(config.Notifiers)
|
notifiers := notifications.GetNotifiers(config.Notifiers)
|
||||||
|
|
||||||
// public ip resolver
|
// public ip resolver
|
||||||
publicIpResolver, resolverTag := getResolver(config.PublicIpResolverTag)
|
publicIpResolver, resolverTag := getResolver(config.PublicIpResolverTag)
|
||||||
|
|||||||
@@ -3,12 +3,18 @@ package notifications
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
configDelimiter = "@"
|
||||||
|
)
|
||||||
|
|
||||||
type Notifiers []Notifier
|
type Notifiers []Notifier
|
||||||
|
|
||||||
func (n Notifiers) NotifyWithLog(ctx context.Context, notification Notification) error {
|
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 {
|
for _, notifier := range n {
|
||||||
if err := notifier.Notify(ctx, notification); err != nil {
|
if err := notifier.Notify(ctx, notification); err != nil {
|
||||||
outErr = errors.Join(outErr, err)
|
outErr = errors.Join(outErr, err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
log.Printf("Notification sent via %s\n", notifier.Tag())
|
log.Printf("Notification sent via %s\n", notifier.Tag())
|
||||||
}
|
}
|
||||||
@@ -30,9 +37,19 @@ type Notification struct {
|
|||||||
Domain string `json:"domain"`
|
Domain string `json:"domain"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var Available = map[string]func() (Notifier, error){
|
func (n Notification) ToSlice() []string {
|
||||||
webhookTag: func() (Notifier, error) {
|
return []string{n.OldIp.String(), n.NewIp.String(), n.CheckedAt.Format(time.RFC3339), n.ResolverTag, n.Domain}
|
||||||
return NewWebhookNotification(NewWebhookConfigFromEnv(), &http.Client{
|
}
|
||||||
|
|
||||||
|
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,
|
Timeout: 10 * time.Second,
|
||||||
}), nil
|
}), nil
|
||||||
},
|
},
|
||||||
@@ -42,3 +59,18 @@ type Notifier interface {
|
|||||||
Tag() string
|
Tag() string
|
||||||
Notify(ctx context.Context, notification Notification) error
|
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"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
webhookTag = "webhook"
|
webhookTag = "webhook"
|
||||||
webhookRequestTypeJson = "JSON"
|
|
||||||
envWebhookUrl = "WEBHOOK_RL"
|
|
||||||
envWebhookRequestType = "WEBHOOK_REQ_TYPE"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Doer interface {
|
type Doer interface {
|
||||||
@@ -24,14 +19,6 @@ type Doer interface {
|
|||||||
|
|
||||||
type WebhookConfig struct {
|
type WebhookConfig struct {
|
||||||
Url string
|
Url string
|
||||||
Json bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewWebhookConfigFromEnv() WebhookConfig {
|
|
||||||
return WebhookConfig{
|
|
||||||
Url: os.Getenv(envWebhookUrl),
|
|
||||||
Json: strings.ToUpper(os.Getenv(envWebhookRequestType)) == webhookRequestTypeJson,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type WebhookNotification struct {
|
type WebhookNotification struct {
|
||||||
@@ -48,12 +35,9 @@ func NewWebhookNotification(config WebhookConfig, client Doer) *WebhookNotificat
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w WebhookNotification) getRequestBody(notification Notification) (io.Reader, error) {
|
func (w WebhookNotification) getRequestBody(notification Notification) (io.Reader, error) {
|
||||||
out := bytes.NewBuffer(notification.NewIp)
|
out := bytes.NewBuffer(nil)
|
||||||
if w.config.Json {
|
|
||||||
if err := json.NewEncoder(out).Encode(notification); err != nil {
|
if err := json.NewEncoder(out).Encode(notification); err != nil {
|
||||||
return nil, fmt.Errorf("error encoding notification body: %w", err)
|
return nil, fmt.Errorf("error encoding json notification body: %w", err)
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
}
|
||||||
return out, nil
|
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