-
Notifications
You must be signed in to change notification settings - Fork 19
/
handlers.go
328 lines (269 loc) · 8.21 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package main
import (
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
. "github.com/jakecoffman/gorunner/service"
"github.com/nu7hatch/gouuid"
)
var nothing = map[string]string{}
func errHelp(msg string) map[string]interface{} {
return map[string]interface{}{"error": msg}
}
// General
func app(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "web/static/app.html")
}
func wsHandler(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
// Upgrade the HTTP connection to a websocket
ws, err := websocket.Upgrade(w, r, nil, 1024, 1024)
if _, ok := err.(websocket.HandshakeError); ok {
return http.StatusBadRequest, errHelp("Not a websocket handshake")
} else if err != nil {
return http.StatusInternalServerError, errHelp(err.Error())
}
conn := NewConnection(ws)
c.Hub().Register(conn)
defer c.Hub().Unregister(conn)
go conn.Writer()
conn.Reader()
return http.StatusOK, nothing
}
// Jobs
func listJobs(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
return http.StatusOK, c.JobList().Dump()
}
func addJob(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
payload := unmarshal(r.Body, "name", w)
err := c.JobList().Append(Job{Name: payload["name"], Status: "New"})
if err != nil {
return http.StatusInternalServerError, err.Error()
}
return http.StatusCreated, nothing
}
func getJob(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
vars := mux.Vars(r)
job, err := c.JobList().Get(vars["job"])
if err != nil {
return http.StatusNotFound, err.Error()
}
return http.StatusOK, job
}
func deleteJob(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
vars := mux.Vars(r)
job, err := c.JobList().Get(vars["job"])
if err != nil {
return http.StatusNotFound, err.Error()
}
err = c.JobList().Delete(job.ID())
if err != nil {
return http.StatusInternalServerError, err.Error()
}
return http.StatusOK, nothing
}
func addTaskToJob(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
vars := mux.Vars(r)
job, err := c.JobList().Get(vars["job"])
if err != nil {
return http.StatusNotFound, err.Error()
}
j := job.(Job)
payload := unmarshal(r.Body, "task", w)
j.AppendTask(payload["task"])
c.JobList().Update(j)
return http.StatusCreated, nothing
}
func removeTaskFromJob(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
vars := mux.Vars(r)
job, err := c.JobList().Get(vars["job"])
if err != nil {
return http.StatusNotFound, err.Error()
}
j := job.(Job)
taskPosition, err := strconv.Atoi(vars["task"])
if err != nil {
return http.StatusBadRequest, err.Error()
}
j.DeleteTask(taskPosition)
c.JobList().Update(j)
return http.StatusOK, nothing
}
func addTriggerToJob(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
vars := mux.Vars(r)
job, err := c.JobList().Get(vars["job"])
if err != nil {
return http.StatusNotFound, err.Error()
}
j := job.(Job)
payload := unmarshal(r.Body, "trigger", w)
j.AppendTrigger(payload["trigger"])
t, err := c.TriggerList().Get(payload["trigger"])
if err != nil {
return http.StatusInternalServerError, err.Error()
}
c.Executor().ArmTrigger(t.(Trigger))
c.JobList().Update(j)
return http.StatusCreated, nothing
}
func removeTriggerFromJob(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
vars := mux.Vars(r)
job, err := c.JobList().Get(vars["job"])
if err != nil {
return http.StatusNotFound, err.Error()
}
j := job.(Job)
t := vars["trigger"]
j.DeleteTrigger(t)
c.JobList().Update(j)
// If Trigger is no longer attached to any Jobs, remove it from Cron to save cycles
jobs := c.JobList().GetJobsWithTrigger(t)
if len(jobs) == 0 {
c.Executor().DisarmTrigger(t)
}
return http.StatusOK, nothing
}
// Run
func listRuns(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
offset := r.FormValue("offset")
length := r.FormValue("length")
if offset == "" {
offset = "-1"
}
if length == "" {
length = "-1"
}
o, err := strconv.Atoi(offset)
if err != nil {
return http.StatusBadRequest, err.Error()
}
l, err := strconv.Atoi(length)
if err != nil {
return http.StatusBadRequest, err.Error()
}
return http.StatusOK, c.RunList().GetRecent(o, l)
}
func addRun(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
payload := unmarshal(r.Body, "job", w)
job, err := c.JobList().Get(payload["job"])
if err != nil {
return http.StatusInternalServerError, err.Error()
}
j := job.(Job)
id, err := uuid.NewV4()
if err != nil {
return http.StatusInternalServerError, err.Error()
}
var tasks []Task
for _, taskName := range j.Tasks {
task, err := c.TaskList().Get(taskName)
if err != nil {
panic(err)
}
t := task.(Task)
tasks = append(tasks, t)
}
err = c.RunList().AddRun(id.String(), j, tasks)
if err != nil {
return http.StatusInternalServerError, err.Error()
}
return http.StatusCreated, map[string]string{"uuid": id.String()}
}
func getRun(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
vars := mux.Vars(r)
run, err := c.RunList().Get(vars["run"])
if err != nil {
return http.StatusNotFound, err.Error()
}
return http.StatusOK, run
}
// Tasks
func listTasks(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
return http.StatusOK, c.TaskList().Dump()
}
func addTask(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
payload := unmarshal(r.Body, "name", w)
err := c.TaskList().Append(Task{payload["name"], ""})
if err != nil {
return http.StatusBadRequest, err.Error()
}
return http.StatusCreated, nothing
}
func getTask(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
vars := mux.Vars(r)
task, err := c.TaskList().Get(vars["task"])
if err != nil {
return http.StatusNotFound, err.Error()
}
return http.StatusOK, task
}
func updateTask(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
vars := mux.Vars(r)
task, err := c.TaskList().Get(vars["task"])
if err != nil {
return http.StatusNotFound, err.Error()
}
payload := unmarshal(r.Body, "script", w)
t := task.(Task)
t.Script = payload["script"]
c.TaskList().Update(t)
return http.StatusOK, nothing
}
func deleteTask(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
vars := mux.Vars(r)
task, err := c.TaskList().Get(vars["task"])
if err != nil {
return http.StatusNotFound, err.Error()
}
c.TaskList().Delete(task.ID())
return http.StatusOK, nothing
}
func listJobsForTask(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
vars := mux.Vars(r)
jobs := c.JobList().GetJobsWithTask(vars["task"])
return http.StatusOK, jobs
}
// Triggers
func listTriggers(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
return http.StatusOK, c.TriggerList().Dump()
}
func addTrigger(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
payload := unmarshal(r.Body, "name", w)
trigger := Trigger{Name: payload["name"]}
c.TriggerList().Append(trigger)
return http.StatusCreated, nothing
}
func getTrigger(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
vars := mux.Vars(r)
trigger, err := c.TriggerList().Get(vars["trigger"])
if err != nil {
return http.StatusNotFound, err.Error()
}
return http.StatusNotFound, trigger
}
func updateTrigger(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
vars := mux.Vars(r)
trigger, err := c.TriggerList().Get(vars["trigger"])
if err != nil {
return http.StatusNotFound, err.Error()
}
payload := unmarshal(r.Body, "cron", w)
t := trigger.(Trigger)
t.Schedule = payload["cron"]
c.Executor().ArmTrigger(t)
err = c.TriggerList().Update(t)
if err != nil {
return http.StatusInternalServerError, err.Error()
}
return http.StatusOK, nothing
}
func deleteTrigger(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
vars := mux.Vars(r)
c.TriggerList().Delete(vars["trigger"])
return http.StatusOK, nothing
}
func listJobsForTrigger(c context, w http.ResponseWriter, r *http.Request) (int, interface{}) {
vars := mux.Vars(r)
jobs := c.JobList().GetJobsWithTrigger(vars["trigger"])
return http.StatusOK, jobs
}