mirror of https://github.com/statping/statping
config.yml loading
parent
a332ba5cf0
commit
ad3dac860d
|
@ -23,6 +23,7 @@ import (
|
||||||
// Connect will attempt to connect to the sqlite, postgres, or mysql database
|
// Connect will attempt to connect to the sqlite, postgres, or mysql database
|
||||||
func Connect(configs *DbConfig, retry bool) error {
|
func Connect(configs *DbConfig, retry bool) error {
|
||||||
conn := configs.ConnectionString()
|
conn := configs.ConnectionString()
|
||||||
|
p := utils.Params
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
log.WithFields(utils.ToFields(configs, conn)).Debugln("attempting to connect to database")
|
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")
|
apiKey := p.GetString("API_KEY")
|
||||||
apiSecret := utils.Params.GetString("API_SECRET")
|
apiSecret := p.GetString("API_SECRET")
|
||||||
configs.ApiKey = apiKey
|
configs.ApiKey = apiKey
|
||||||
configs.ApiSecret = apiSecret
|
configs.ApiSecret = apiSecret
|
||||||
|
|
||||||
log.WithFields(utils.ToFields(dbSession)).Debugln("connected to database")
|
log.WithFields(utils.ToFields(dbSession)).Debugln("connected to database")
|
||||||
|
|
||||||
maxOpenConn := utils.Params.GetInt("MAX_OPEN_CONN")
|
maxOpenConn := p.GetInt("MAX_OPEN_CONN")
|
||||||
maxIdleConn := utils.Params.GetInt("MAX_IDLE_CONN")
|
maxIdleConn := p.GetInt("MAX_IDLE_CONN")
|
||||||
maxLifeConn := utils.Params.GetDuration("MAX_LIFE_CONN")
|
maxLifeConn := p.GetDuration("MAX_LIFE_CONN")
|
||||||
|
|
||||||
dbSession.DB().SetMaxOpenConns(maxOpenConn)
|
dbSession.DB().SetMaxOpenConns(maxOpenConn)
|
||||||
dbSession.DB().SetMaxIdleConns(maxIdleConn)
|
dbSession.DB().SetMaxIdleConns(maxIdleConn)
|
||||||
|
|
|
@ -4,26 +4,28 @@ import (
|
||||||
"github.com/statping/statping/types/errors"
|
"github.com/statping/statping/types/errors"
|
||||||
"github.com/statping/statping/utils"
|
"github.com/statping/statping/utils"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoadConfigFile(directory string) (*DbConfig, error) {
|
func LoadConfigFile(directory string) (*DbConfig, error) {
|
||||||
p := utils.Params
|
p := utils.Params
|
||||||
log.Infof("Attempting to read config file at: %s/config.yml ", directory)
|
log.Infof("Attempting to read config file at: %s/config.yml ", directory)
|
||||||
utils.Params.SetConfigFile(directory + "/config.yml")
|
p.SetConfigFile(directory + "/config.yml")
|
||||||
utils.Params.SetConfigType("yaml")
|
p.SetConfigType("yaml")
|
||||||
|
p.ReadInConfig()
|
||||||
|
|
||||||
if utils.FileExists(directory + "/config.yml") {
|
db := new(DbConfig)
|
||||||
utils.Params.ReadInConfig()
|
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
|
if os.Getenv("DB_CONN") == "sqlite" || os.Getenv("DB_CONN") == "sqlite3" {
|
||||||
content, err := utils.OpenFile(directory + "config.yml")
|
db.DbConn = "sqlite3"
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := yaml.Unmarshal([]byte(content), &db); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if db.DbConn != "" {
|
if db.DbConn != "" {
|
||||||
p.Set("DB_CONN", db.DbConn)
|
p.Set("DB_CONN", db.DbConn)
|
||||||
}
|
}
|
||||||
|
@ -42,6 +44,15 @@ func LoadConfigFile(directory string) (*DbConfig, error) {
|
||||||
if db.DbData != "" {
|
if db.DbData != "" {
|
||||||
p.Set("DB_DATABASE", 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{
|
configs := &DbConfig{
|
||||||
DbConn: p.GetString("DB_CONN"),
|
DbConn: p.GetString("DB_CONN"),
|
||||||
|
|
|
@ -12,7 +12,7 @@ func (d *DbConfig) Save(directory string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue