statping/handlers/setup.go

139 lines
3.2 KiB
Go
Raw Normal View History

2018-12-04 05:57:11 +00:00
// Statping
2018-08-16 06:22:20 +00:00
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
2018-12-04 04:17:29 +00:00
// https://github.com/hunterlong/statping
2018-08-16 06:22:20 +00:00
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-06-30 00:57:05 +00:00
package handlers
import (
2020-01-28 12:15:48 +00:00
"errors"
2020-03-09 15:15:15 +00:00
"github.com/hunterlong/statping/database"
"github.com/hunterlong/statping/notifiers"
2020-03-04 10:29:00 +00:00
"github.com/hunterlong/statping/types/configs"
"github.com/hunterlong/statping/types/core"
"github.com/hunterlong/statping/types/null"
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/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
2020-03-06 22:18:06 +00:00
//sample, _ := strconv.ParseBool(r.PostForm.Get("sample_data"))
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-03-09 15:15:15 +00:00
if err = configs.ConnectConfigs(confgs); err != nil {
log.Errorln(err)
2020-03-06 22:18:06 +00:00
if err := confgs.Delete(); err != nil {
log.Errorln(err)
sendErrorJson(err, w, r)
return
}
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-09 15:15:15 +00:00
exists := database.DB().HasTable("core")
if !exists {
if err := confgs.DropDatabase(); err != nil {
sendErrorJson(err, w, r)
return
}
if err := configs.CreateDatabase(); err != nil {
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
}
2020-03-09 15:15:15 +00:00
log.Infoln("Migrating Notifiers...")
if err := notifiers.Migrate(); err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
c := &core.Core{
Name: "Statping Sample Data",
Description: "This data is only used to testing",
//ApiKey: apiKey.(string),
//ApiSecret: apiSecret.(string),
Domain: "http://localhost:8080",
Version: "test",
CreatedAt: time.Now().UTC(),
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")
if err := core.InitApp(); 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
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
}