diff --git a/core/configs.go b/core/configs.go index 8f4c5a77..bcee6837 100644 --- a/core/configs.go +++ b/core/configs.go @@ -2,14 +2,19 @@ package core import ( "errors" + "fmt" "github.com/go-yaml/yaml" "github.com/hunterlong/statup/types" + "github.com/hunterlong/statup/utils" "io/ioutil" "os" + "time" ) func LoadConfig() (*types.Config, error) { if os.Getenv("DB_CONN") != "" { + utils.Log(1, "DB_CONN environment variable was found") + //time.Sleep(20 * time.Second) return LoadUsingEnv() } Configs = new(types.Config) @@ -51,12 +56,65 @@ func LoadUsingEnv() (*types.Config, error) { if os.Getenv("USE_CDN") == "true" { CoreApp.UseCdn = true } + + dbConfig := &DbConfig{ + DbConn: os.Getenv("DB_CONN"), + DbHost: os.Getenv("DB_HOST"), + DbUser: os.Getenv("DB_USER"), + DbPass: os.Getenv("DB_PASS"), + DbData: os.Getenv("DB_DATABASE"), + DbPort: 5432, + Project: "Statup - " + os.Getenv("NAME"), + Description: "New Statup Installation", + Domain: os.Getenv("DOMAIN"), + Username: "admin", + Password: "admin", + Email: "info@localhost.com", + } + + err := DbConnection(dbConfig.DbConn) + if err != nil { + utils.Log(4, err) + return nil, err + } + + exists, err := DbSession.Collection("core").Find().Exists() + if !exists { + + utils.Log(1, fmt.Sprintf("Core database does not exist, creating now!")) + DropDatabase() + CreateDatabase() + + CoreApp = &Core{ + Name: dbConfig.Project, + Description: dbConfig.Description, + Config: "config.yml", + ApiKey: utils.NewSHA1Hash(9), + ApiSecret: utils.NewSHA1Hash(16), + Domain: dbConfig.Domain, + MigrationId: time.Now().Unix(), + } + + CoreApp.DbConnection = dbConfig.DbConn + + err := CoreApp.Insert() + if err != nil { + utils.Log(3, err) + } + + admin := &User{ + Username: "admin", + Password: "admin", + Email: "info@admin.com", + Admin: true, + } + admin.Create() + + LoadSampleData() + + return Configs, err + + } + return Configs, nil } - -func ifOr(val, def string) string { - if val == "" { - return def - } - return val -} diff --git a/core/core.go b/core/core.go index 72c112bd..84bec322 100644 --- a/core/core.go +++ b/core/core.go @@ -58,6 +58,12 @@ func NewCore() *Core { return CoreApp } +func (c *Core) Insert() error { + col := DbSession.Collection("core") + _, err := col.Insert(c) + return err +} + func InitApp() { SelectCore() notifiers.Collections = DbSession.Collection("communication") @@ -121,6 +127,11 @@ func SelectLastMigration() (int64, error) { func SelectCore() (*Core, error) { var c *Core + exists := DbSession.Collection("core").Exists() + if !exists { + return nil, errors.New("core database has not been setup yet.") + } + err := DbSession.Collection("core").Find().One(&c) if err != nil { return nil, err diff --git a/core/database.go b/core/database.go index 40e0df5a..1aecfc01 100644 --- a/core/database.go +++ b/core/database.go @@ -135,6 +135,9 @@ func (c *DbConfig) Save() error { } CoreApp, err = SelectCore() + if err != nil { + utils.Log(4, err) + } CoreApp.DbConnection = c.DbConn return err @@ -207,7 +210,7 @@ func RunDatabaseUpgrades() error { } func DropDatabase() { - fmt.Println("Dropping Tables...") + utils.Log(1, "Dropping Database Tables...") down, _ := SqlBox.String("down.sql") requests := strings.Split(down, ";") for _, request := range requests { @@ -219,7 +222,7 @@ func DropDatabase() { } func CreateDatabase() { - fmt.Println("Creating Tables...") + utils.Log(1, "Creating Database Tables...") sql := "postgres_up.sql" if CoreApp.DbConnection == "mysql" { sql = "mysql_up.sql" @@ -236,7 +239,7 @@ func CreateDatabase() { } //secret := NewSHA1Hash() //db.QueryRow("INSERT INTO core (secret, version) VALUES ($1, $2);", secret, VERSION).Scan() - fmt.Println("Database Created") + utils.Log(1, "Database Created") //SampleData() } diff --git a/core/services.go b/core/services.go index 815ed836..d763ea99 100644 --- a/core/services.go +++ b/core/services.go @@ -58,6 +58,7 @@ func SelectAllServices() ([]*Service, error) { err := col.All(&srvcs) if err != nil { utils.Log(3, err) + return nil, err } for _, s := range srvcs { s.Checkins = s.SelectAllCheckins() diff --git a/core/setup.go b/core/setup.go index e3840151..841ca1f9 100644 --- a/core/setup.go +++ b/core/setup.go @@ -73,15 +73,15 @@ func LoadSampleData() error { utils.Log(3, fmt.Sprintf("Error creating Service %v: %v", id, err)) } - checkin := &Checkin{ - Service: s2.Id, - Interval: 30, - Api: utils.NewSHA1Hash(18), - } - id, err = checkin.Create() - if err != nil { - utils.Log(3, fmt.Sprintf("Error creating Checkin %v: %v", id, err)) - } + //checkin := &Checkin{ + // Service: s2.Id, + // Interval: 30, + // Api: utils.NewSHA1Hash(18), + //} + //id, err = checkin.Create() + //if err != nil { + // utils.Log(3, fmt.Sprintf("Error creating Checkin %v: %v", id, err)) + //} //for i := 0; i < 3; i++ { // s1.Check() diff --git a/handlers/settings.go b/handlers/settings.go index 0b49a663..dee7a811 100644 --- a/handlers/settings.go +++ b/handlers/settings.go @@ -1,7 +1,6 @@ package handlers import ( - "fmt" "github.com/hunterlong/statup/core" "github.com/hunterlong/statup/notifiers" "github.com/hunterlong/statup/utils" @@ -140,6 +139,11 @@ func SaveNotificationHandler(w http.ResponseWriter, r *http.Request) { if err != nil { utils.Log(3, err) } - msg := fmt.Sprintf("%v - %v - %v", notifierId, notifer, enabled) - w.Write([]byte(msg)) + + if notifer.Enabled { + notify := notifiers.SelectNotifier(notifer.Id) + go notify.Run() + } + + http.Redirect(w, r, "/settings", http.StatusSeeOther) } diff --git a/main.go b/main.go index eaa305f3..cdc76b76 100644 --- a/main.go +++ b/main.go @@ -65,7 +65,7 @@ func mainProcess() { var err error err = core.DbConnection(core.Configs.Connection) if err != nil { - utils.Log(3, err) + utils.Log(4, fmt.Sprintf("could not connect to database: %v", err)) } core.RunDatabaseUpgrades() diff --git a/notifiers/email.go b/notifiers/email.go index 29533515..7a39808e 100644 --- a/notifiers/email.go +++ b/notifiers/email.go @@ -111,19 +111,19 @@ func (u *Email) Run() error { // ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS func (u *Email) OnFailure() error { - utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method)) - - // Do failing stuff here! - + if u.Enabled { + utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method)) + // Do failing stuff here! + } return nil } // ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS func (u *Email) OnSuccess() error { - utils.Log(1, fmt.Sprintf("Notification %v is receiving a successful notification.", u.Method)) - - // Do checking or any successful things here - + if u.Enabled { + utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method)) + // Do failing stuff here! + } return nil } diff --git a/notifiers/notifiers.go b/notifiers/notifiers.go index 18550efd..160e218e 100644 --- a/notifiers/notifiers.go +++ b/notifiers/notifiers.go @@ -70,6 +70,12 @@ func (n *Notification) isInDatabase() (bool, error) { return Collections.Find("id", n.Id).Exists() } +func SelectNotification(id int64) (*Notification, error) { + var notifier *Notification + err := Collections.Find("id", id).One(¬ifier) + return notifier, err +} + func (n *Notification) Update() (*Notification, error) { n.CreatedAt = time.Now() err := Collections.Find("id", n.Id).Update(n) @@ -97,6 +103,15 @@ func Select(id int64) *Notification { return notifier } +func SelectNotifier(id int64) Notifier { + var notifier Notifier + for _, n := range AllCommunications { + notif := n.(Notifier) + return notif + } + return notifier +} + func (f NotificationForm) Value() string { notifier := Select(f.id) return notifier.GetValue(f.DbField) diff --git a/notifiers/slack.go b/notifiers/slack.go index a8d94132..52093f6e 100644 --- a/notifiers/slack.go +++ b/notifiers/slack.go @@ -46,11 +46,19 @@ func (u *Slack) Select() *Notification { // WHEN NOTIFIER LOADS func (u *Slack) Init() error { - err := SendSlack("its online") - u.Install() + err := u.Install() + + if err == nil { + notifier, _ := SelectNotification(u.Id) + forms := u.Form + u.Notification = notifier + u.Form = forms + if u.Enabled { + go u.Run() + } + } - //go u.Run() return err } @@ -84,19 +92,19 @@ func SendSlack(msg string) error { // ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS func (u *Slack) OnFailure() error { - utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method)) - - // Do failing stuff here! - + if u.Enabled { + utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method)) + // Do failing stuff here! + } return nil } // ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS func (u *Slack) OnSuccess() error { - utils.Log(1, fmt.Sprintf("Notification %v is receiving a successful notification.", u.Method)) - - // Do checking or any successful things here - + if u.Enabled { + utils.Log(1, fmt.Sprintf("Notification %v is receiving a successful notification.", u.Method)) + // Do checking or any successful things here + } return nil }