-
Notifications
You must be signed in to change notification settings - Fork 53
/
api.go
204 lines (169 loc) · 3.96 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
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
package main
import (
"encoding/json"
"fmt"
"net"
"net/http"
"os"
)
type API struct {
done chan bool
daemon *Daemon
}
func extractUser(r *http.Request) (*User, error) {
userHeader, ok := r.Header["X-Username"]
if !ok {
return nil, fmt.Errorf("Missing X-Username header")
}
return lookupUser(userHeader[0])
}
func (a *API) start(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("Method not allowed"))
return
}
if a.daemon.VM != nil {
w.WriteHeader(http.StatusConflict)
w.Write([]byte("Conflict"))
return
}
user, err := extractUser(r)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
return
}
a.daemon.VM, err = NewVM(user)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
err = a.daemon.VM.Start()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
// w.Write([]byte(fmt.Sprintf("Virtual machine started, tty available at %s", a.daemon.VM.TTY)))
}
func (a *API) started(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("Method not allowed"))
return
}
if a.daemon.VM == nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Virtual machine has not been started"))
return
}
a.daemon.VM.Started = true
w.Write([]byte("Virtual machine flagged as started"))
}
func (a *API) stop(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("Method not allowed"))
return
}
if a.daemon.VM == nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Virtual machine is not running"))
return
}
a.daemon.VM.Started = false
a.daemon.VM.Ready = false
a.daemon.VM.Stop()
a.daemon.VM = nil
w.WriteHeader(http.StatusOK)
// w.Write([]byte("Virtual machine shut down"))
}
func (a *API) status(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if a.daemon.VM == nil {
user, err := extractUser(r)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("\"message\": \"Unauthorized\""))
return
}
status, err := EmptyStatus(*user)
if err != nil {
statusErr := VMStatusError{
Status: "error",
Message: err.Error(),
}
js, _ := json.Marshal(statusErr)
w.WriteHeader(http.StatusInternalServerError)
w.Write(js)
return
}
js, _ := json.Marshal(status)
w.WriteHeader(http.StatusOK)
w.Write(js)
return
}
status, err := a.daemon.VM.Status()
if err != nil {
statusErr := VMStatusError{
Status: "error",
Message: err.Error(),
}
js, _ := json.Marshal(statusErr)
w.WriteHeader(http.StatusInternalServerError)
w.Write(js)
return
}
js, err := json.Marshal(status)
if err != nil {
statusErr := VMStatusError{
Status: "error",
Message: err.Error(),
}
js, _ := json.Marshal(statusErr)
w.WriteHeader(http.StatusInternalServerError)
w.Write(js)
return
}
w.WriteHeader(http.StatusOK)
w.Write(js)
}
func (a *API) Listen() error {
addr, err := net.ResolveTCPAddr("tcp", "0.0.0.0:1050")
if err != nil {
return err
}
raw, err := net.ListenTCP("tcp", addr)
if err != nil {
return err
}
listener := &tcpListener{
TCPListener: raw,
done: a.done,
}
mux := http.NewServeMux()
mux.HandleFunc("/start", a.start)
mux.HandleFunc("/started", a.started)
mux.HandleFunc("/stop", a.stop)
mux.HandleFunc("/status", a.status)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
loggedMux := newLoggedHandler(mux, os.Stdout)
server := &http.Server{
Handler: loggedMux,
}
return server.Serve(listener)
}
func (a *API) Stop() {
a.done <- true
}
func NewAPI(daemon *Daemon) *API {
return &API{
daemon: daemon,
done: make(chan bool),
}
}