package config import ( "strings" "github.com/spf13/viper" ) const FileName = "podsync" type AppConfig struct { YouTubeApiKey string `yaml:"youtubeApiKey"` VimeoApiKey string `yaml:"vimeoApiKey"` PatreonClientId string `yaml:"patreonClientId"` PatreonSecret string `yaml:"patreonSecret"` PostgresConnectionURL string `yaml:"postgresConnectionUrl"` RedisURL string `yaml:"redisUrl"` } func ReadConfiguration() (cfg *AppConfig, err error) { viper.SetConfigName(FileName) // Configuration file viper.AddConfigPath(".") viper.AddConfigPath("/app/config/") // Env variables viper.AutomaticEnv() viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) envmap := map[string]string{ "youtubeApiKey": "YOUTUBE_API_KEY", "vimeoApiKey": "VIMEO_API_KEY", "patreonClientId": "PATREON_CLIENT_ID", "patreonSecret": "PATREON_SECRET", "postgresConnectionUrl": "POSTGRES_CONNECTION_URL", "redisUrl": "REDIS_CONNECTION_URL", } for k, v := range envmap { viper.BindEnv(k, v) } err = viper.ReadInConfig() if err != nil { if _, ok := err.(viper.ConfigFileNotFoundError); !ok { return } // Ignore file not found error err = nil } cfg = &AppConfig{} viper.Unmarshal(cfg) return }