From 88d5a33084400f7a7d207eafdc85ae9aec5bd067 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Wed, 1 May 2019 09:09:43 +0200 Subject: [PATCH] Make config compatible with old configuration method using env vars --- config.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/config.go b/config.go index a7ab44c..8a3425f 100644 --- a/config.go +++ b/config.go @@ -3,7 +3,10 @@ package main import ( "encoding/json" "flag" + "fmt" "io/ioutil" + "os" + "strconv" ) type config struct { @@ -26,6 +29,9 @@ var ( func init() { parseConfigFile(appConfig) + // Replace values that are set via environment vars (to make it compatible with old method) + overwriteEnvVarValues(appConfig) + fmt.Println(appConfig) } func parseConfigFile(appConfig *config) { @@ -42,6 +48,30 @@ func parseConfigFile(appConfig *config) { return } +func overwriteEnvVarValues(appConfig *config) { + port, set := os.LookupEnv("PORT") + if set { + appConfig.Port = port + } + dntString, set := os.LookupEnv("DNT") + dntBool, e := strconv.ParseBool(dntString) + if set && e == nil { + appConfig.Dnt = dntBool + } + dbPath, set := os.LookupEnv("DB_PATH") + if set { + appConfig.DbPath = dbPath + } + username, set := os.LookupEnv("STATS_USERNAME") + if set { + appConfig.StatsUsername = username + } + password, set := os.LookupEnv("STATS_PASSWORD") + if set { + appConfig.StatsPassword = password + } +} + func (ac *config) statsAuth() bool { return len(ac.StatsUsername) > 0 && len(ac.StatsPassword) > 0 }