2021-08-15 19:37:11 +00:00
|
|
|
package service
|
|
|
|
|
2021-08-22 12:46:48 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
2021-08-25 19:17:19 +00:00
|
|
|
"path/filepath"
|
2021-08-22 12:46:48 +00:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
2021-08-15 19:37:11 +00:00
|
|
|
|
|
|
|
type config struct {
|
2021-08-22 12:46:48 +00:00
|
|
|
DNS configDNS
|
|
|
|
Users configUsers
|
|
|
|
}
|
|
|
|
|
|
|
|
type configDNS struct {
|
|
|
|
Server string
|
|
|
|
DefaultTTL uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
type configUsers struct {
|
|
|
|
ConfigDir string
|
2021-08-15 19:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var C config
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
SetConfigDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetConfigDefaults() {
|
2021-08-24 14:49:17 +00:00
|
|
|
viper.SetDefault("Service.DNS.Server", "127.0.0.1:53")
|
2021-08-15 19:37:11 +00:00
|
|
|
viper.SetDefault("Service.DNS.DefaultTTL", 60)
|
2021-08-21 18:34:45 +00:00
|
|
|
viper.SetDefault("Service.Users.ConfigDir", "users/")
|
2021-08-15 19:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfig() {
|
2021-08-24 15:03:59 +00:00
|
|
|
// Does not work with partially overrides, only GetXXX does resolve the subkeys correctly
|
|
|
|
// https://github.com/spf13/viper/issues/798
|
|
|
|
// https://github.com/spf13/viper/issues/309
|
|
|
|
// viper.UnmarshalKey("Service", &C)
|
|
|
|
C.DNS.Server = viper.GetString("Service.DNS.Server")
|
|
|
|
C.DNS.DefaultTTL = viper.GetUint32("Service.DNS.DefaultTTL")
|
2021-08-25 19:17:19 +00:00
|
|
|
path := viper.GetString("Service.Users.ConfigDir")
|
|
|
|
if filepath.IsAbs(path) {
|
|
|
|
C.Users.ConfigDir = filepath.Clean(path)
|
|
|
|
} else {
|
|
|
|
C.Users.ConfigDir = filepath.Join(viper.GetString("ConfigDir"), path)
|
|
|
|
}
|
2021-08-22 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (obj *config) PrettyPrint() string {
|
|
|
|
s, err := json.MarshalIndent(obj, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to pretty print Service Config via JSON: %v", err)
|
|
|
|
}
|
|
|
|
return string(s)
|
2021-08-15 19:37:11 +00:00
|
|
|
}
|