statping/handlers/setup.go

141 lines
3.5 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"
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/core"
2020-02-25 13:18:29 +00:00
"github.com/hunterlong/statping/database"
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/types"
"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-01-28 12:15:48 +00:00
if core.CoreApp.Setup {
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"))
dir := utils.Directory
2020-03-03 06:42:37 +00:00
configs := &types.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-03 06:42:37 +00:00
log.WithFields(utils.ToFields(core.CoreApp, configs)).Debugln("new configs posted")
2019-12-30 08:08:51 +00:00
2020-03-03 06:42:37 +00:00
if err := core.SaveConfig(configs); err != nil {
log.Errorln(err)
2020-01-28 12:15:48 +00:00
sendErrorJson(err, w, r)
return
}
2019-12-30 08:08:51 +00:00
if _, err = core.LoadConfigFile(dir); 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-03 06:42:37 +00:00
if err = core.CoreApp.Connect(configs, false, dir); err != nil {
log.Errorln(err)
core.DeleteConfig()
2020-01-28 12:15:48 +00:00
sendErrorJson(err, w, r)
return
}
if err = core.CoreApp.DropDatabase(); err != nil {
sendErrorJson(err, w, r)
2018-06-30 00:57:05 +00:00
return
}
2020-01-28 12:15:48 +00:00
if err = core.CoreApp.CreateDatabase(); err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-03 06:42:37 +00:00
core.CoreApp, err = core.InsertCore(configs)
2018-06-30 00:57:05 +00:00
if 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-02-25 13:18:29 +00:00
admin := &types.User{
2020-03-03 06:42:37 +00:00
Username: configs.Username,
Password: configs.Password,
Email: configs.Email,
Admin: types.NewNullBool(true),
2020-02-25 13:18:29 +00:00
}
database.Create(admin)
2018-06-30 00:57:05 +00:00
2019-04-03 15:12:44 +00:00
if sample {
2020-01-28 12:15:48 +00:00
if err = core.SampleData(); err != nil {
sendErrorJson(err, w, r)
return
}
2019-04-03 15:12:44 +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 {
Message string `json:"message"`
Config *types.DbConfig `json:"config"`
}{
2020-03-03 06:42:37 +00:00
"success",
configs,
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
}