1
// Copyright 2016 - 2020 The aurora Authors. All rights reserved. Use of this
2
// source code is governed by a MIT license that can be found in the LICENSE
3
// file.
4
//
5
// The aurora is a web-based beanstalkd queue server console written in Go
6
// and works on macOS, Linux and Windows machines. Main idea behind using Go
7
// for backend development is to utilize ability of the compiler to produce
8
// zero-dependency binaries for multiple platforms. aurora was created as an
9
// attempt to build very simple and portable application to work with local or
10
// remote beanstalkd server.
11

12
package main
13

14
import (
15
	"io"
16
	"net/http"
17
	"net/url"
18
	"strings"
19
)
20

21
// handlerMain handle request on router: /
22
func handlerMain(w http.ResponseWriter, r *http.Request) {
23 10
	w.Header().Set("Server", "Go WebServer")
24 10
	w.Header().Set("Content-Type", "text/html")
25 10
	server := r.URL.Query().Get("server")
26 10
	readCookies(r)
27 10
	_, _ = io.WriteString(w, tplMain(getServerStatus(), server))
28
}
29

30
// handlerServerList handle request on router: /index
31
func handlerServerList(w http.ResponseWriter, r *http.Request) {
32 10
	setHeader(w, r)
33 10
	readCookies(r)
34 10
	_, _ = io.WriteString(w, getServerStatus())
35
}
36

37
// serversRemove handle request on router: /serversRemove
38
func serversRemove(w http.ResponseWriter, r *http.Request) {
39 10
	setHeader(w, r)
40 10
	readCookies(r)
41 10
	server := r.URL.Query().Get("removeServer")
42 10
	removeServerInCookie(server, w, r)
43 10
	removeServerInConfig(server)
44 10
	w.Header().Set("Location", "./public")
45 10
	w.WriteHeader(307)
46
}
47

48
// handlerServer handle request on router: /server
49
func handlerServer(w http.ResponseWriter, r *http.Request) {
50 10
	setHeader(w, r)
51 10
	readCookies(r)
52 10
	server := r.URL.Query().Get("server")
53 10
	action := r.URL.Query().Get("action")
54 10
	switch action {
55 10
	case "reloader":
56 10
		_, _ = io.WriteString(w, getServerTubes(server))
57 10
		return
58 10
	case "clearTubes":
59 10
		_ = r.ParseForm()
60 10
		clearTubes(server, r.Form)
61 10
		_, _ = io.WriteString(w, `{"result":true}`)
62 10
		return
63
	}
64 10
	_, _ = io.WriteString(w, tplServer(getServerTubes(server), server))
65
}
66

67
// handlerTube handle request on router: /tube
68
func handlerTube(w http.ResponseWriter, r *http.Request) {
69 10
	setHeader(w, r)
70 10
	readCookies(r)
71 10
	server := r.URL.Query().Get("server")
72 10
	tube := r.URL.Query().Get("tube")
73 10
	action := r.URL.Query().Get("action")
74 10
	count := r.URL.Query().Get("count")
75 10
	switch action {
76 10
	case "addjob":
77 10
		addJob(server, r.PostFormValue("tubeName"), r.PostFormValue("tubeData"), r.PostFormValue("tubePriority"), r.PostFormValue("tubeDelay"), r.PostFormValue("tubeTtr"))
78 10
		_, _ = io.WriteString(w, `{"result":true}`)
79 10
		return
80 10
	case "search":
81 10
		content := searchTube(server, tube, r.URL.Query().Get("limit"), r.URL.Query().Get("searchStr"))
82 10
		_, _ = io.WriteString(w, tplTube(content, server, tube))
83 10
		return
84 10
	case "addSample":
85 10
		_ = r.ParseForm()
86 10
		addSample(server, r.Form, w)
87 10
		return
88 10
	default:
89 10
		handleRedirect(w, r, server, tube, action, count)
90
	}
91
}
92

