pull/429/head
hunterlong 2020-02-26 08:56:38 -08:00
parent dd895e1876
commit 307e270fc9
6 changed files with 39 additions and 20 deletions

View File

@ -140,10 +140,13 @@ func EnvToConfig() (*DbConfig, error) {
domain := utils.Getenv("DOMAIN", "").(string)
sqlFile := utils.Getenv("SQL_FILE", "").(string)
if dbConn != "sqlite" {
if dbConn != "" {
if dbHost == "" {
return nil, errors.New("Missing DB_HOST environment variable")
}
if dbPort == 0 {
return nil, errors.New("Missing DB_PORT environment variable")
}
if dbUser == "" {
return nil, errors.New("Missing DB_USER environment variable")
}
@ -155,7 +158,7 @@ func EnvToConfig() (*DbConfig, error) {
}
}
CoreApp.Config = &types.DbConfig{
config := &types.DbConfig{
DbConn: dbConn,
DbHost: dbHost,
DbUser: dbUser,
@ -173,7 +176,9 @@ func EnvToConfig() (*DbConfig, error) {
SqlFile: sqlFile,
}
return &DbConfig{CoreApp.Config}, err
CoreApp.Config = config
return &DbConfig{config}, err
}
// SampleData runs all the sample data for a new Statping installation
@ -186,6 +191,10 @@ func SampleData() error {
log.Errorln(err)
return err
}
if err := insertSampleCheckins(); err != nil {
log.Errorln(err)
return err
}
return nil
}

View File

@ -16,6 +16,7 @@
package core
import (
"errors"
"fmt"
"github.com/go-yaml/yaml"
"github.com/hunterlong/statping/core/notifier"
@ -179,33 +180,39 @@ func (c *Core) Connect(retry bool, location string) error {
if DbSession != nil {
return nil
}
var conn, dbType string
var conn string
var err error
switch dbType {
if c.Config == nil {
return errors.New("missing database connection configs")
}
configs := c.Config
switch configs.DbConn {
case "sqlite":
sqlFilename := findDbFile()
conn = sqlFilename
log.Infof("SQL database file at: %v/%v", utils.Directory, conn)
dbType = "sqlite3"
configs.DbConn = "sqlite3"
case "mysql":
host := fmt.Sprintf("%v:%v", CoreApp.Config.DbHost, CoreApp.Config.DbPort)
conn = fmt.Sprintf("%v:%v@tcp(%v)/%v?charset=utf8&parseTime=True&loc=UTC&time_zone=%%27UTC%%27", CoreApp.Config.DbUser, CoreApp.Config.DbPass, host, CoreApp.Config.DbData)
host := fmt.Sprintf("%v:%v", configs.DbHost, configs.DbPort)
conn = fmt.Sprintf("%v:%v@tcp(%v)/%v?charset=utf8&parseTime=True&loc=UTC&time_zone=%%27UTC%%27", configs.DbUser, configs.DbPass, host, configs.DbData)
case "postgres":
sslMode := "disable"
if postgresSSL != "" {
sslMode = postgresSSL
}
conn = fmt.Sprintf("host=%v port=%v user=%v dbname=%v password=%v timezone=UTC sslmode=%v", CoreApp.Config.DbHost, CoreApp.Config.DbPort, CoreApp.Config.DbUser, CoreApp.Config.DbData, CoreApp.Config.DbPass, sslMode)
conn = fmt.Sprintf("host=%v port=%v user=%v dbname=%v password=%v timezone=UTC sslmode=%v", configs.DbHost, configs.DbPort, configs.DbUser, configs.DbData, configs.DbPass, sslMode)
case "mssql":
host := fmt.Sprintf("%v:%v", CoreApp.Config.DbHost, CoreApp.Config.DbPort)
conn = fmt.Sprintf("sqlserver://%v:%v@%v?database=%v", CoreApp.Config.DbUser, CoreApp.Config.DbPass, host, CoreApp.Config.DbData)
host := fmt.Sprintf("%v:%v", configs.DbHost, configs.DbPort)
conn = fmt.Sprintf("sqlserver://%v:%v@%v?database=%v", configs.DbUser, configs.DbPass, host, configs.DbData)
}
log.WithFields(utils.ToFields(c, conn)).Debugln("attempting to connect to database")
dbSession, err := database.Openw(dbType, conn)
dbSession, err := database.Openw(configs.DbConn, conn)
if err != nil {
log.Debugln(fmt.Sprintf("Database connection error %v", err))
if retry {
log.Errorln(fmt.Sprintf("Database connection to '%v' is not available, trying again in 5 seconds...", CoreApp.Config.DbHost))
log.Errorln(fmt.Sprintf("Database connection to '%v' is not available, trying again in 5 seconds...", configs.DbHost))
return c.waitForDb()
} else {
return err
@ -226,7 +233,7 @@ func (c *Core) Connect(retry bool, location string) error {
if utils.VerboseMode >= 4 {
DbSession.LogMode(true).Debug().SetLogger(gorm.Logger{log})
}
log.Infoln(fmt.Sprintf("Database %v connection was successful.", dbType))
log.Infoln(fmt.Sprintf("Database %v connection was successful.", configs.DbConn))
}
return err
}

View File

@ -26,9 +26,9 @@ func SelectGroups(includeAll bool, auth bool) []*Group {
}
}
sort.Sort(GroupOrder(validGroups))
if includeAll {
validGroups = append(validGroups, &Group{})
}
//if includeAll {
// validGroups = append(validGroups, &Group{})
//}
return validGroups
}

View File

@ -216,9 +216,11 @@ func insertSampleGroups() error {
func insertSampleCheckins() error {
s1 := SelectService(1)
checkin1 := &types.Checkin{
Name: "Example Checkin 1",
ServiceId: s1.Id,
Interval: 300,
GracePeriod: 300,
ApiKey: utils.RandomString(7),
}
if _, err := database.Create(checkin1); err != nil {
@ -227,9 +229,11 @@ func insertSampleCheckins() error {
s2 := SelectService(1)
checkin2 := &types.Checkin{
Name: "Example Checkin 2",
ServiceId: s2.Id,
Interval: 900,
GracePeriod: 300,
ApiKey: utils.RandomString(7),
}
if _, err := database.Create(checkin2); err != nil {

View File

@ -30,7 +30,7 @@ import (
func apiAllGroupHandler(r *http.Request) interface{} {
auth, admin := IsUser(r), IsAdmin(r)
groups := core.SelectGroups(admin, auth)
return groups
return flattenGroups(groups)
}
func flattenGroups(groups []*core.Group) []*types.Group {

View File

@ -238,9 +238,8 @@ func apiServiceFailuresHandler(r *http.Request) interface{} {
return errors.New("service not found")
}
var fails []types.Failure
var fails []*types.Failure
database.ParseQueries(r, service.Failures()).Find(&fails)
return fails
}