-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
51 lines (48 loc) · 1.3 KB
/
api.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
package server
import (
"encoding/json"
"net/http"
"strconv"
)
// makeError creates a map with a single key describing an error condition.
func makeError(desc string) interface{} {
return map[string]interface{}{
"error": desc,
}
}
// api provides methods that the UI can utilize.
func (s *Server) api(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
var response interface{}
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
switch r.Form.Get("action") {
case "getContainers":
response = s.configurator.Containers()
case "restartContainer":
id := r.Form.Get("id")
if len(id) == 0 {
response = makeError("container ID is missing")
} else if err := s.monitor.Restart(r.Context(), id); err != nil {
response = makeError(err.Error())
} else {
response = map[string]interface{}{}
}
default:
response = makeError("invalid action")
}
b, err := json.Marshal(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(b)
}