config.yml loading

pull/529/head
hunterlong 2020-04-23 20:48:21 -07:00
parent a332ba5cf0
commit ad3dac860d
3 changed files with 29 additions and 17 deletions

View File

@ -23,6 +23,7 @@ import (
// Connect will attempt to connect to the sqlite, postgres, or mysql database
func Connect(configs *DbConfig, retry bool) error {
conn := configs.ConnectionString()
p := utils.Params
var err error
log.WithFields(utils.ToFields(configs, conn)).Debugln("attempting to connect to database")
@ -39,16 +40,16 @@ func Connect(configs *DbConfig, retry bool) error {
}
}
apiKey := utils.Params.GetString("API_KEY")
apiSecret := utils.Params.GetString("API_SECRET")
apiKey := p.GetString("API_KEY")
apiSecret := p.GetString("API_SECRET")
configs.ApiKey = apiKey
configs.ApiSecret = apiSecret
log.WithFields(utils.ToFields(dbSession)).Debugln("connected to database")
maxOpenConn := utils.Params.GetInt("MAX_OPEN_CONN")
maxIdleConn := utils.Params.GetInt("MAX_IDLE_CONN")
maxLifeConn := utils.Params.GetDuration("MAX_LIFE_CONN")
maxOpenConn := p.GetInt("MAX_OPEN_CONN")
maxIdleConn := p.GetInt("MAX_IDLE_CONN")
maxLifeConn := p.GetDuration("MAX_LIFE_CONN")
dbSession.DB().SetMaxOpenConns(maxOpenConn)
dbSession.DB().SetMaxIdleConns(maxIdleConn)

View File

@ -4,26 +4,28 @@ import (
"github.com/statping/statping/types/errors"
"github.com/statping/statping/utils"
"gopkg.in/yaml.v2"
"os"
)
func LoadConfigFile(directory string) (*DbConfig, error) {
p := utils.Params
log.Infof("Attempting to read config file at: %s/config.yml ", directory)
utils.Params.SetConfigFile(directory + "/config.yml")
utils.Params.SetConfigType("yaml")
p.SetConfigFile(directory + "/config.yml")
p.SetConfigType("yaml")
p.ReadInConfig()
if utils.FileExists(directory + "/config.yml") {
utils.Params.ReadInConfig()
db := new(DbConfig)
content, err := utils.OpenFile(directory + "/config.yml")
if err == nil {
if err := yaml.Unmarshal([]byte(content), &db); err != nil {
return nil, err
}
}
var db *DbConfig
content, err := utils.OpenFile(directory + "config.yml")
if err != nil {
return nil, err
}
if err := yaml.Unmarshal([]byte(content), &db); err != nil {
return nil, err
if os.Getenv("DB_CONN") == "sqlite" || os.Getenv("DB_CONN") == "sqlite3" {
db.DbConn = "sqlite3"
}
if db.DbConn != "" {
p.Set("DB_CONN", db.DbConn)
}
@ -42,6 +44,15 @@ func LoadConfigFile(directory string) (*DbConfig, error) {
if db.DbData != "" {
p.Set("DB_DATABASE", db.DbData)
}
if db.Location != "" {
p.Set("LOCATION", db.Location)
}
if db.ApiKey != "" {
p.Set("API_KEY", db.ApiKey)
}
if db.ApiSecret != "" {
p.Set("API_SECRET", db.ApiSecret)
}
configs := &DbConfig{
DbConn: p.GetString("DB_CONN"),

View File

@ -12,7 +12,7 @@ func (d *DbConfig) Save(directory string) error {
if err != nil {
return err
}
if err := utils.SaveFile(directory+"/configs.yml", c); err != nil {
if err := utils.SaveFile(directory+"/config.yml", c); err != nil {
return nil
}
return nil