forked from shamanec/GADS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
293 lines (255 loc) · 10.4 KB
/
main.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"os/exec"
_ "GADS/docs"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
httpSwagger "github.com/swaggo/http-swagger"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
var ws_conn *websocket.Conn
var project_log_file *os.File
// Devices struct which contains
// an array of devices from the config.json
type Devices struct {
Devices []Device `json:"ios-devices-list"`
}
// Device struct which contains device info
type Device struct {
AppiumPort int `json:"appium_port"`
DeviceName string `json:"device_name"`
DeviceOSVersion string `json:"device_os_version"`
DeviceUDID string `json:"device_udid"`
WdaMjpegPort int `json:"wda_mjpeg_port"`
WdaPort int `json:"wda_port"`
}
// ProjectConfig struct which contains the project configuration values
type ProjectConfig struct {
DevicesHost string `json:"devices_host"`
SeleniumHubHost string `json:"selenium_hub_host"`
SeleniumHubPort string `json:"selenium_hub_port"`
SeleniumHubProtocolType string `json:"selenium_hub_protocol_type"`
WdaBundleID string `json:"wda_bundle_id"`
}
type ProjectConfigPageData struct {
WebDriverAgentProvided bool
SudoPasswordSet bool
UdevIOSListenerStatus string
ImageStatus string
ProjectConfigValues ProjectConfig
}
type ContainerRow struct {
ContainerID string
ImageName string
ContainerStatus string
ContainerPorts string
ContainerName string
DeviceUDID string
}
// Load the initial page
func GetInitialPage(w http.ResponseWriter, r *http.Request) {
var index = template.Must(template.ParseFiles("static/index.html"))
if err := index.Execute(w, nil); err != nil {
log.WithFields(log.Fields{
"event": "index_page_load",
}).Error("Couldn't load index.html")
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// Load the initial page with the project configuration info
func GetProjectConfigurationPage(w http.ResponseWriter, r *http.Request) {
jsonFile, err := os.Open("./configs/config.json")
if err != nil {
fmt.Println(err)
return
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var projectConfig ProjectConfig
json.Unmarshal(byteValue, &projectConfig)
var configRow = ProjectConfig{
DevicesHost: projectConfig.DevicesHost,
SeleniumHubHost: projectConfig.SeleniumHubHost,
SeleniumHubPort: projectConfig.SeleniumHubPort,
SeleniumHubProtocolType: projectConfig.SeleniumHubProtocolType,
WdaBundleID: projectConfig.WdaBundleID}
var index = template.Must(template.ParseFiles("static/project_config.html"))
pageData := ProjectConfigPageData{WebDriverAgentProvided: CheckWDAProvided(), SudoPasswordSet: CheckSudoPasswordSet(), UdevIOSListenerStatus: UdevIOSListenerState(), ImageStatus: ImageExists(), ProjectConfigValues: configRow}
if err := index.Execute(w, pageData); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// @Summary Update project configuration
// @Description Updates one or multiple configuration values
// @Tags configuration
// @Param config body ProjectConfig true "Update config"
// @Accept json
// @Produce json
// @Success 200 {object} SimpleResponseJSON
// @Failure 500 {object} ErrorJSON
// @Router /configuration/update-config [put]
func UpdateProjectConfigHandler(w http.ResponseWriter, r *http.Request) {
requestBody, _ := ioutil.ReadAll(r.Body)
devices_host := gjson.Get(string(requestBody), "devices_host").Str
selenium_hub_host := gjson.Get(string(requestBody), "selenium_hub_host").Str
selenium_hub_port := gjson.Get(string(requestBody), "selenium_hub_port").Str
selenium_hub_protocol_type := gjson.Get(string(requestBody), "selenium_hub_protocol_type").Str
wda_bundle_id := gjson.Get(string(requestBody), "wda_bundle_id").Str
// Open the configuration json file
jsonFile, err := os.Open("./configs/config.json")
if err != nil {
JSONError(w, "config_file_interaction", "Could not open the config.json file.", 500)
return
}
defer jsonFile.Close()
// Read the configuration json file into byte array
configJson, err := ioutil.ReadAll(jsonFile)
if err != nil {
JSONError(w, "config_file_interaction", "Could not read the config.json file.", 500)
return
}
var updatedJSON string
updatedJSON, _ = sjson.Set(string(configJson), "ios-devices-list.-1", devices_host)
if devices_host != "" {
updatedJSON, _ = sjson.Set(string(configJson), "devices_host", devices_host)
}
if selenium_hub_host != "" {
updatedJSON, _ = sjson.Set(string(configJson), "selenium_hub_host", selenium_hub_host)
}
if selenium_hub_port != "" {
updatedJSON, _ = sjson.Set(string(configJson), "selenium_hub_port", selenium_hub_port)
}
if selenium_hub_protocol_type != "" {
updatedJSON, _ = sjson.Set(string(configJson), "selenium_hub_protocol_type", selenium_hub_protocol_type)
}
if wda_bundle_id != "" {
updatedJSON, _ = sjson.Set(string(configJson), "wda_bundle_id", wda_bundle_id)
}
// Prettify the json so it looks good inside the file
var prettyJSON bytes.Buffer
json.Indent(&prettyJSON, []byte(updatedJSON), "", " ")
err = ioutil.WriteFile("./configs/config.json", []byte(prettyJSON.String()), 0644)
if err != nil {
JSONError(w, "config_file_interaction", "Could not write to the config.json file.", 500)
return
}
SimpleJSONResponse(w, "Successfully updated project config in ./configs/config.json", 200)
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func setLogging() {
log.SetFormatter(&log.JSONFormatter{})
project_log_file, err := os.OpenFile("./logs/project.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0755)
if err != nil {
panic(err)
}
log.SetOutput(project_log_file)
}
func GetLogsPage(w http.ResponseWriter, r *http.Request) {
var logs_page = template.Must(template.ParseFiles("static/project_logs.html"))
if err := logs_page.Execute(w, nil); err != nil {
log.WithFields(log.Fields{
"event": "project_logs_page",
}).Error("Couldn't load project_logs.html")
return
}
}
func GetDeviceControlPage(w http.ResponseWriter, r *http.Request) {
var device_control_page = template.Must(template.ParseFiles("static/device_control.html"))
if err := device_control_page.Execute(w, nil); err != nil {
log.WithFields(log.Fields{
"event": "device_control_page",
}).Error("Couldn't load device_control.html")
return
}
}
// @Summary Get project logs
// @Description Provides project logs as plain text response
// @Tags project-logs
// @Success 200
// @Failure 200
// @Router /project-logs [get]
func GetLogs(w http.ResponseWriter, r *http.Request) {
// Execute the command to restart the container by container ID
commandString := "tail -n 1000 ./logs/project.log"
cmd := exec.Command("bash", "-c", commandString)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.WithFields(log.Fields{
"event": "get_project_logs",
}).Error("Attempted to get project logs but no logs available.")
fmt.Fprintf(w, "No logs available")
return
}
//SimpleJSONResponse(w, "get_project_logs", out.String(), 200)
fmt.Fprintf(w, out.String())
}
type Order struct {
Username string `json:"username"`
Fullname string `json:"fullname"`
}
func handleRequests() {
// Create a new instance of the mux router
myRouter := mux.NewRouter().StrictSlash(true)
myRouter.PathPrefix("/swagger").Handler(httpSwagger.WrapHandler)
myRouter.PathPrefix("/swagger/").Handler(httpSwagger.Handler(
httpSwagger.URL("http://localhost:10000/swagger/doc.json"), //The url pointing to API definition
httpSwagger.DeepLinking(true),
httpSwagger.DocExpansion("none"),
httpSwagger.DomID("#swagger-ui"),
))
// iOS containers endpoints
// Android containers endpoints
// General containers endpoints
myRouter.HandleFunc("/containers/{container_id}/restart", RestartContainer).Methods("POST")
myRouter.HandleFunc("/containers/{container_id}/remove", RemoveContainer).Methods("POST")
myRouter.HandleFunc("/containers/{container_id}/logs", GetContainerLogs).Methods("GET")
myRouter.HandleFunc("/device-containers/{device_udid}/remove", RemoveDeviceContainer).Methods("POST")
myRouter.HandleFunc("/device-containers/{device_udid}/create", CreateDeviceContainer).Methods("POST")
// Configuration endpoints
myRouter.HandleFunc("/configuration/build-image/{image_type}", BuildDockerImage).Methods("POST")
myRouter.HandleFunc("/configuration/remove-image", RemoveDockerImage).Methods("POST")
myRouter.HandleFunc("/configuration/setup-ios-listener", SetupUdevListener).Methods("POST")
myRouter.HandleFunc("/configuration/remove-ios-listener", RemoveUdevListener).Methods("POST")
myRouter.HandleFunc("/configuration/update-config", UpdateProjectConfigHandler).Methods("PUT")
myRouter.HandleFunc("/configuration/set-sudo-password", SetSudoPassword).Methods("PUT")
myRouter.HandleFunc("/configuration/upload-wda", UploadWDA).Methods("POST")
myRouter.HandleFunc("/configuration/upload-app", UploadApp).Methods("POST")
// Devices endpoints
myRouter.HandleFunc("/device-logs/{log_type}/{device_udid}", GetDeviceLogs).Methods("GET")
myRouter.HandleFunc("/ios-devices", GetConnectedIOSDevices).Methods("GET")
myRouter.HandleFunc("/ios-devices/register", RegisterIOSDevice).Methods("POST")
myRouter.HandleFunc("/ios-devices/{device_udid}/install-app", InstallIOSApp).Methods("POST")
myRouter.HandleFunc("/ios-devices/{device_udid}/uninstall-app", UninstallIOSApp).Methods("POST")
myRouter.HandleFunc("/devices/device-control", GetDeviceControlInfo).Methods("GET")
// Logs
myRouter.HandleFunc("/project-logs", GetLogs).Methods("GET")
// Asset endpoints
myRouter.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
myRouter.PathPrefix("/main/").Handler(http.StripPrefix("/main/", http.FileServer(http.Dir("./"))))
// Page loads
myRouter.HandleFunc("/configuration.html", GetProjectConfigurationPage)
myRouter.HandleFunc("/android-containers.html", GetAndroidContainers)
myRouter.HandleFunc("/ios-containers.html", GetIOSContainers)
myRouter.HandleFunc("/project-logs.html", GetLogsPage)
myRouter.HandleFunc("/device-control.html", GetDeviceControlPage)
myRouter.HandleFunc("/", GetInitialPage)
log.Fatal(http.ListenAndServe(":10000", myRouter))
}
func main() {
setLogging()
handleRequests()
}