From 5c7ae6ab8ac93aa09850fb32659356ec0effd652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ask=20Bj=C3=B8rn=20Hansen?= Date: Sun, 10 Dec 2023 20:43:38 -0800 Subject: [PATCH] Basic config package to parse NTP Pool system config --- config/config.go | 90 +++++++++++++++++++++++++++++++++++++++ config/config_accessor.go | 31 ++++++++++++++ config/config_test.go | 26 +++++++++++ 3 files changed, 147 insertions(+) create mode 100644 config/config.go create mode 100644 config/config_accessor.go create mode 100644 config/config_test.go diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..2474e69 --- /dev/null +++ b/config/config.go @@ -0,0 +1,90 @@ +// Package config provides NTP Pool specific +// configuration tools. +package config + +import ( + "net/url" + "os" + "strconv" + "strings" + + "go.ntppool.org/common/logger" +) + +//go:generate accessory -type Config + +type Config struct { + deploymentMode string `accessor:"getter"` + + manageHostname string `accessor:"getter"` + manageTLS bool + + webHostname string `accessor:"getter"` + webHostnames []string + webTLS bool + + valid bool `accessor:"getter"` +} + +func New() *Config { + c := Config{} + c.deploymentMode = os.Getenv("deployment_mode") + c.manageHostname = os.Getenv("manage_hostname") + + c.webHostnames = strings.Split(os.Getenv("web_hostname"), ",") + for i, h := range c.webHostnames { + c.webHostnames[i] = strings.TrimSpace(h) + } + if len(c.webHostnames) > 0 { + c.webHostname = c.webHostnames[0] + c.valid = true + } + + c.manageTLS = parseBool(os.Getenv("manage_tls")) + c.webTLS = parseBool(os.Getenv("web_tls")) + + return &c +} + +func (c *Config) WebURL(path string, query *url.Values) string { + return baseURL(c.webHostname, c.webTLS, path, query) +} + +func baseURL(host string, tls bool, path string, query *url.Values) string { + uri := url.URL{} + uri.Host = host + if tls { + uri.Scheme = "https" + } else { + uri.Scheme = "http" + } + uri.Path = path + if query != nil { + uri.RawQuery = query.Encode() + } + + return uri.String() +} + +func parseBool(s string) bool { + switch strings.ToLower(s) { + case "yes": + return true + case "y": + return true + case "no": + return false + case "n": + return false + case "": + return false + } + + t, err := strconv.ParseBool(s) + if err != nil { + logger.Setup().Error("could not parse bool", "string", s, "err", err) + return false + } + + return t +} diff --git a/config/config_accessor.go b/config/config_accessor.go new file mode 100644 index 0000000..2362516 --- /dev/null +++ b/config/config_accessor.go @@ -0,0 +1,31 @@ +// Code generated by accessory; DO NOT EDIT. + +package config + +func (c *Config) DeploymentMode() string { + if c == nil { + return "" + } + return c.deploymentMode +} + +func (c *Config) ManageHostname() string { + if c == nil { + return "" + } + return c.manageHostname +} + +func (c *Config) WebHostname() string { + if c == nil { + return "" + } + return c.webHostname +} + +func (c *Config) Valid() bool { + if c == nil { + return false + } + return c.valid +} diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..b3a5f94 --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,26 @@ +package config + +import ( + "net/url" + "os" + "testing" +) + +func TestBaseURL(t *testing.T) { + + os.Setenv("web_hostname", "www.ntp.dev, web.ntppool.dev") + os.Setenv("web_tls", "yes") + + c := New() + if !c.Valid() { + t.Fatalf("config not valid") + } + + q := url.Values{} + q.Set("foo", "bar") + u := c.WebURL("/foo", &q) + if u != "https://www.ntp.dev/foo?foo=bar" { + t.Fatalf("unexpected WebURL: %s", u) + } + +}