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

86 lines
1.9 KiB
Go
Raw Permalink 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"`
2020-04-22 19:40:28 +00:00
BaseUrl string `json:"baseUrl"`
2019-05-01 09:46:52 +00:00
Dnt bool `json:"dnt"`
DbPath string `json:"dbPath"`
StatsUsername string `json:"statsUsername"`
StatsPassword string `json:"statsPassword"`
2019-05-27 13:03:30 +00:00
SmtpFrom string `json:"smtpfrom"`
SmtpUser string `json:"smtpUser"`
SmtpPassword string `json:"smtpPassword"`
SmtpHost string `json:"smtpHost"`
TGBotToken string `json:"tgBotToken"`
2020-04-22 19:40:28 +00:00
TGHookSecret string `json:"tgHookSecret"`
2019-05-01 09:46:52 +00:00
Reports []report `json:"reports"`
}
2019-04-02 14:28:06 +00:00
var (
2019-05-01 09:46:52 +00:00
appConfig = &config{
2020-04-22 19:40:28 +00:00
Port: "8080",
Dnt: true,
DbPath: "data/kis3.db",
2019-05-01 09:46:52 +00:00
}
2019-04-02 14:28:06 +00:00
)
2019-05-26 20:32:02 +00:00
func initConfig() {
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
}
2020-01-13 06:51:40 +00:00
e = json.Unmarshal(configJson, appConfig)
2019-05-01 09:46:52 +00:00
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
}
2020-04-22 19:40:28 +00:00
baseUrl, set := os.LookupEnv("BASE_URL")
if set {
appConfig.BaseUrl = baseUrl
}
2019-05-01 09:46:52 +00:00
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
}