Beispiel #1
0
func newConfig(product string, ver string) *Config {
	env := pickEnv("development", "GO_ENV", "RACK_ENV", "RAILS_ENV", "NODE_ENV")
	viper.SetConfigName(env)       // name of config file (without extension)
	viper.AddConfigPath("config/") // path to look for the config file in
	viper.SetConfigType("yaml")
	viper.SetEnvPrefix(product)
	viper.AutomaticEnv()
	viper.BindEnv("port", "PORT")
	viper.SetDefault("port", "6060")
	viper.SetDefault("interface", "")
	viper.SetDefault("shafile", "REVISION")
	viper.SetDefault("middleware", []string{"request_identification", "request_tracing", "route_metrics"})

	host, _ := os.Hostname()
	viper.SetDefault("host", host)

	err := viper.ReadInConfig() // Find and read the config file
	if err != nil {
		log.Fatalf("Cannot read configuration %v", err)
	}

	host = viper.GetString("host")
	sha := readSha()

	c := &Config{
		Environment: env,
		Hostname:    host,
		Product:     product,
		Version:     ver,
		Sha:         sha,
	}
	return c
}
Beispiel #2
0
// viper has a bug which doesn't allow actual config to override nested default.
func (c *Config) GetStringNestedWithDefault(key string, defval string) string {
	v := viper.GetString(key)
	if v != "" {
		return v
	}
	return defval
}
Beispiel #3
0
func (c *Config) GetString(key string) string {
	return viper.GetString(key)
}
Beispiel #4
0
func readSha() string {
	shafile := viper.GetString("shafile")
	bytes, err := ioutil.ReadFile(shafile)
	return conditionally(err == nil, fmt.Sprintf("%s", bytes), "no-rev").(string)
}