91 lines
1.6 KiB
Go
91 lines
1.6 KiB
Go
// 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
|
|
}
|