statping/types/configs/load.go

104 lines
2.7 KiB
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package configs
import (
"errors"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/utils"
2020-04-24 02:26:09 +00:00
"gopkg.in/yaml.v2"
2020-04-24 03:48:21 +00:00
"os"
2020-03-04 10:29:00 +00:00
)
func LoadConfigs(cfgFile string) (*DbConfig, error) {
writeAble, err := utils.DirWritable(utils.Directory)
if err != nil {
return nil, err
}
if !writeAble {
return nil, errors.New("Directory %s is not writable: " + utils.Directory)
}
2020-04-16 09:57:00 +00:00
p := utils.Params
log.Infof("Attempting to read config file at: %s", cfgFile)
p.SetConfigFile(cfgFile)
2020-04-24 03:48:21 +00:00
p.SetConfigType("yaml")
p.ReadInConfig()
2020-04-22 05:24:50 +00:00
2020-04-24 03:48:21 +00:00
db := new(DbConfig)
content, err := utils.OpenFile(cfgFile)
2020-04-24 03:48:21 +00:00
if err == nil {
if err := yaml.Unmarshal([]byte(content), &db); err != nil {
return nil, err
}
2020-04-22 05:24:50 +00:00
}
2020-04-16 17:32:54 +00:00
2020-04-24 03:48:21 +00:00
if os.Getenv("DB_CONN") == "sqlite" || os.Getenv("DB_CONN") == "sqlite3" {
db.DbConn = "sqlite3"
2020-04-24 02:26:09 +00:00
}
if db.DbConn != "" {
p.Set("DB_CONN", db.DbConn)
}
if db.DbHost != "" {
p.Set("DB_HOST", db.DbHost)
}
if db.DbPort != 0 {
p.Set("DB_PORT", db.DbPort)
}
if db.DbPass != "" {
2020-04-24 15:11:18 +00:00
p.Set("DB_PASS", db.DbPass)
2020-04-24 02:26:09 +00:00
}
if db.DbUser != "" {
p.Set("DB_USER", db.DbUser)
}
if db.DbData != "" {
p.Set("DB_DATABASE", db.DbData)
}
2020-04-24 03:48:21 +00:00
if db.Location != "" {
p.Set("LOCATION", db.Location)
}
2020-08-03 10:35:48 +00:00
if db.ApiSecret != "" && p.GetString("API_SECRET") == "" {
2020-04-24 03:48:21 +00:00
p.Set("API_SECRET", db.ApiSecret)
}
if db.Language != "" {
2020-07-15 17:09:00 +00:00
p.Set("LANGUAGE", db.Language)
}
if db.SendReports {
p.Set("ALLOW_REPORTS", true)
}
2020-07-15 17:09:00 +00:00
if db.LetsEncryptEmail != "" {
p.Set("LETSENCRYPT_EMAIL", db.LetsEncryptEmail)
}
if db.LetsEncryptHost != "" {
p.Set("LETSENCRYPT_HOST", db.LetsEncryptHost)
}
if db.LetsEncryptEnable {
p.Set("LETSENCRYPT_ENABLE", db.LetsEncryptEnable)
}
2020-04-24 02:26:09 +00:00
2020-04-16 17:32:54 +00:00
configs := &DbConfig{
2020-07-15 17:09:00 +00:00
DbConn: p.GetString("DB_CONN"),
DbHost: p.GetString("DB_HOST"),
DbUser: p.GetString("DB_USER"),
DbPass: p.GetString("DB_PASS"),
DbData: p.GetString("DB_DATABASE"),
DbPort: p.GetInt("DB_PORT"),
Project: p.GetString("NAME"),
Description: p.GetString("DESCRIPTION"),
Domain: p.GetString("DOMAIN"),
Email: p.GetString("EMAIL"),
Username: p.GetString("ADMIN_USER"),
Password: p.GetString("ADMIN_PASSWORD"),
Location: utils.Directory,
SqlFile: p.GetString("SQL_FILE"),
Language: p.GetString("LANGUAGE"),
SendReports: p.GetBool("ALLOW_REPORTS"),
LetsEncryptEnable: p.GetBool("LETSENCRYPT_ENABLE"),
2020-08-03 10:35:48 +00:00
LetsEncryptHost: p.GetString("LETSENCRYPT_HOST"),
LetsEncryptEmail: p.GetString("LETSENCRYPT_EMAIL"),
ApiSecret: p.GetString("API_SECRET"),
2020-03-04 10:29:00 +00:00
}
log.WithFields(utils.ToFields(configs)).Debugln("read config file: " + cfgFile)
2020-04-16 17:32:54 +00:00
2020-04-20 15:17:22 +00:00
if configs.DbConn == "" {
return configs, errors.New("Starting in setup mode")
}
2020-04-16 17:32:54 +00:00
return configs, nil
2020-03-04 10:29:00 +00:00
}