forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.go
183 lines (169 loc) · 4.84 KB
/
settings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <[email protected]> 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/>.
package handlers
import (
"fmt"
"github.com/gorilla/mux"
"github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/notifiers"
"github.com/hunterlong/statup/source"
"github.com/hunterlong/statup/utils"
"net/http"
)
func SettingsHandler(w http.ResponseWriter, r *http.Request) {
if !IsAuthenticated(r) {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
ExecuteResponse(w, r, "settings.html", core.CoreApp)
}
func SaveSettingsHandler(w http.ResponseWriter, r *http.Request) {
if !IsAuthenticated(r) {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
r.ParseForm()
name := r.PostForm.Get("project")
if name != "" {
core.CoreApp.Name = name
}
description := r.PostForm.Get("description")
if description != core.CoreApp.Description {
core.CoreApp.Description = description
}
style := r.PostForm.Get("style")
if style != core.CoreApp.Style {
core.CoreApp.Style = style
}
footer := r.PostForm.Get("footer")
if footer != core.CoreApp.Footer {
core.CoreApp.Footer = footer
}
domain := r.PostForm.Get("domain")
if domain != core.CoreApp.Domain {
core.CoreApp.Domain = domain
}
core.CoreApp.UseCdn = (r.PostForm.Get("enable_cdn") == "on")
core.CoreApp, _ = core.UpdateCore(core.CoreApp)
core.OnSettingsSaved(core.CoreApp.ToCore())
ExecuteResponse(w, r, "settings.html", core.CoreApp)
}
func SaveSASSHandler(w http.ResponseWriter, r *http.Request) {
if !IsAuthenticated(r) {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
r.ParseForm()
theme := r.PostForm.Get("theme")
variables := r.PostForm.Get("variables")
mobile := r.PostForm.Get("mobile")
source.SaveAsset([]byte(theme), utils.Directory, "scss/base.scss")
source.SaveAsset([]byte(variables), utils.Directory, "scss/variables.scss")
source.SaveAsset([]byte(mobile), utils.Directory, "scss/mobile.scss")
source.CompileSASS(utils.Directory)
ResetRouter()
ExecuteResponse(w, r, "settings.html", core.CoreApp)
}
func SaveAssetsHandler(w http.ResponseWriter, r *http.Request) {
if !IsAuthenticated(r) {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
dir := utils.Directory
err := source.CreateAllAssets(dir)
if err != nil {
utils.Log(3, err)
return
}
err = source.CompileSASS(dir)
if err != nil {
source.CopyToPublic(source.CssBox, dir+"/assets/css", "base.css")
utils.Log(2, "Default 'base.css' was insert because SASS did not work.")
}
ResetRouter()
ExecuteResponse(w, r, "settings.html", core.CoreApp)
}
func DeleteAssetsHandler(w http.ResponseWriter, r *http.Request) {
if !IsAuthenticated(r) {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
source.DeleteAllAssets(utils.Directory)
ResetRouter()
ExecuteResponse(w, r, "settings.html", core.CoreApp)
}
func SaveNotificationHandler(w http.ResponseWriter, r *http.Request) {
var err error
if !IsAuthenticated(r) {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
vars := mux.Vars(r)
r.ParseForm()
notifierId := vars["id"]
enabled := r.PostForm.Get("enable")
host := r.PostForm.Get("host")
port := int(utils.StringInt(r.PostForm.Get("port")))
username := r.PostForm.Get("username")
password := r.PostForm.Get("password")
var1 := r.PostForm.Get("var1")
var2 := r.PostForm.Get("var2")
apiKey := r.PostForm.Get("api_key")
apiSecret := r.PostForm.Get("api_secret")
limits := int(utils.StringInt(r.PostForm.Get("limits")))
notifer := notifiers.SelectNotifier(utils.StringInt(notifierId)).Select()
if host != "" {
notifer.Host = host
}
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
}
if enabled == "on" {
notifer.Enabled = true
} else {
notifer.Enabled = false
}
notifer, err = notifer.Update()
if err != nil {
utils.Log(3, err)
}
if notifer.Enabled {
notify := notifiers.SelectNotifier(notifer.Id)
go notify.Run()
}
utils.Log(1, fmt.Sprintf("Notifier saved: %v", notifer))
ExecuteResponse(w, r, "settings.html", core.CoreApp)
}