mirror of
https://github.com/mxpv/podsync.git
synced 2024-05-11 05:55:04 +00:00
Implement config package
This commit is contained in:
56
web/pkg/config/config.go
Normal file
56
web/pkg/config/config.go
Normal file
@@ -0,0 +1,56 @@
|
||||
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"`
|
||||
}
|
||||
|
||||
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",
|
||||
}
|
||||
|
||||
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
|
||||
}
|
54
web/pkg/config/config_test.go
Normal file
54
web/pkg/config/config_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const YamlConfig = `
|
||||
youtubeApiKey: "1"
|
||||
vimeoApiKey: "2"
|
||||
patreonClientId: "3"
|
||||
patreonSecret: "4"
|
||||
postgresConnectionUrl: "5"
|
||||
`
|
||||
|
||||
func TestReadYaml(t *testing.T) {
|
||||
defer viper.Reset()
|
||||
|
||||
err := ioutil.WriteFile("./podsync.yaml", []byte(YamlConfig), 0644)
|
||||
defer os.Remove("./podsync.yaml")
|
||||
require.NoError(t, err)
|
||||
|
||||
cfg, err := ReadConfiguration()
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "1", cfg.YouTubeApiKey)
|
||||
require.Equal(t, "2", cfg.VimeoApiKey)
|
||||
require.Equal(t, "3", cfg.PatreonClientId)
|
||||
require.Equal(t, "4", cfg.PatreonSecret)
|
||||
require.Equal(t, "5", cfg.PostgresConnectionURL)
|
||||
}
|
||||
|
||||
func TestReadEnv(t *testing.T) {
|
||||
defer viper.Reset()
|
||||
defer os.Clearenv()
|
||||
|
||||
os.Setenv("YOUTUBE_API_KEY", "11")
|
||||
os.Setenv("VIMEO_API_KEY", "22")
|
||||
os.Setenv("PATREON_CLIENT_ID", "33")
|
||||
os.Setenv("PATREON_SECRET", "44")
|
||||
os.Setenv("POSTGRES_CONNECTION_URL", "55")
|
||||
|
||||
cfg, err := ReadConfiguration()
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "11", cfg.YouTubeApiKey)
|
||||
require.Equal(t, "22", cfg.VimeoApiKey)
|
||||
require.Equal(t, "33", cfg.PatreonClientId)
|
||||
require.Equal(t, "44", cfg.PatreonSecret)
|
||||
}
|
Reference in New Issue
Block a user