traefik/pkg/updater/provider.go
2025-05-29 16:29:59 +03:00

49 lines
1 KiB
Go

package updater
import (
"bytes"
"encoding/json"
"net/http"
"github.com/rs/zerolog/log"
"github.com/traefik/traefik/v3/pkg/config/dynamic"
"github.com/traefik/traefik/v3/pkg/config/static"
"github.com/traefik/traefik/v3/pkg/safe"
)
type Updater struct {
callbackUrls []string
}
func New(config *static.Configuration) *Updater {
updater := &Updater{
callbackUrls: config.Global.UpdaterCallbacks,
}
return updater
}
func (u *Updater) HandleConfigUpdate(cfg dynamic.Configuration) {
body, err := json.Marshal(cfg)
if err != nil {
// should never happen?
log.Error().Err(err).Msg("Error while marshalling dynamic configuration data to json")
return
}
requestBody := bytes.NewBuffer(body)
for _, url := range u.callbackUrls {
safe.Go(func() {
resp, err := http.Post(url, "application/json", requestBody)
if err != nil {
log.Error().Err(err).Str("url", url).Msg("Error while sending configuration data to callback")
} else {
log.Debug().Str("url", url).Msg("Configuration data sent")
resp.Body.Close()
}
})
}
}