Skip to content

Commit

Permalink
refactor: cleaner config unmarshalling.
Browse files Browse the repository at this point in the history
  • Loading branch information
n6g7 committed Jan 18, 2024
1 parent 7193ae2 commit be6bd96
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
29 changes: 6 additions & 23 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"fmt"
"log/slog"
"strings"
"time"

"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -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
}

0 comments on commit be6bd96

Please sign in to comment.