Torben Nehmer
a6e52d9d3d
This seems to be a bug in viper, as soon as we're working with subkeys, things fall apart
53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type config struct {
|
|
DNS configDNS
|
|
Users configUsers
|
|
}
|
|
|
|
type configDNS struct {
|
|
Server string
|
|
DefaultTTL uint32
|
|
}
|
|
|
|
type configUsers struct {
|
|
ConfigDir string
|
|
}
|
|
|
|
var C config
|
|
|
|
func init() {
|
|
SetConfigDefaults()
|
|
}
|
|
|
|
func SetConfigDefaults() {
|
|
viper.SetDefault("Service.DNS.Server", "127.0.0.1:53")
|
|
viper.SetDefault("Service.DNS.DefaultTTL", 60)
|
|
viper.SetDefault("Service.Users.ConfigDir", "users/")
|
|
}
|
|
|
|
func LoadConfig() {
|
|
// 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")
|
|
C.Users.ConfigDir = viper.GetString("Service.Users.ConfigDir")
|
|
}
|
|
|
|
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)
|
|
}
|