statping/handlers/settings.go

189 lines
5.0 KiB
Go
Raw Normal View History

2018-08-16 06:22:20 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// 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 (
2018-07-12 06:53:18 +00:00
"fmt"
"github.com/gorilla/mux"
2018-06-30 00:57:05 +00:00
"github.com/hunterlong/statup/core"
2018-09-12 04:14:22 +00:00
"github.com/hunterlong/statup/core/notifier"
2018-08-10 04:38:54 +00:00
"github.com/hunterlong/statup/source"
2018-06-30 00:57:05 +00:00
"github.com/hunterlong/statup/utils"
"net/http"
"net/url"
2018-06-30 00:57:05 +00:00
)
2018-07-17 09:18:20 +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
}
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 {
app.Footer = 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
}
app.UseCdn = (r.PostForm.Get("enable_cdn") == "on")
core.CoreApp, _ = core.UpdateCore(app)
//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-08-23 07:28:48 +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-07-28 01:50:13 +00:00
utils.Log(2, "Default 'base.css' was insert because SASS did not work.")
}
2018-08-23 07:28:48 +00:00
ResetRouter()
ExecuteResponse(w, r, "settings.html", core.CoreApp, "/settings")
2018-06-30 00:57:05 +00:00
}
2018-08-12 19:23:04 +00:00
func DeleteAssetsHandler(w http.ResponseWriter, r *http.Request) {
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-08-23 07:28:48 +00:00
ResetRouter()
ExecuteResponse(w, r, "settings.html", core.CoreApp, "/settings")
}
func parseId(r *http.Request) int64 {
vars := mux.Vars(r)
return utils.StringInt(vars["id"])
}
func parseForm(r *http.Request) url.Values {
r.ParseForm()
return r.PostForm
2018-07-22 22:17:38 +00:00
}
func SaveNotificationHandler(w http.ResponseWriter, r *http.Request) {
2018-07-12 06:53:18 +00:00
var err error
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
}
form := parseForm(r)
2018-07-12 06:53:18 +00:00
vars := mux.Vars(r)
method := vars["method"]
enabled := form.Get("enable")
host := form.Get("host")
port := int(utils.StringInt(form.Get("port")))
username := form.Get("username")
password := form.Get("password")
var1 := form.Get("var1")
var2 := form.Get("var2")
apiKey := form.Get("api_key")
apiSecret := form.Get("api_secret")
limits := int(utils.StringInt(form.Get("limits")))
2018-06-30 00:57:05 +00:00
notifer, notif, err := notifier.SelectNotifier(method)
if err != nil {
utils.Log(3, fmt.Sprintf("issue saving notifier %v: %v", method, err))
ExecuteResponse(w, r, "settings.html", core.CoreApp, "/settings")
return
}
2018-07-12 06:53:18 +00:00
if host != "" {
notifer.Host = host
2018-07-02 06:21:41 +00:00
}
if port != 0 {
notifer.Port = port
}
if username != "" {
notifer.Username = username
}
if password != "" && password != "##########" {
notifer.Password = password
}
if var1 != "" {
notifer.Var1 = var1
}
if var2 != "" {
notifer.Var2 = var2
}
if apiKey != "" {
notifer.ApiKey = apiKey
}
if apiSecret != "" {
notifer.ApiSecret = apiSecret
}
if limits != 0 {
notifer.Limits = limits
}
notifer.Enabled = enabled == "on"
_, err = notifier.Update(notif, notifer)
if err != nil {
utils.Log(3, fmt.Sprintf("issue updating notifier: %v", err))
}
2018-09-12 04:14:22 +00:00
notifier.OnSave(notifer.Method)
ExecuteResponse(w, r, "settings.html", core.CoreApp, "/settings")
}