// Setup sets up defaults for viper configuration options and
// overrides these values with the values from the given configuration file
// if it is not empty. Those values again are overwritten by environment
// variables.
func Setup(configFilePath string) error {
	viper.Reset()

	// Expect environment variables to be prefix with "ALMIGHTY_".
	viper.SetEnvPrefix("ALMIGHTY")

	// Automatically map environment variables to viper values
	viper.AutomaticEnv()

	// To override nested variables through environment variables, we
	// need to make sure that we don't have to use dots (".") inside the
	// environment variable names.
	// To override foo.bar you need to set ALM_FOO_BAR
	viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

	viper.SetTypeByDefaultValue(true)
	setConfigDefaults()

	// Read the config
	// Explicitly specify which file to load config from
	if configFilePath != "" {
		viper.SetConfigFile(configFilePath)
		viper.SetConfigType("yaml")
		err := viper.ReadInConfig() // Find and read the config file
		if err != nil {             // Handle errors reading the config file
			return fmt.Errorf("Fatal error config file: %s \n", err)
		}
	}

	return nil
}
func setConfigDefaults() {
	//---------
	// Postgres
	//---------
	viper.SetTypeByDefaultValue(true)
	viper.SetDefault(varPostgresHost, "localhost")
	viper.SetDefault(varPostgresPort, 5432)
	viper.SetDefault(varPostgresUser, "postgres")
	viper.SetDefault(varPostgresDatabase, "postgres")
	viper.SetDefault(varPostgresPassword, "mysecretpassword")
	viper.SetDefault(varPostgresSSLMode, "disable")
	viper.SetDefault(varPostgresConnectionTimeout, 5)

	// Number of seconds to wait before trying to connect again
	viper.SetDefault(varPostgresConnectionRetrySleep, time.Duration(time.Second))

	//-----
	// HTTP
	//-----
	viper.SetDefault(varHTTPAddress, "0.0.0.0:8080")

	//-----
	// Misc
	//-----

	// Enable development related features, e.g. token generation endpoint
	viper.SetDefault(varDeveloperModeEnabled, false)

	viper.SetDefault(varPopulateCommonTypes, true)

	// Auth-related defaults
	viper.SetDefault(varTokenPublicKey, defaultTokenPublicKey)
	viper.SetDefault(varTokenPrivateKey, defaultTokenPrivateKey)
	viper.SetDefault(varKeycloakClientID, defaultKeycloakClientID)
	viper.SetDefault(varKeycloakSecret, defaultKeycloakSecret)
	viper.SetDefault(varGithubAuthToken, defaultActualToken)
	viper.SetDefault(varKeycloakEndpointAuth, defaultKeycloakEndpointAuth)
	viper.SetDefault(varKeycloakEndpointToken, defaultKeycloakEndpointToken)
	viper.SetDefault(varKeycloakEndpointUserinfo, defaultKeycloakEndpointUserinfo)
	viper.SetDefault(varKeycloakTesUserName, defaultKeycloakTesUserName)
	viper.SetDefault(varKeycloakTesUserSecret, defaultKeycloakTesUserSecret)
}