-
Notifications
You must be signed in to change notification settings - Fork 39
/
handler.go
219 lines (191 loc) · 5.46 KB
/
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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
func destroyHandler(w http.ResponseWriter, req *http.Request) {
key := mux.Vars(req)["id"]
log.Printf("destroy %s unit", key)
if err := fleetClient.Destroy(key); err != nil {
// log.Printf("unit destroy error: %s", err)
// renderer.JSON(w, http.StatusBadRequest, err)
// return
}
renderer.JSON(w, http.StatusOK, map[string]string{"result": "success"})
}
func startHandler(w http.ResponseWriter, req *http.Request) {
key := mux.Vars(req)["id"]
log.Printf("start unit %s", key)
if err := fleetClient.Start(key); err != nil {
// log.Printf("unit start error: %s", err)
// renderer.JSON(w, http.StatusBadRequest, err)
// return
}
renderer.JSON(w, http.StatusOK, map[string]string{"result": "success"})
}
func stopHandler(w http.ResponseWriter, req *http.Request) {
key := mux.Vars(req)["id"]
log.Printf("stop %s unit", key)
if err := fleetClient.Stop(key); err != nil {
// log.Printf("unit stop error: %s", err)
// renderer.JSON(w, http.StatusBadRequest, err)
// return
}
renderer.JSON(w, http.StatusOK, map[string]string{"result": "success"})
}
func loadHandler(w http.ResponseWriter, req *http.Request) {
key := mux.Vars(req)["id"]
log.Printf("load unit - %s", key)
if err := fleetClient.Load(key); err != nil {
// log.Printf("unit load error: %s", err)
// renderer.JSON(w, http.StatusBadRequest, err)
// return
}
renderer.JSON(w, http.StatusOK, map[string]string{"result": "success"})
}
func uploadUnitHandler(w http.ResponseWriter, req *http.Request) {
log.Println("upload unit")
if _, err := os.Stat(tempDir); err != nil {
log.Printf("err: %s", err)
} else if os.IsNotExist(err) {
os.Mkdir(tempDir, 0755)
}
file, header, err := req.FormFile("file")
if err != nil {
log.Printf("read file error : %s", err)
renderer.JSON(w, http.StatusBadRequest, err)
return
}
defer file.Close()
serviceFile := fmt.Sprintf("%s/%s", tempDir, header.Filename)
out, err := os.Create(serviceFile)
if err != nil {
log.Printf("Open file errpr: %s", err)
renderer.JSON(w, http.StatusBadRequest, err)
return
}
defer out.Close()
// write the content from POST to the file
_, err = io.Copy(out, file)
if err != nil {
fmt.Fprintln(w, err)
}
err = fleetClient.Submit(header.Filename, serviceFile)
if err != nil {
// log.Printf("Fleet submit error: %s", err)
// renderer.JSON(w, http.StatusBadRequest, err)
// return
}
if err := fleetClient.Start(header.Filename); err != nil {
// log.Printf("unit start error: %s", err)
// renderer.JSON(w, http.StatusBadRequest, err)
// return
}
renderer.JSON(w, http.StatusOK, map[string]string{"result": "success"})
}
func submitUnitHandler(w http.ResponseWriter, req *http.Request) {
log.Println("submit unit")
type UnitForm struct {
Name string `json:"name"`
Service string `json:"service"`
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("read request body error : %s", err)
renderer.JSON(w, http.StatusBadRequest, err)
return
}
var t UnitForm
dec := json.NewDecoder(strings.NewReader(string(body)))
dec.Decode(&t)
if err != nil {
log.Printf("json decode error : %s", err)
}
if _, err := os.Stat(tempDir); err != nil {
log.Printf("err: %s", err)
} else if os.IsNotExist(err) {
os.Mkdir(tempDir, 0755)
}
serviceFile := fmt.Sprintf("%s/%s", tempDir, t.Name)
lines := strings.Split(string(t.Service), "\\n")
fo, err := os.Create(serviceFile)
if err != nil {
log.Printf("Open file errpr: %s", err)
renderer.JSON(w, http.StatusBadRequest, err)
return
}
defer func() {
if err := fo.Close(); err != nil {
panic(err)
}
}()
for _, str := range lines {
fmt.Fprintln(fo, str)
}
err = fleetClient.Submit(t.Name, serviceFile)
if err != nil {
// log.Printf("Fleet submit error: %s", err)
// renderer.JSON(w, http.StatusBadRequest, err)
// return
}
if err := fleetClient.Start(t.Name); err != nil {
// log.Printf("unit start error: %s", err)
// renderer.JSON(w, http.StatusBadRequest, err)
// return
}
renderer.JSON(w, http.StatusOK, map[string]string{"result": "success"})
}
func machineAllHandler(w http.ResponseWriter, req *http.Request) {
log.Println("machine all")
status, _ := fleetClient.MachineAll()
renderer.JSON(w, http.StatusOK, status)
}
func statusAllHandler(w http.ResponseWriter, req *http.Request) {
log.Println("unit all")
status, _ := fleetClient.StatusAll()
renderer.JSON(w, http.StatusOK, status)
}
func statusHandler(w http.ResponseWriter, req *http.Request) {
key := mux.Vars(req)["id"]
log.Printf("unit detail - %s", key)
status, _ := fleetClient.StatusUnit(key)
renderer.JSON(w, http.StatusOK, status)
}
// websocket handler
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool { return true },
}
func wsHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
// write journal message
key := mux.Vars(r)["id"]
stdout, errout, err := fleetClient.JournalF(key)
if err != nil {
conn.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("Error: %v", err)))
return
}
go func() {
for line := range stdout {
conn.WriteMessage(websocket.TextMessage, []byte(line))
}
}()
go func() {
for line := range errout {
conn.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("Stderr: %s", line)))
}
}()
}