jlelse
/
kis3
Archived
1
Fork 0
This repository has been archived on 2021-01-04. You can view files and clone it, but cannot push or open issues or pull requests.
kis3/config.go

77 lines
1.6 KiB
Go
Raw Normal View History

2019-04-02 14:28:06 +00:00
package main
import (
2019-05-01 09:46:52 +00:00
"encoding/json"
"flag"
"io/ioutil"
2019-04-02 14:28:06 +00:00
"os"
"strconv"
)
type config struct {
2019-05-01 09:46:52 +00:00
Port string `json:"port"`
Dnt bool `json:"dnt"`
DbPath string `json:"dbPath"`
StatsUsername string `json:"statsUsername"`
StatsPassword string `json:"statsPassword"`
Reports []report `json:"reports"`
}
2019-04-02 14:28:06 +00:00
var (
2019-05-01 09:46:52 +00:00
appConfig = &config{
Port: "8080",
Dnt: true,
DbPath: "data/kis3.db",
StatsUsername: "",
StatsPassword: "",
}
2019-04-02 14:28:06 +00:00
)
func init() {
2019-05-01 09:46:52 +00:00
parseConfigFile(appConfig)
// Replace values that are set via environment vars (to make it compatible with old method)
overwriteEnvVarValues(appConfig)
2019-04-02 14:28:06 +00:00
}
2019-05-01 09:46:52 +00:00
func parseConfigFile(appConfig *config) {
configFile := flag.String("c", "config.json", "Config file")
flag.Parse()
configJson, e := ioutil.ReadFile(*configFile)
2019-04-02 14:28:06 +00:00
if e != nil {
2019-05-01 09:46:52 +00:00
return
2019-04-02 14:28:06 +00:00
}
2019-05-01 09:46:52 +00:00
e = json.Unmarshal([]byte(configJson), appConfig)
if e != nil {
return
2019-04-02 14:28:06 +00:00
}
return
}
2019-05-01 09:46:52 +00:00
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
}
2019-04-02 14:28:06 +00:00
}
2019-04-03 14:51:44 +00:00
2019-05-01 09:46:52 +00:00
func (ac *config) statsAuth() bool {
return len(ac.StatsUsername) > 0 && len(ac.StatsPassword) > 0
2019-04-03 14:51:44 +00:00
}