statping/handlers/setup.go

158 lines
3.9 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-04 10:29:00 +00:00
"github.com/hunterlong/statping/types/configs"
"github.com/hunterlong/statping/types/core"
"github.com/hunterlong/statping/types/null"
"github.com/hunterlong/statping/types/users"
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/utils"
2018-06-30 00:57:05 +00:00
"net/http"
2020-01-28 12:15:48 +00:00
"strconv"
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
}
if err = r.ParseForm(); err != nil {
log.Errorln(err)
sendErrorJson(err, w, r)
2018-06-30 00:57:05 +00:00
return
}
dbHost := r.PostForm.Get("db_host")
dbUser := r.PostForm.Get("db_user")
dbPass := r.PostForm.Get("db_password")
dbDatabase := r.PostForm.Get("db_database")
dbConn := r.PostForm.Get("db_connection")
dbPort := utils.ToInt(r.PostForm.Get("db_port"))
2018-06-30 00:57:05 +00:00
project := r.PostForm.Get("project")
username := r.PostForm.Get("username")
password := r.PostForm.Get("password")
description := r.PostForm.Get("description")
domain := r.PostForm.Get("domain")
email := r.PostForm.Get("email")
2020-01-28 12:15:48 +00:00
sample, _ := strconv.ParseBool(r.PostForm.Get("sample_data"))
2020-03-04 10:29:00 +00:00
confg := &configs.DbConfig{
2018-08-19 08:48:02 +00:00
DbConn: dbConn,
DbHost: dbHost,
DbUser: dbUser,
DbPass: dbPass,
DbData: dbDatabase,
2020-02-26 06:59:56 +00:00
DbPort: int(dbPort),
2018-08-19 08:48:02 +00:00
Project: project,
Description: description,
Domain: domain,
Username: username,
Password: password,
Email: email,
Error: nil,
2018-08-19 23:51:56 +00:00
Location: utils.Directory,
2020-03-03 06:42:37 +00:00
}
2018-08-19 08:48:02 +00:00
2020-03-04 10:29:00 +00:00
log.WithFields(utils.ToFields(core.App, confg)).Debugln("new configs posted")
2019-12-30 08:08:51 +00:00
2020-03-04 10:29:00 +00:00
if err := confg.Save(utils.Directory); err != nil {
log.Errorln(err)
2020-01-28 12:15:48 +00:00
sendErrorJson(err, w, r)
return
}
2020-03-04 15:58:42 +00:00
if confg, err = configs.LoadConfigFile(utils.Directory); 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
if err = configs.ConnectConfigs(confg); err != nil {
log.Errorln(err)
2020-03-04 10:29:00 +00:00
if err := confg.Delete(); err != nil {
log.Errorln(err)
sendErrorJson(err, w, r)
2020-03-04 15:58:42 +00:00
return
2020-03-04 10:29:00 +00:00
}
2020-03-04 15:58:42 +00:00
return
2020-01-28 12:15:48 +00:00
}
2020-03-04 14:20:47 +00:00
if err = confg.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-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-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
admin := &users.User{
Username: confg.Username,
Password: confg.Password,
Email: confg.Email,
Admin: null.NewNullBool(true),
}
if err := admin.Create(); err != nil {
log.Errorln(err)
sendErrorJson(err, w, r)
return
2020-02-25 13:18:29 +00:00
}
2018-06-30 00:57:05 +00:00
2019-04-03 15:12:44 +00:00
if sample {
2020-03-04 10:29:00 +00:00
if err = configs.TriggerSamples(); err != nil {
2020-01-28 12:15:48 +00:00
sendErrorJson(err, w, r)
return
}
2019-04-03 15:12:44 +00:00
}
2020-03-04 10:29:00 +00:00
2018-07-02 06:21:41 +00:00
core.InitApp()
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-04 10:29:00 +00:00
confg,
2020-01-28 12:15:48 +00:00
}
returnJson(out, w, r)
2018-06-30 00:57:05 +00:00
}
func setupResponseError(w http.ResponseWriter, r *http.Request, a interface{}) {
2018-12-06 19:03:55 +00:00
ExecuteResponse(w, r, "setup.gohtml", a, nil)
2018-06-30 00:57:05 +00:00
}