statping/handlers/setup.go

123 lines
2.5 KiB
Go
Raw Normal View History

2018-06-30 00:57:05 +00:00
package handlers
import (
2020-01-28 12:15:48 +00:00
"errors"
"github.com/statping/statping/notifiers"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/configs"
"github.com/statping/statping/types/core"
"github.com/statping/statping/types/null"
2020-03-14 03:13:20 +00:00
"github.com/statping/statping/types/services"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/utils"
2018-06-30 00:57:05 +00:00
"net/http"
2018-07-02 06:21:41 +00:00
"time"
2018-06-30 00:57:05 +00:00
)
func processSetupHandler(w http.ResponseWriter, r *http.Request) {
var err error
2020-03-04 10:29:00 +00:00
if core.App.Setup {
2020-01-28 12:15:48 +00:00
sendErrorJson(errors.New("Statping has already been setup"), w, r)
return
}
2020-03-06 22:18:06 +00:00
confgs, err := configs.LoadConfigForm(r)
if err != nil {
2020-01-28 12:15:48 +00:00
log.Errorln(err)
sendErrorJson(err, w, r)
2018-06-30 00:57:05 +00:00
return
}
2018-08-19 08:48:02 +00:00
project := r.PostForm.Get("project")
description := r.PostForm.Get("description")
domain := r.PostForm.Get("domain")
2019-12-30 08:08:51 +00:00
2020-03-06 22:18:06 +00:00
log.WithFields(utils.ToFields(core.App, confgs)).Debugln("new configs posted")
2020-04-19 08:03:50 +00:00
if err = configs.ConnectConfigs(confgs, false); err != nil {
log.Errorln(err)
2020-01-28 12:15:48 +00:00
sendErrorJson(err, w, r)
2018-06-30 00:57:05 +00:00
return
}
2020-03-06 22:18:06 +00:00
if err := confgs.Save(utils.Directory); err != nil {
log.Errorln(err)
2020-03-06 09:33:46 +00:00
sendErrorJson(err, w, r)
2020-03-04 15:58:42 +00:00
return
2020-01-28 12:15:48 +00:00
}
2020-03-10 05:24:35 +00:00
exists := confgs.Db.HasTable("core")
2020-03-09 15:15:15 +00:00
if !exists {
if err := confgs.DropDatabase(); err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-10 05:24:35 +00:00
if err := confgs.CreateDatabase(); err != nil {
2020-03-09 15:15:15 +00:00
sendErrorJson(err, w, r)
return
}
if err := configs.CreateAdminUser(confgs); err != nil {
sendErrorJson(err, w, r)
return
}
if err := configs.TriggerSamples(); err != nil {
sendErrorJson(err, w, r)
return
}
}
2020-03-06 22:18:06 +00:00
if err = confgs.MigrateDatabase(); err != nil {
2020-01-28 12:15:48 +00:00
sendErrorJson(err, w, r)
2018-06-30 00:57:05 +00:00
return
}
log.Infoln("Migrating Notifiers...")
notifiers.InitNotifiers()
2020-03-09 15:15:15 +00:00
2020-03-04 10:29:00 +00:00
c := &core.Core{
Name: project,
Description: description,
2020-04-16 09:57:00 +00:00
ApiSecret: utils.Params.GetString("API_SECRET"),
Domain: domain,
Version: core.App.Version,
Started: utils.Now(),
CreatedAt: utils.Now(),
UseCdn: null.NewNullBool(false),
Footer: null.NewNullString(""),
2020-01-28 12:15:48 +00:00
}
2020-03-06 22:18:06 +00:00
log.Infoln("Creating new Core")
2020-03-04 10:29:00 +00:00
if err := c.Create(); err != nil {
log.Errorln(err)
2020-01-28 12:15:48 +00:00
sendErrorJson(err, w, r)
2018-06-30 00:57:05 +00:00
return
}
2020-03-04 10:29:00 +00:00
core.App = c
2020-03-06 22:18:06 +00:00
log.Infoln("Initializing new Statping instance")
2020-03-14 03:13:20 +00:00
if _, err := services.SelectAllServices(true); err != nil {
2020-03-04 10:29:00 +00:00
log.Errorln(err)
sendErrorJson(err, w, r)
return
2020-02-25 13:18:29 +00:00
}
2018-06-30 00:57:05 +00:00
2020-03-14 03:13:20 +00:00
go services.CheckServices()
core.App.Setup = true
2018-11-05 22:04:06 +00:00
CacheStorage.Delete("/")
2018-08-16 20:55:30 +00:00
resetCookies()
2020-01-28 12:15:48 +00:00
time.Sleep(1 * time.Second)
out := struct {
2020-03-04 10:29:00 +00:00
Message string `json:"message"`
Config *configs.DbConfig `json:"config"`
2020-01-28 12:15:48 +00:00
}{
2020-03-03 06:42:37 +00:00
"success",
2020-03-06 22:18:06 +00:00
confgs,
2020-01-28 12:15:48 +00:00
}
returnJson(out, w, r)
2018-06-30 00:57:05 +00:00
}