2021-08-15 19:37:11 +00:00
|
|
|
package webapi
|
|
|
|
|
|
|
|
import (
|
2021-08-22 12:46:48 +00:00
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
|
2021-08-15 19:37:11 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
ListenAddress string
|
|
|
|
}
|
|
|
|
|
|
|
|
var C config
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
SetConfigDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetConfigDefaults() {
|
|
|
|
viper.SetDefault("WebAPI.ListenAddress", ":8080")
|
|
|
|
}
|
|
|
|
|
|
|
|
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("WebAPI", &C)
|
|
|
|
C.ListenAddress = viper.GetString("WebAPI.ListenAddress")
|
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 WebAPI Config via JSON: %v", err)
|
|
|
|
}
|
|
|
|
return string(s)
|
2021-08-15 19:37:11 +00:00
|
|
|
}
|