statping/handlers/setup.go

149 lines
3.1 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"
2020-05-29 22:06:13 +00:00
"net/url"
"strconv"
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")
2020-06-26 23:51:12 +00:00
sendNews, _ := strconv.ParseBool(r.PostForm.Get("newsletter"))
sendReports, _ := strconv.ParseBool(r.PostForm.Get("send_reports"))
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
}
2020-09-16 07:01:44 +00:00
if err := configs.CreateAdminUser(); err != nil {
2020-03-09 15:15:15 +00:00
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,
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(""),
Language: confgs.Language,
AllowReports: null.NewNullBool(sendReports),
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-05-29 22:06:13 +00:00
if sendNews {
2020-06-02 02:27:31 +00:00
log.Infof("Sending email address %s to newsletter server", confgs.Email)
2020-05-29 22:06:13 +00:00
if err := registerNews(confgs.Email, confgs.Domain); err != nil {
log.Errorln(err)
}
}
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
services.CheckServices()
2020-03-14 03:13:20 +00:00
core.App.Setup = true
2018-08-16 20:55:30 +00:00
resetCookies()
2020-08-08 04:26:32 +00:00
2020-01-28 12:15:48 +00:00
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
}
2020-05-29 22:06:13 +00:00
func registerNews(email, domain string) error {
if email == "" {
return nil
}
2020-05-29 22:06:13 +00:00
v := url.Values{}
v.Set("email", email)
v.Set("domain", domain)
v.Set("timezone", "UTC")
resp, err := http.PostForm("https://news.statping.com/new", v)
2020-05-29 22:06:13 +00:00
if err != nil {
return err
}
2020-06-02 02:27:31 +00:00
return resp.Body.Close()
2020-05-29 22:06:13 +00:00
}