-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
47 lines (43 loc) · 880 Bytes
/
run.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
package main
import (
"encoding/json"
"net/http"
"time"
)
//Run : A instance when scribd was run
type Run struct {
ID string
Machinename string
Start time.Time
End time.Time
FilesCount int64
}
//Runlist : A list of runs
type Runlist struct {
Runs []Run
}
//RunHandler : Enables the creation and update of runs
func RunHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
runs := GetAllRuns()
js, err := json.Marshal(runs)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
var run Run
dec := json.NewDecoder(r.Body)
err := dec.Decode(&run)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
}
if r.Method == "POST" {
NewRun(run)
} else if r.Method == "PUT" {
EndRun(run)
} else {
http.Error(w, "", http.StatusMethodNotAllowed)
}
}