Skip to content

Commit f5d756b

Browse files
authored
Add email config via env vars (#134)
1 parent f7b5eba commit f5d756b

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Make sure to replace `/path/to/host` with your preferred root directory for conf
7474

7575
In the [config](./config/) directory are a couple of starter configuration files for prod and a dev environments. The server expects a config.yaml in the config directory and will load settings from it when started.
7676

77-
**Note:** You can set `email.password` and `jwt.secret` using environment variables `TW_EMAIL_PASSWORD` and `TW_JWT_SECRET` for improved security and flexibility.
77+
**Note:** You can set `email.host`, `email.port`, `email.email`, `email.password` and `jwt.secret` using environment variables `TW_EMAIL_HOST`, `TW_EMAIL_PORT`, `TW_EMAIL_SENDER`, `TW_EMAIL_PASSWORD` and `TW_JWT_SECRET` for improved security and flexibility.
7878

7979
The configuration files are yaml mappings with the following values:
8080

config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ func LoadConfig() *Config {
6060

6161
// Allow values with secrets to be set via environment variables
6262
_ = viper.BindEnv("jwt.secret", "TW_JWT_SECRET")
63+
_ = viper.BindEnv("email.host", "TW_EMAIL_HOST")
64+
_ = viper.BindEnv("email.port", "TW_EMAIL_PORT")
65+
_ = viper.BindEnv("email.email", "TW_EMAIL_SENDER")
6366
_ = viper.BindEnv("email.password", "TW_EMAIL_PASSWORD")
6467

6568
err := viper.ReadInConfig()

config/config_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,41 @@ func TestLoadConfig_PanicOnMissingFile(t *testing.T) {
7979
_ = LoadConfig()
8080
})
8181
}
82+
83+
func TestLoadConfig_EmailEnvOverride(t *testing.T) {
84+
_ = os.MkdirAll("./config", 0755)
85+
f, err := os.Create("./config/config.yaml")
86+
assert.NoError(t, err)
87+
defer os.Remove("./config/config.yaml")
88+
defer f.Close()
89+
90+
_, err = f.WriteString(`email:
91+
host: smtp.example.com
92+
port: 25
93+
email: test@example.com
94+
password: testpass
95+
server:
96+
port: 1234
97+
jwt:
98+
secret: secret
99+
`)
100+
assert.NoError(t, err)
101+
102+
os.Setenv("TW_EMAIL_HOST", "smtp.override.com")
103+
os.Setenv("TW_EMAIL_PORT", "2525")
104+
os.Setenv("TW_EMAIL_SENDER", "override@example.com")
105+
os.Setenv("TW_EMAIL_PASSWORD", "overridepass")
106+
107+
viper.Reset()
108+
cfg := LoadConfig()
109+
110+
assert.Equal(t, "smtp.override.com", cfg.EmailConfig.Host)
111+
assert.Equal(t, 2525, cfg.EmailConfig.Port)
112+
assert.Equal(t, "override@example.com", cfg.EmailConfig.Email)
113+
assert.Equal(t, "overridepass", cfg.EmailConfig.Password)
114+
115+
os.Unsetenv("TW_EMAIL_HOST")
116+
os.Unsetenv("TW_EMAIL_PORT")
117+
os.Unsetenv("TW_EMAIL_SENDER")
118+
os.Unsetenv("TW_EMAIL_PASSWORD")
119+
}

0 commit comments

Comments
 (0)