refac: runtime settings are read once at startup and passed in through the composition root; no handler, router or repository reads environment variables
This commit is contained in:
1 parent
e474f03d91
commit
0b4e614ec9
15 files changed
+722
-66
No files matched your search
@@ -0,0 +1,59 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type SmtpSettings struct {
|
||||
Email string
|
||||
Password string
|
||||
Host string
|
||||
Port int
|
||||
}
|
||||
|
||||
type Settings struct {
|
||||
Environment string
|
||||
CorsAllowedOrigins []string
|
||||
PublicURL string
|
||||
UploadDir string
|
||||
JWTSecret string
|
||||
DBFile string
|
||||
Port string
|
||||
Smtp SmtpSettings
|
||||
}
|
||||
|
||||
func (s Settings) IsProduction() bool {
|
||||
return s.Environment == "production"
|
||||
}
|
||||
|
||||
func NewSettingsFromEnv() (Settings, error) {
|
||||
smtpPort, err := strconv.Atoi(os.Getenv("SMTP_PORT"))
|
||||
if err != nil {
|
||||
return Settings{}, err
|
||||
}
|
||||
|
||||
return Settings{
|
||||
Environment: os.Getenv("ENVIRONMENT"),
|
||||
CorsAllowedOrigins: parseCorsAllowedOrigins(os.Getenv("CORS_ALLOWED_ORIGINS")),
|
||||
PublicURL: os.Getenv("PUBLIC_URL"),
|
||||
UploadDir: os.Getenv("UPLOAD_DIR"),
|
||||
JWTSecret: os.Getenv("JWT_SECRET"),
|
||||
DBFile: os.Getenv("DB_FILE"),
|
||||
Port: os.Getenv("PORT"),
|
||||
Smtp: SmtpSettings{
|
||||
Email: os.Getenv("SMTP_EMAIL"),
|
||||
Password: os.Getenv("SMTP_PASSWORD"),
|
||||
Host: os.Getenv("SMTP_HOST"),
|
||||
Port: smtpPort,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func parseCorsAllowedOrigins(raw string) []string {
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
return strings.Split(raw, ";")
|
||||
}
|
||||
Reference in new issue
Block a user