-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_handler.go
75 lines (57 loc) · 1.41 KB
/
http_handler.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
package main
import (
"io/ioutil"
"strings"
"fmt"
"net/http"
"log"
)
func handler(w http.ResponseWriter, r *http.Request) {
var cmd string
if r.Method == http.MethodGet {
cmd = r.URL.Query().Get("cmd")
} else if r.Method == http.MethodPost {
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
log.Println("invalid request")
http.Error(w, "invalid request", http.StatusUnauthorized)
return
}
cmd = string(body)
} else {
log.Println("unsupported http method: ", r.Method)
http.Error(w, "unsupported http method: " + r.Method, http.StatusUnauthorized)
return
}
log.Println("received cmd: " + cmd)
args := strings.Fields(cmd)
ok, reason, name := authorized(args);
if !ok {
log.Println("unauthorized:", reason)
http.Error(w, "unauthorized: " + reason, http.StatusUnauthorized)
return
}
if args[1] == "run" {
if name == "" {
log.Println("missing container name, noop")
http.Error(w, "missing container name", http.StatusBadRequest)
return
}
saveStartedContainers(name)
log.Println("starting container: ", name)
} else if args[1] == "load" {
args[3] = imgHome + "/" + args[3]
} else {
// noop
}
exitcode, output := execute(args)
w.Header().Set("DOCKER_EXIT_CODE", exitcode)
if exitcode != "0" {
http.Error(w, output, http.StatusBadRequest)
} else {
log.Println("started container:", name)
fmt.Println(w, output)
}
return
}