93
// handleRedirect handle request with redirect response.
94
func handleRedirect(w http.ResponseWriter, r *http.Request, server string, tube string, action string, count string) {
95 10
	var link strings.Builder
96 10
	link.WriteString(`./tube?server=`)
97 10
	link.WriteString(server)
98 10
	link.WriteString(`&tube=`)
99 10
	switch action {
100 10
	case "kick":
101 10
		kick(server, tube, count)
102 10
		link.WriteString(url.QueryEscape(tube))
103 10
		w.Header().Set("Location", link.String())
104 10
		w.WriteHeader(307)
105 10
	case "kickJob":
106 10
		kickJob(server, tube, r.URL.Query().Get("jobid"))
107 10
		link.WriteString(url.QueryEscape(tube))
108 10
		w.Header().Set("Location", link.String())
109 10
		w.WriteHeader(307)
110 10
	case "pause":
111 10
		pause(server, tube, count)
112 10
		link.WriteString(url.QueryEscape(tube))
113 10
		w.Header().Set("Location", link.String())
114 10
		w.WriteHeader(307)
115 10
	case "moveJobsTo":
116 10
		destTube := tube
117 10
		if r.URL.Query().Get("destTube") != "" {
118 10
			destTube = r.URL.Query().Get("destTube")
119
		}
120 10
		moveJobsTo(server, tube, destTube, r.URL.Query().Get("state"), r.URL.Query().Get("destState"))
121 10
		link.WriteString(url.QueryEscape(destTube))
122 10
		w.Header().Set("Location", link.String())
123 10
		w.WriteHeader(307)
124 10
	case "deleteAll":
125 10
		deleteAll(server, tube)
126 10
		link.WriteString(url.QueryEscape(tube))
127 10
		w.Header().Set("Location", link.String())
128 10
		w.WriteHeader(307)
129 10
	case "deleteJob":
130 10
		deleteJob(server, tube, r.URL.Query().Get("jobid"))
131 10
		link.WriteString(url.QueryEscape(tube))
132 10
		w.Header().Set("Location", link.String())
133 10
		w.WriteHeader(307)
134 10
	case "loadSample":
135 10
		loadSample(server, tube, r.URL.Query().Get("key"))
136 10
		link.WriteString(url.QueryEscape(tube))
137 10
		w.Header().Set("Location", link.String())
138 10
		w.WriteHeader(307)
139
	}
140 10
	_, _ = io.WriteString(w, tplTube(currentTube(server, tube), server, tube))
141
}
142

143
// handlerSample handle request on router: /sample
144
func handlerSample(w http.ResponseWriter, r *http.Request) {
145 10
	setHeader(w, r)
146 10
	readCookies(r)
147 10
	action := r.URL.Query().Get("action")
148 10
	server := r.URL.Query().Get("server")
149 10
	switch action {
150 10
	case "manageSamples":
151 10
		_, _ = io.WriteString(w, tplSampleJobsManage(getSampleJobList(), server))
152 10
		return
153 10
	case "newSample":
154 10
		_, _ = io.WriteString(w, tplSampleJobsManage(tplSampleJobEdit("", ""), server))
155 10
		return
156 10
	case "editSample":
157 10
		_, _ = io.WriteString(w, tplSampleJobsManage(tplSampleJobEdit(r.URL.Query().Get("key"), ""), server))
158 10
		return
159 10
	case "actionNewSample":
160 10
		_ = r.ParseForm()
161 10
		newSample(server, r.Form, w, r)
162 10
		return
163 0
	case "actionEditSample":
164 0
		_ = r.ParseForm()
165 0
		editSample(server, r.Form, r.URL.Query().Get("key"), w, r)
166 0
		return
167 0
	case "deleteSample":
168 0
		deleteSamples(r.URL.Query().Get("key"))
169 0
		w.Header().Set("Location", "./sample?action=manageSamples")
170 0
		w.WriteHeader(307)
171 0
		return
172
	}
173
}
174

175
// handlerStatistics handle request on router: /statistics
176
func handlerStatistics(w http.ResponseWriter, r *http.Request) {
177 10
	setHeader(w, r)
178 10
	readCookies(r)
179 10
	action := r.URL.Query().Get("action")
180 10
	server := r.URL.Query().Get("server")
181 10
	tube := r.URL.Query().Get("tube")
182 10
	switch action {
183 10
	case "preference":
184 10
		_, _ = io.WriteString(w, tplStatisticSetting(tplStatisticEdit("")))
185 10
		return
186 10
	case "save":
187 10
		_ = r.ParseForm()
188 10
		statisticPreferenceSave(r.Form, w, r)
189 10
		return
190 10
	case "reloader":
191 10
		_, _ = io.WriteString(w, statisticWaitress(server, tube))
192 10
		return
193
	}
194 10
	_, _ = io.WriteString(w, tplStatistic(server, tube))
195
}

Read our documentation on viewing source code .

Loading