From be6bd96cf3d1c45ec637483d266825463cf52744 Mon Sep 17 00:00:00 2001 From: Nathan Gaberel Date: Wed, 17 Jan 2024 23:40:55 -0800 Subject: [PATCH] refactor: cleaner config unmarshalling. --- README.md | 8 ++++---- internal/config/load.go | 29 ++++++----------------------- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index a6f1096..43a4b3c 100644 --- a/README.md +++ b/README.md @@ -127,15 +127,15 @@ All configuration is passed as environment variables. | Variable name | Default | Description | | -------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | `SERVICE_DOMAIN` | | Domain under which service subdomains should be created. Any service with a declared domain that does not match "\*.$SERVICE_DOMAIN" will be ignored. Bingo only ever creates or deletes subdomains of `SERVICE_DOMAIN`. | -| `PROXY_TYPE` | `fabio` | The type of proxy to fetch services from. Supports "fabio" or "traefik". | +| `PROXY_TYPE` | `fabio` | The type of proxy to fetch services from. Supports "fabio" or "traefik". | | `PROXY_POLL_INTERVAL` | `5s` | Time interval between requests to reverse proxy. | -| `FABIO_HOSTS` | | List of space-separated hosts where Fabio is running. | +| `FABIO_HOSTS` | | List of comma-separated hosts where Fabio is running. | | `FABIO_ADMIN_PORT` | `9998` | Fabio's [admin UI port](https://fabiolb.net/ref/ui.addr/). | | `FABIO_SCHEME` | `http` | URI scheme for Fabio | -| `TRAEFIK_HOSTS` | | List of space-separated hosts where Traefik is running. | +| `TRAEFIK_HOSTS` | | List of comma-separated hosts where Traefik is running. | | `TRAEFIK_ADMIN_PORT` | `8080` | Traefik's [API port](https://doc.traefik.io/traefik/operations/api/). | | `TRAEFIK_SCHEME` | `http` | URI scheme for Traefik | -| `TRAEFIK_ENTRYPOINTS` | | List of space-separated Traefik entrypoints to watch. Only services mapped to these entry points will be managed. | +| `TRAEFIK_ENTRYPOINTS` | | List of comma-separated Traefik entrypoints to watch. Only services mapped to these entry points will be managed. | | `NAMESERVER_TYPE` | `pihole` | The type of nameserver to managed records in. Supports "pihole" or "route53". | | `NAMESERVER_POLL_INTERVAL` | `30s` | Time interval between requests to nameserver. | | `PIHOLE_URL` | | Address of the Pi-hole instance. | diff --git a/internal/config/load.go b/internal/config/load.go index ae80abe..7d5483c 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -3,7 +3,6 @@ package config import ( "fmt" "log/slog" - "strings" "time" "github.com/mitchellh/mapstructure" @@ -53,31 +52,15 @@ func Load() (*Config, error) { viper.BindEnv("Prometheus.MetricsPath", "PROMETHEUS_METRICS_PATH") config := &Config{} - err := viper.Unmarshal(config, viper.DecodeHook(mapstructure.TextUnmarshallerHookFunc())) + err := viper.Unmarshal(config, viper.DecodeHook( + mapstructure.ComposeDecodeHookFunc( + mapstructure.StringToSliceHookFunc(","), + mapstructure.TextUnmarshallerHookFunc(), + ), + )) if err != nil { return nil, fmt.Errorf("couldn't parse config: %w", err) } - // Manual fixes - if len(config.Proxy.Fabio.Hosts) > 0 { - config.Proxy.Fabio.Hosts = splitAndFilter(config.Proxy.Fabio.Hosts[0]) - } - if len(config.Proxy.Traefik.Hosts) > 0 { - config.Proxy.Traefik.Hosts = splitAndFilter(config.Proxy.Traefik.Hosts[0]) - } - if len(config.Proxy.Traefik.EntryPoints) > 0 { - config.Proxy.Traefik.EntryPoints = splitAndFilter(config.Proxy.Traefik.EntryPoints[0]) - } - return config, config.Validate() } - -func splitAndFilter(data string) (ret []string) { - for _, val := range strings.Split(data, " ") { - if val == "" { - continue - } - ret = append(ret, val) - } - return -}