func loadConfig() API { log.Debug("server.config::loadConfig") var api API var plugins map[string]map[string]interface{} if err := viper.ReadInConfig(); err != nil { log.Fatal("Failed loading server.json ", err) } if err := viper.MarshalKey("api", &api); err != nil { log.Fatal("Malformed api section in server.json ", err) } if err := viper.MarshalKey("plugins", &plugins); err != nil { log.Fatal("Malformed plugins section in server.json ", err) } api.plugins = plugins return api }
func main() { viper.SetDefault("ConfigFile", "$HOME/.krb.jira.json") viper.SetConfigName(".krb.jira") viper.SetConfigType("json") //viper.AddConfigPath("$HOME") viper.AddConfigPath(".") viper.ReadInConfig() fmt.Printf("host:%s\n", viper.GetString("host")) fmt.Printf("port:%d\n", viper.GetInt("port")) fmt.Printf("user:%s\n", viper.GetString("user")) fmt.Printf("pass:%s\n", viper.GetString("pass")) apiInfo := &JiraApiInfo{} viper.MarshalKey("apiInfo", &apiInfo) fmt.Printf("apiInfo:%q\n", apiInfo) // NB: marshaling this whole struct doesn't seem to work? /* config := &ConfigInfo{} err := viper.Marshal(&config) if err != nil { panic(err) } fmt.Printf("config:%q\n", config) */ }
func (cfg *viperProvider) initLogging() { var C logger.LoggerConfig err := viper.MarshalKey("logger", &C) if err != nil { panic(fmt.Errorf("Can't parse: %s \n", err)) } logger.InitLog(C) cfg.log = logger.Log("config") }
func getGlobalProject(v *viper.Viper) (*libcentrifugo.Project, bool) { p := &libcentrifugo.Project{} // TODO: the same as for structureFromConfig function if v == nil { if !viper.IsSet("project_name") || viper.GetString("project_name") == "" { return nil, false } p.Name = libcentrifugo.ProjectKey(viper.GetString("project_name")) p.Secret = viper.GetString("project_secret") p.ConnLifetime = int64(viper.GetInt("project_connection_lifetime")) p.Anonymous = viper.GetBool("project_anonymous") p.Watch = viper.GetBool("project_watch") p.Publish = viper.GetBool("project_publish") p.JoinLeave = viper.GetBool("project_join_leave") p.Presence = viper.GetBool("project_presence") p.HistorySize = int64(viper.GetInt("project_history_size")) p.HistoryLifetime = int64(viper.GetInt("project_history_lifetime")) } else { if !v.IsSet("project_name") || v.GetString("project_name") == "" { return nil, false } p.Name = libcentrifugo.ProjectKey(v.GetString("project_name")) p.Secret = v.GetString("project_secret") p.ConnLifetime = int64(v.GetInt("project_connection_lifetime")) p.Anonymous = v.GetBool("project_anonymous") p.Watch = v.GetBool("project_watch") p.Publish = v.GetBool("project_publish") p.JoinLeave = v.GetBool("project_join_leave") p.Presence = v.GetBool("project_presence") p.HistorySize = int64(v.GetInt("project_history_size")) p.HistoryLifetime = int64(v.GetInt("project_history_lifetime")) } var nl []libcentrifugo.Namespace if v == nil { viper.MarshalKey("project_namespaces", &nl) } else { v.MarshalKey("project_namespaces", &nl) } p.Namespaces = nl return p, true }
func (cfg *viperProvider) initValues() { cfg.configValues = make(map[string]interface{}) for _, key := range viper.AllKeys() { configType, present := cfg.configTypes[key] if !present { continue } config := reflect.New(configType).Interface() err := viper.MarshalKey(key, config) if err != nil { cfg.log.Fatalf("Can't parse section %s, got: %v", key, err) } cfg.substituteEnvVars(config) cfg.configValues[key] = config } }
func structureFromConfig(v *viper.Viper) *libcentrifugo.Structure { // TODO: as viper does not have exported global config instance // we need to use nil when application wants to use global viper // config - this must be improved using our own global viper instance var pl []libcentrifugo.Project if v == nil { viper.MarshalKey("projects", &pl) } else { v.MarshalKey("projects", &pl) } // top level project configuration p, exists := getGlobalProject(v) if exists { // add global project to project list pl = append([]libcentrifugo.Project{*p}, pl...) } s := libcentrifugo.NewStructure(pl) return s }