forked from xuri/aurora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
186 lines (176 loc) · 5.8 KB
/
handlers.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
184
185
186
// Copyright 2016 - 2018 The aurora Authors. All rights reserved. Use of this
// source code is governed by a MIT license that can be found in the LICENSE
// file.
//
// The aurora is a web-based Beanstalk queue server console written in Go
// and works on macOS, Linux and Windows machines. Main idea behind using Go
// for backend development is to utilize ability of the compiler to produce
// zero-dependency binaries for multiple platforms. aurora was created as an
// attempt to build very simple and portable application to work with local or
// remote Beanstalk server.
package main
import (
"bytes"
"io"
"net/http"
"net/url"
)
// handlerMain handle request on router: /
func handlerMain(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", "Go WebServer")
w.Header().Set("Content-Type", "text/html")
server := r.URL.Query().Get("server")
readCookies(r)
_, _ = io.WriteString(w, tplMain(getServerStatus(), server))
}
// handlerServerList handle request on router: /index
func handlerServerList(w http.ResponseWriter, r *http.Request) {
setHeader(w, r)
readCookies(r)
_, _ = io.WriteString(w, getServerStatus())
}
// serversRemove handle request on router: /serversRemove
func serversRemove(w http.ResponseWriter, r *http.Request) {
setHeader(w, r)
readCookies(r)
server := r.URL.Query().Get("removeServer")
removeServerInCookie(server, w, r)
removeServerInConfig(server)
http.Redirect(w, r, "/public", 301)
}
// handlerServer handle request on router: /server
func handlerServer(w http.ResponseWriter, r *http.Request) {
setHeader(w, r)
readCookies(r)
server := r.URL.Query().Get("server")
action := r.URL.Query().Get("action")
switch action {
case "reloader":
_, _ = io.WriteString(w, getServerTubes(server))
return
case "clearTubes":
_ = r.ParseForm()
clearTubes(server, r.Form)
_, _ = io.WriteString(w, `{"result":true}`)
return
}
_, _ = io.WriteString(w, tplServer(getServerTubes(server), server))
}
// handlerTube handle request on router: /tube
func handlerTube(w http.ResponseWriter, r *http.Request) {
setHeader(w, r)
readCookies(r)
server := r.URL.Query().Get("server")
tube := r.URL.Query().Get("tube")
action := r.URL.Query().Get("action")
count := r.URL.Query().Get("count")
switch action {
case "addjob":
addJob(server, r.PostFormValue("tubeName"), r.PostFormValue("tubeData"), r.PostFormValue("tubePriority"), r.PostFormValue("tubeDelay"), r.PostFormValue("tubeTtr"))
_, _ = io.WriteString(w, `{"result":true}`)
return
case "search":
content := searchTube(server, tube, r.URL.Query().Get("limit"), r.URL.Query().Get("searchStr"))
_, _ = io.WriteString(w, tplTube(content, server, tube))
return
case "addSample":
_ = r.ParseForm()
addSample(server, r.Form, w)
return
default:
handleRedirect(w, r, server, tube, action, count)
}
}
// handleRedirect handle request with redirect response.
func handleRedirect(w http.ResponseWriter, r *http.Request, server string, tube string, action string, count string) {
var link bytes.Buffer
link.WriteString(`/tube?server=`)
link.WriteString(server)
link.WriteString(`&tube=`)
switch action {
case "kick":
kick(server, tube, count)
link.WriteString(url.QueryEscape(tube))
http.Redirect(w, r, link.String(), 302)
case "kickJob":
kickJob(server, tube, r.URL.Query().Get("jobid"))
link.WriteString(url.QueryEscape(tube))
http.Redirect(w, r, link.String(), 302)
case "pause":
pause(server, tube, count)
link.WriteString(url.QueryEscape(tube))
http.Redirect(w, r, link.String(), 302)
case "moveJobsTo":
destTube := tube
if r.URL.Query().Get("destTube") != "" {
destTube = r.URL.Query().Get("destTube")
}
moveJobsTo(server, tube, destTube, r.URL.Query().Get("state"), r.URL.Query().Get("destState"))
link.WriteString(url.QueryEscape(destTube))
http.Redirect(w, r, link.String(), 302)
case "deleteAll":
deleteAll(server, tube)
link.WriteString(url.QueryEscape(tube))
http.Redirect(w, r, link.String(), 302)
case "deleteJob":
deleteJob(server, tube, r.URL.Query().Get("jobid"))
link.WriteString(url.QueryEscape(tube))
http.Redirect(w, r, link.String(), 302)
case "loadSample":
loadSample(server, tube, r.URL.Query().Get("key"))
link.WriteString(url.QueryEscape(tube))
http.Redirect(w, r, link.String(), 302)
}
_, _ = io.WriteString(w, tplTube(currentTube(server, tube), server, tube))
}
// handlerSample handle request on router: /sample
func handlerSample(w http.ResponseWriter, r *http.Request) {
setHeader(w, r)
readCookies(r)
action := r.URL.Query().Get("action")
server := r.URL.Query().Get("server")
switch action {
case "manageSamples":
_, _ = io.WriteString(w, tplSampleJobsManage(getSampleJobList(), server))
return
case "newSample":
_, _ = io.WriteString(w, tplSampleJobsManage(tplSampleJobEdit("", ""), server))
return
case "editSample":
_, _ = io.WriteString(w, tplSampleJobsManage(tplSampleJobEdit(r.URL.Query().Get("key"), ""), server))
return
case "actionNewSample":
_ = r.ParseForm()
newSample(server, r.Form, w, r)
return
case "actionEditSample":
_ = r.ParseForm()
editSample(server, r.Form, r.URL.Query().Get("key"), w, r)
return
case "deleteSample":
deleteSamples(r.URL.Query().Get("key"))
http.Redirect(w, r, "/sample?action=manageSamples", 301)
return
}
}
// handlerStatistics handle request on router: /statistics
func handlerStatistics(w http.ResponseWriter, r *http.Request) {
setHeader(w, r)
readCookies(r)
action := r.URL.Query().Get("action")
server := r.URL.Query().Get("server")
tube := r.URL.Query().Get("tube")
switch action {
case "preference":
_, _ = io.WriteString(w, tplStatisticSetting(tplStatisticEdit("")))
return
case "save":
_ = r.ParseForm()
statisticPreferenceSave(r.Form, w, r)
return
case "reloader":
_, _ = io.WriteString(w, statisticWaitress(server, tube))
return
}
_, _ = io.WriteString(w, tplStatistic(server, tube))
}