Skip to content

Commit

Permalink
feat: client interacts with server running on Lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenschneider committed Oct 4, 2023
1 parent bfe821f commit 858b1e2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
7 changes: 7 additions & 0 deletions client/dispatch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package client

import "github.com/soerenschneider/dyndns/internal/common"

type EventDispatch interface {
Notify(msg *common.UpdateRecordRequest) error
}
43 changes: 43 additions & 0 deletions client/http_dispatch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package client

import (
"bytes"
"encoding/json"
"net/http"
"time"

"github.com/hashicorp/go-retryablehttp"
"github.com/rs/zerolog/log"
"github.com/soerenschneider/dyndns/internal/common"
)

type HttpDispatch struct {
client *http.Client
url string
}

func NewHttpDispatcher(url string) (*HttpDispatch, error) {
client := retryablehttp.NewClient()
client.RetryMax = 3
client.HTTPClient.Timeout = 5 * time.Second

return &HttpDispatch{
client: client.HTTPClient,
url: url,
}, nil
}

func (h *HttpDispatch) Notify(msg *common.UpdateRecordRequest) error {
data, err := json.Marshal(msg)
if err != nil {
return err
}

response, err := h.client.Post(h.url, "application/json", bytes.NewBuffer(data))
if err != nil {
return err
}

log.Info().Int("status", response.StatusCode).Msg("Received reply")
return nil
}
34 changes: 29 additions & 5 deletions cmd/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
"github.com/soerenschneider/dyndns/client/resolvers"
"github.com/soerenschneider/dyndns/conf"
"github.com/soerenschneider/dyndns/internal"
"github.com/soerenschneider/dyndns/internal/events"
"github.com/soerenschneider/dyndns/internal/events/mqtt"
"github.com/soerenschneider/dyndns/internal/metrics"
"github.com/soerenschneider/dyndns/internal/notification"
"github.com/soerenschneider/dyndns/internal/util"
"github.com/soerenschneider/dyndns/internal/verification"
"github.com/soerenschneider/dyndns/internal/verification/key_provider"
"go.uber.org/multierr"
)

var (
Expand Down Expand Up @@ -77,6 +77,30 @@ func dieOnError(err error, msg string) {
}
}

func buildNotifiers(config *conf.ClientConf) (map[string]client.EventDispatch, error) {
dispatchers := map[string]client.EventDispatch{}

var errs, err error
if len(config.Brokers) > 0 {
for _, broker := range config.Brokers {
dispatchers[broker], err = mqtt.NewMqttClient(broker, config.ClientId, fmt.Sprintf("dyndns/%s", config.Host), config.TlsConfig())
if err != nil {
errs = multierr.Append(errs, err)
}
}
}

if len(config.HttpConfig.Url) > 0 {
httpDispatcher, err := client.NewHttpDispatcher(config.HttpConfig.Url)
if err != nil {
errs = multierr.Append(errs, err)
}
dispatchers[config.HttpConfig.Url] = httpDispatcher
}

return dispatchers, errs
}

func RunClient(config *conf.ClientConf) {
metrics.Version.WithLabelValues(internal.BuildVersion, internal.CommitHash).SetToCurrentTime()
metrics.ProcessStartTime.SetToCurrentTime()
Expand All @@ -93,10 +117,10 @@ func RunClient(config *conf.ClientConf) {
resolver, err := buildResolver(config)
dieOnError(err, "could not build ip resolver")

dispatchers := map[string]events.EventDispatch{}
for _, broker := range config.Brokers {
dispatchers[broker], err = mqtt.NewMqttClient(broker, config.ClientId, fmt.Sprintf("dyndns/%s", config.Host), config.TlsConfig())
dieOnError(err, "Could not build mqtt dispatcher")
dispatchers, err := buildNotifiers(config)
dieOnError(err, "Could not build mqtt dispatcher")
if len(dispatchers) == 0 {
log.Fatal().Msg("no dispatchers defined")
}

reconciler, err := client.NewReconciler(dispatchers)
Expand Down
4 changes: 4 additions & 0 deletions conf/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type ClientConf struct {
FallbackUrls []string `json:"http_resolver_fallback_urls,omitempty" env:"DYNDNS_HTTP_RESOLVER_FALLBACK_URLS" envSeparator:";"`
NetworkInterface string `json:"interface,omitempty"`
Once bool // this is not parsed via json, it's an cli flag

HttpConfig struct {
Url string `json:"url"`
} `json:"http"`
MqttConfig
*EmailConfig `json:"notifications"`
}
Expand Down

0 comments on commit 858b1e2

Please sign in to comment.