dyndns/webapi/config.go
Torben Nehmer a6e52d9d3d Fix: viper.UnmarshalKey ignores defaults
This seems to be a bug in viper, as soon as we're working with subkeys, things fall apart
2021-08-24 17:03:59 +02:00

39 lines
757 B
Go

package webapi
import (
"encoding/json"
"log"
"github.com/spf13/viper"
)
type config struct {
ListenAddress string
}
var C config
func init() {
SetConfigDefaults()
}
func SetConfigDefaults() {
viper.SetDefault("WebAPI.ListenAddress", ":8080")
}
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("WebAPI", &C)
C.ListenAddress = viper.GetString("WebAPI.ListenAddress")
}
func (obj *config) PrettyPrint() string {
s, err := json.MarshalIndent(obj, "", " ")
if err != nil {
log.Fatalf("Failed to pretty print WebAPI Config via JSON: %v", err)
}
return string(s)
}