/
handlers.go
140 lines (105 loc) · 2.84 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
package main
import (
"encoding/json"
"github.com/codegangsta/negroni"
"net/http"
"strings"
"time"
)
const rootFolder = "/build"
type ExecInstruction struct {
Id string
Command string
Delay time.Duration
}
// api/exec
func execHandler(w http.ResponseWriter, req *http.Request) {
JsonHeaders(w, req)
if req.Method == "POST" {
v := ExecInstruction{
Delay: 1,
}
err := decodeBody(req, &v)
if err != nil {
respondWithJson(w, http.StatusNotAcceptable, err.Error())
return
}
v.Delay = v.Delay * time.Second
_, err = exec_async(v.Id, v.Delay)
if err != nil {
respondWithJson(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJson(w, http.StatusOK, nil)
} else {
respondWithJson(w, http.StatusMethodNotAllowed, "method not allowed")
}
}
func stopHandler(w http.ResponseWriter, req *http.Request) {
JsonHeaders(w, req)
if req.Method == "POST" {
v := ExecInstruction{}
err := decodeBody(req, &v)
if err != nil {
respondWithJson(w, http.StatusNotAcceptable, err.Error())
return
}
err = stop(v.Id)
if err != nil {
respondWithJson(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJson(w, http.StatusOK, nil)
} else {
respondWithJson(w, http.StatusMethodNotAllowed, "method not allowed")
}
}
func curStatHandler(w http.ResponseWriter, req *http.Request) {
JsonHeaders(w, req)
if req.Method == "GET" {
project.ColectResults()
respondWithJson(w, http.StatusOK, project.LastResults)
} else {
respondWithJson(w, http.StatusMethodNotAllowed, "method not allowed")
}
}
func decodeBody(req *http.Request, v interface{}) error {
defer req.Body.Close()
dec := json.NewDecoder(req.Body)
err := dec.Decode(v)
return err
}
func JsonHeaders(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE")
w.Header().Set("Access-Control-Allow-Headers", strings.Join(r.Header["Access-Control-Request-Headers"], ","))
}
func respondWithJson(w http.ResponseWriter, code int, v interface{}) {
b := []byte(`{"status": "ok"}`)
var err error
if v != nil {
b, err = json.MarshalIndent(v, "", " ")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
}
w.WriteHeader(code)
w.Write(b)
}
func runWeb(host string) {
mux := http.NewServeMux()
mux.HandleFunc(rootFolder+"/api/exec", execHandler)
mux.HandleFunc(rootFolder+"/api/status/current", curStatHandler)
mux.HandleFunc(rootFolder+"/api/stop", stopHandler)
mux.HandleFunc(rootFolder+"/api/flow", func(w http.ResponseWriter, req *http.Request) {
JsonHeaders(w, req)
w.Write(project.ToJson())
})
n := negroni.Classic()
// n := negroni.New()
n.UseHandler(mux)
n.Run(host)
}