dyndns/webapi/config.go

42 lines
874 B
Go
Raw Normal View History

package webapi
import (
"encoding/json"
"log"
"github.com/spf13/viper"
)
type config struct {
ListenAddress string
AllowHello bool
}
var C config
func init() {
SetConfigDefaults()
}
func SetConfigDefaults() {
viper.SetDefault("WebAPI.ListenAddress", ":8080")
viper.SetDefault("WebAPI.AllowHello", false)
}
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")
C.AllowHello = viper.GetBool("WebAPI.AllowHello")
}
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)
}