feat(healthchecksio): option HEALTH_HEALTHCHECKSIO_BASE_URL

This commit is contained in:
Quentin McGaw
2024-04-30 12:59:01 +00:00
parent 11575ee82a
commit 937a249ffa
5 changed files with 22 additions and 12 deletions

View File

@@ -99,6 +99,7 @@ ENV \
SHOUTRRR_DEFAULT_TITLE="DDNS Updater" \
TZ= \
HEALTH_SERVER_ADDRESS=127.0.0.1:9999 \
HEALTH_HEALTHCHECKSIO_BASE_URL=https://hc-ping.com \
HEALTH_HEALTHCHECKSIO_UUID=
ARG VERSION=unknown
ARG CREATED="an unknown date"

View File

@@ -249,6 +249,7 @@ Note that:
| `LISTENING_ADDRESS` | `:8000` | Internal TCP listening port for the web UI |
| `ROOT_URL` | `/` | URL path to append to all paths to the webUI (i.e. `/ddns` for accessing `https://example.com/ddns` through a proxy) |
| `HEALTH_SERVER_ADDRESS` | `127.0.0.1:9999` | Health server listening address |
| `HEALTH_HEALTHCHECKSIO_BASE_URL` | `https://hc-ping.com` | Base URL for the [healthchecks.io](https://healthchecks.io) server |
| `HEALTH_HEALTHCHECKSIO_UUID` | | UUID to idenfity with the [healthchecks.io](https://healthchecks.io) server |
| `DATADIR` | `/updater/data` | Directory to read and write data files from internally |
| `BACKUP_PERIOD` | `0` | Set to a period (i.e. `72h15m`) to enable zip backups of data/config.json and data/updates.json in a zip file |

View File

@@ -249,7 +249,8 @@ func _main(ctx context.Context, reader *reader.Reader, args []string, logger log
return fmt.Errorf("creating resolver: %w", err)
}
hioClient := healthchecksio.New(client, *config.Health.HealthchecksioUUID)
hioClient := healthchecksio.New(client, config.Health.HealthchecksioBaseURL,
*config.Health.HealthchecksioUUID)
updater := update.NewUpdater(db, client, shoutrrrClient, logger, timeNow)
runner := update.NewRunner(db, updater, ipGetter, config.Update.Period,

View File

@@ -2,6 +2,7 @@ package config
import (
"fmt"
"net/url"
"os"
"github.com/qdm12/gosettings"
@@ -11,12 +12,14 @@ import (
)
type Health struct {
ServerAddress *string
HealthchecksioUUID *string
ServerAddress *string
HealthchecksioBaseURL string
HealthchecksioUUID *string
}
func (h *Health) SetDefaults() {
h.ServerAddress = gosettings.DefaultPointer(h.ServerAddress, "127.0.0.1:9999")
h.HealthchecksioBaseURL = gosettings.DefaultComparable(h.HealthchecksioBaseURL, "https://hc-ping.com")
h.HealthchecksioUUID = gosettings.DefaultPointer(h.HealthchecksioUUID, "")
}
@@ -26,6 +29,11 @@ func (h Health) Validate() (err error) {
return fmt.Errorf("server listening address: %w", err)
}
_, err = url.Parse(h.HealthchecksioBaseURL)
if err != nil {
return fmt.Errorf("healthchecks.io base URL: %w", err)
}
return nil
}
@@ -37,6 +45,7 @@ func (h Health) toLinesNode() *gotree.Node {
node := gotree.New("Health")
node.Appendf("Server listening address: %s", *h.ServerAddress)
if *h.HealthchecksioUUID != "" {
node.Appendf("Healthchecks.io base URL: %s", h.HealthchecksioBaseURL)
node.Appendf("Healthchecks.io UUID: %s", *h.HealthchecksioUUID)
}
return node
@@ -44,5 +53,6 @@ func (h Health) toLinesNode() *gotree.Node {
func (h *Health) Read(reader *reader.Reader) {
h.ServerAddress = reader.Get("HEALTH_SERVER_ADDRESS")
h.HealthchecksioBaseURL = reader.String("HEALTH_HEALTHCHECKSIO_BASE_URL")
h.HealthchecksioUUID = reader.Get("HEALTH_HEALTHCHECKSIO_UUID")
}

View File

@@ -5,20 +5,21 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
)
// New creates a new healthchecks.io client.
// If passed an empty uuid string, it acts as no-op implementation.
func New(httpClient *http.Client, uuid string) *Client {
func New(httpClient *http.Client, baseURL, uuid string) *Client {
return &Client{
httpClient: httpClient,
baseURL: baseURL,
uuid: uuid,
}
}
type Client struct {
httpClient *http.Client
baseURL string
uuid string
}
@@ -41,16 +42,12 @@ func (c *Client) Ping(ctx context.Context, state State) (err error) {
return nil
}
url := url.URL{
Scheme: "https",
Host: "hc-ping.com",
Path: "/" + c.uuid,
}
url := c.baseURL + "/" + c.uuid
if state != Ok {
url.Path += "/" + string(state)
url += "/" + string(state)
}
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil)
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return fmt.Errorf("creating request: %w", err)
}