Initial Notifiers implementation

This commit is contained in:
mkelcik
2023-05-04 11:44:27 +02:00
parent 2d52cbe920
commit ffd5253f59
7 changed files with 211 additions and 8 deletions

44
notifications/types.go Normal file
View 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
}