statping/handlers/settings.go

203 lines
5.7 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 (
2019-05-14 19:13:52 +00:00
"bytes"
"fmt"
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/core"
"github.com/hunterlong/statping/source"
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
2019-05-14 19:13:52 +00:00
"io"
2018-06-30 00:57:05 +00:00
"net/http"
"net/url"
"strconv"
2019-05-14 19:13:52 +00:00
"strings"
"time"
2018-06-30 00:57:05 +00:00
)
func settingsHandler(w http.ResponseWriter, r *http.Request) {
2018-12-06 19:03:55 +00:00
ExecuteResponse(w, r, "settings.gohtml", core.CoreApp, nil)
2018-06-30 00:57:05 +00:00
}
func saveSettingsHandler(w http.ResponseWriter, r *http.Request) {
var err error
2019-03-05 20:13:25 +00:00
form := parseForm(r)
app := core.CoreApp
2019-03-05 20:13:25 +00:00
name := form.Get("project")
2018-06-30 00:57:05 +00:00
if name != "" {
app.Name = name
2018-06-30 00:57:05 +00:00
}
2019-03-05 20:13:25 +00:00
description := form.Get("description")
if description != app.Description {
app.Description = description
2018-06-30 00:57:05 +00:00
}
2019-03-05 20:13:25 +00:00
style := form.Get("style")
if style != app.Style {
app.Style = style
2018-06-30 00:57:05 +00:00
}
2019-03-05 20:13:25 +00:00
footer := form.Get("footer")
if footer != app.Footer.String {
app.Footer = types.NewNullString(footer)
2018-06-30 00:57:05 +00:00
}
2019-03-05 20:13:25 +00:00
domain := form.Get("domain")
if domain != app.Domain {
app.Domain = domain
2018-06-30 00:57:05 +00:00
}
2019-03-05 20:13:25 +00:00
timezone := form.Get("timezone")
timeFloat, _ := strconv.ParseFloat(timezone, 10)
app.Timezone = float32(timeFloat)
2019-03-05 20:13:25 +00:00
app.UseCdn = types.NewNullBool(form.Get("enable_cdn") == "on")
core.CoreApp, err = core.UpdateCore(app)
if err != nil {
utils.Log(3, fmt.Sprintf("issue updating Core: %v", err.Error()))
}
//notifiers.OnSettingsSaved(core.CoreApp.ToCore())
2018-12-06 19:03:55 +00:00
ExecuteResponse(w, r, "settings.gohtml", core.CoreApp, "/settings")
2018-06-30 00:57:05 +00:00
}
func saveSASSHandler(w http.ResponseWriter, r *http.Request) {
2019-03-05 20:13:25 +00:00
form := parseForm(r)
2019-02-06 18:51:30 +00:00
theme := form.Get("theme")
variables := form.Get("variables")
mobile := form.Get("mobile")
2018-08-16 02:22:10 +00:00
source.SaveAsset([]byte(theme), utils.Directory, "scss/base.scss")
source.SaveAsset([]byte(variables), utils.Directory, "scss/variables.scss")
2018-08-23 07:28:48 +00:00
source.SaveAsset([]byte(mobile), utils.Directory, "scss/mobile.scss")
2018-08-16 02:22:10 +00:00
source.CompileSASS(utils.Directory)
2018-10-06 05:56:08 +00:00
resetRouter()
2018-12-06 19:03:55 +00:00
ExecuteResponse(w, r, "settings.gohtml", core.CoreApp, "/settings")
2018-06-30 00:57:05 +00:00
}
func saveAssetsHandler(w http.ResponseWriter, r *http.Request) {
2018-07-28 03:24:54 +00:00
dir := utils.Directory
2019-03-05 20:13:25 +00:00
if err := source.CreateAllAssets(dir); err != nil {
2018-08-16 02:22:10 +00:00
utils.Log(3, err)
2018-12-04 08:15:33 +00:00
sendErrorJson(err, w, r)
2018-08-16 02:22:10 +00:00
return
}
2019-03-05 20:13:25 +00:00
if err := source.CompileSASS(dir); err != nil {
2018-08-11 16:24:32 +00:00
source.CopyToPublic(source.CssBox, dir+"/assets/css", "base.css")
2018-11-06 07:15:55 +00:00
utils.Log(3, "Default 'base.css' was inserted because SASS did not work.")
2018-07-28 01:50:13 +00:00
}
2018-10-06 05:56:08 +00:00
resetRouter()
2018-12-06 19:03:55 +00:00
ExecuteResponse(w, r, "settings.gohtml", core.CoreApp, "/settings")
2018-06-30 00:57:05 +00:00
}
func deleteAssetsHandler(w http.ResponseWriter, r *http.Request) {
2019-03-05 20:13:25 +00:00
if err := source.DeleteAllAssets(utils.Directory); err != nil {
utils.Log(3, fmt.Errorf("error deleting all assets %v", err))
2018-07-22 22:17:38 +00:00
}
2018-10-06 05:56:08 +00:00
resetRouter()
2018-12-06 19:03:55 +00:00
ExecuteResponse(w, r, "settings.gohtml", core.CoreApp, "/settings")
}
2019-05-14 19:13:52 +00:00
func bulkImportHandler(w http.ResponseWriter, r *http.Request) {
var fileData bytes.Buffer
file, _, err := r.FormFile("file")
if err != nil {
utils.Log(3, fmt.Errorf("error bulk import services: %v", err))
w.Write([]byte(err.Error()))
return
}
defer file.Close()
io.Copy(&fileData, file)
data := fileData.String()
for i, line := range strings.Split(strings.TrimSuffix(data, "\n"), "\n")[1:] {
col := strings.Split(line, ",")
newService, err := commaToService(col)
if err != nil {
utils.Log(3, fmt.Errorf("issue with row %v: %v", i, err))
2019-05-14 20:42:47 +00:00
continue
2019-05-14 19:13:52 +00:00
}
service := core.ReturnService(newService)
_, err = service.Create(true)
if err != nil {
utils.Log(3, fmt.Errorf("cannot create service %v: %v", col[0], err))
2019-05-14 20:42:47 +00:00
continue
2019-05-14 19:13:52 +00:00
}
utils.Log(1, fmt.Sprintf("Created new service %v", service.Name))
}
ExecuteResponse(w, r, "settings.gohtml", core.CoreApp, "/settings")
}
// commaToService will convert a CSV comma delimited string slice to a Service type
// this function is used for the bulk import services feature
func commaToService(s []string) (*types.Service, error) {
if len(s) != 16 {
err := fmt.Errorf("does not have the expected amount of %v columns for a service", 16)
return nil, err
}
interval, err := time.ParseDuration(s[4])
if err != nil {
return nil, err
}
timeout, err := time.ParseDuration(s[9])
if err != nil {
return nil, err
}
2019-05-14 19:44:38 +00:00
allowNotifications, err := strconv.ParseBool(s[11])
2019-05-14 19:13:52 +00:00
if err != nil {
return nil, err
}
2019-05-14 19:44:38 +00:00
public, err := strconv.ParseBool(s[12])
2019-05-14 19:13:52 +00:00
if err != nil {
return nil, err
}
newService := &types.Service{
Name: s[0],
Domain: s[1],
Expected: types.NewNullString(s[2]),
ExpectedStatus: int(utils.ToInt(s[3])),
Interval: int(utils.ToInt(interval.Seconds())),
Type: s[5],
Method: s[6],
PostData: types.NewNullString(s[7]),
Port: int(utils.ToInt(s[8])),
Timeout: int(utils.ToInt(timeout.Seconds())),
AllowNotifications: types.NewNullBool(allowNotifications),
Public: types.NewNullBool(public),
2019-05-14 19:44:38 +00:00
GroupId: int(utils.ToInt(s[13])),
Headers: types.NewNullString(s[14]),
Permalink: types.NewNullString(s[15]),
2019-05-14 19:13:52 +00:00
}
return newService, nil
}
func parseForm(r *http.Request) url.Values {
r.ParseForm()
return r.PostForm
2018-07-22 22:17:38 +00:00
}
2018-09-18 22:02:27 +00:00
func parseGet(r *http.Request) url.Values {
r.ParseForm()
return r.Form
}