jlelse
/
kis3
Archived
1
Fork 0

Make config compatible with old configuration method using env vars

This commit is contained in:
Jan-Lukas Else 2019-05-01 09:09:43 +02:00
parent 45d04b9af4
commit 88d5a33084
1 changed files with 30 additions and 0 deletions

View File

@ -3,7 +3,10 @@ package main
import ( import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt"
"io/ioutil" "io/ioutil"
"os"
"strconv"
) )
type config struct { type config struct {
@ -26,6 +29,9 @@ var (
func init() { func init() {
parseConfigFile(appConfig) 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) { func parseConfigFile(appConfig *config) {
@ -42,6 +48,30 @@ func parseConfigFile(appConfig *config) {
return 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 { func (ac *config) statsAuth() bool {
return len(ac.StatsUsername) > 0 && len(ac.StatsPassword) > 0 return len(ac.StatsUsername) > 0 && len(ac.StatsPassword) > 0
} }