statping/handlers/settings.go

140 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 (
"fmt"
2018-07-12 06:53:18 +00:00
"github.com/gorilla/mux"
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"
2018-06-30 00:57:05 +00:00
"net/http"
"net/url"
"strconv"
2018-06-30 00:57:05 +00:00
)
func settingsHandler(w http.ResponseWriter, r *http.Request) {
2018-07-02 06:21:41 +00:00
if !IsAuthenticated(r) {
2018-06-30 00:57:05 +00:00
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
ExecuteResponse(w, r, "settings.html", core.CoreApp, nil)
2018-06-30 00:57:05 +00:00
}
func saveSettingsHandler(w http.ResponseWriter, r *http.Request) {
2018-07-02 06:21:41 +00:00
if !IsAuthenticated(r) {
2018-06-30 00:57:05 +00:00
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
var err error
2018-06-30 00:57:05 +00:00
r.ParseForm()
app := core.CoreApp
2018-06-30 00:57:05 +00:00
name := r.PostForm.Get("project")
if name != "" {
app.Name = name
2018-06-30 00:57:05 +00:00
}
description := r.PostForm.Get("description")
if description != app.Description {
app.Description = description
2018-06-30 00:57:05 +00:00
}
style := r.PostForm.Get("style")
if style != app.Style {
app.Style = style
2018-06-30 00:57:05 +00:00
}
footer := r.PostForm.Get("footer")
if footer != app.Footer.String {
app.Footer = types.NewNullString(footer)
2018-06-30 00:57:05 +00:00
}
domain := r.PostForm.Get("domain")
if domain != app.Domain {
app.Domain = domain
2018-06-30 00:57:05 +00:00
}
timezone := r.PostForm.Get("timezone")
timeFloat, _ := strconv.ParseFloat(timezone, 10)
app.Timezone = float32(timeFloat)
app.UseCdn = types.NewNullBool(r.PostForm.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())
ExecuteResponse(w, r, "settings.html", core.CoreApp, "/settings")
2018-06-30 00:57:05 +00:00
}
func saveSASSHandler(w http.ResponseWriter, r *http.Request) {
2018-07-02 06:21:41 +00:00
if !IsAuthenticated(r) {
2018-06-30 00:57:05 +00:00
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
r.ParseForm()
theme := r.PostForm.Get("theme")
variables := r.PostForm.Get("variables")
2018-08-23 07:28:48 +00:00
mobile := r.PostForm.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()
ExecuteResponse(w, r, "settings.html", core.CoreApp, "/settings")
2018-06-30 00:57:05 +00:00
}
func saveAssetsHandler(w http.ResponseWriter, r *http.Request) {
2018-07-02 06:21:41 +00:00
if !IsAuthenticated(r) {
2018-06-30 00:57:05 +00:00
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
2018-07-28 03:24:54 +00:00
dir := utils.Directory
2018-08-16 02:22:10 +00:00
err := source.CreateAllAssets(dir)
if err != nil {
utils.Log(3, err)
return
}
err = source.CompileSASS(dir)
2018-07-28 01:50:13 +00:00
if 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()
ExecuteResponse(w, r, "settings.html", core.CoreApp, "/settings")
2018-06-30 00:57:05 +00:00
}
func deleteAssetsHandler(w http.ResponseWriter, r *http.Request) {
2018-08-12 19:23:04 +00:00
if !IsAuthenticated(r) {
http.Redirect(w, r, "/", http.StatusSeeOther)
2018-07-22 22:17:38 +00:00
return
}
2018-08-16 20:55:30 +00:00
source.DeleteAllAssets(utils.Directory)
2018-10-06 05:56:08 +00:00
resetRouter()
ExecuteResponse(w, r, "settings.html", core.CoreApp, "/settings")
}
func parseId(r *http.Request) int64 {
vars := mux.Vars(r)
return utils.ToInt(vars["id"])
}
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
}