-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
285 lines (236 loc) · 7.78 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
package main
import (
_ "github.com/go-sql-driver/mysql"
"github.com/tiklup11/go-hello-world/service"
)
func main() {
service.RunEgoServer()
}
// utils.DeleteFolder("./../go_transformations/test.zip/")
// func RunHandler(w http.ResponseWriter, r *http.Request) {
// fmt.Print("POST request recieved at /run \n")
// fmt.Print("processing request.. \n")
// // Check if the request method is POST
// if r.Method != "POST" {
// http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
// fmt.Print(w, "Method not allowed", http.StatusMethodNotAllowed)
// return
// }
// // Parse the multipart form
// err := r.ParseMultipartForm(32 << 20) // Max file size 32MB
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// fmt.Print(w, err.Error(), http.StatusInternalServerError)
// return
// }
// // Get the ZIP file from the form
// file, handler, err := r.FormFile("file")
// if err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
// fmt.Print(w, err.Error(), http.StatusBadRequest)
// return
// }
// defer file.Close()
// // Create a temporary directory to extract the files from the ZIP
// tmpDir, err := os.MkdirTemp("", "zip-extract")
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// fmt.Print(w, err.Error(), http.StatusInternalServerError)
// return
// }
// defer os.RemoveAll(tmpDir)
// // Extract the files from the ZIP to the temporary directory
// zipReader, err := zip.NewReader(file, handler.Size)
// if err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
// fmt.Print(w, err.Error(), http.StatusBadRequest)
// return
// }
// for _, f := range zipReader.File {
// fpath := filepath.Join(tmpDir, f.Name)
// if !strings.HasPrefix(fpath, filepath.Clean(tmpDir)+string(os.PathSeparator)) {
// http.Error(w, "Invalid ZIP file", http.StatusBadRequest)
// return
// }
// if f.FileInfo().IsDir() {
// os.MkdirAll(fpath, os.ModePerm)
// continue
// }
// err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm)
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// defer outFile.Close()
// inFile, err := f.Open()
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// defer inFile.Close()
// _, err = io.Copy(outFile, inFile)
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// }
// // Save the extracted files to the server directory
// destDir := "../go_tranformations"
// err = os.MkdirAll(destDir, os.ModePerm)
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// err = os.Rename(tmpDir, filepath.Join(destDir, handler.Filename))
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// // Return a success message
// output := generateScriptAndRun()
// fmt.Print(output)
// fmt.Fprintf(w, output)
// }
// func handleIndex(w http.ResponseWriter, r *http.Request) {
// html := `<html>
// <body>
// <form action="/run" method="post" enctype="multipart/form-data">
// <input type="file" name="file"><br><br>
// <input type="submit" value="Run">
// </form>
// </body>
// </html>`
// fmt.Fprint(w, html)
// }
// func changeDirTo(path string) {
// // Change the working directory to the specified path
// err := os.Chdir(path)
// fmt.Print("changing directory to tranformation_code :", path)
// fmt.Print("\n")
// if err != nil {
// fmt.Print(err)
// }
// }
// func testScript() string {
// go_tranformation_path := "../go_tranformations/test.zip"
// changeDirTo(go_tranformation_path)
// // Run the script and capture the output
// out := runScript("./myscript.sh")
// return string(out)
// }
// func runScript(path string) string {
// cmd, err := exec.Command("/bin/sh", path).Output()
// if err != nil {
// fmt.Printf("error %s", err)
// }
// output := string(cmd)
// return output
// }
// func generateScriptAndRun() string {
// go_tranformation_path := "../go_tranformations/test.zip"
// changeDirTo(go_tranformation_path)
// // Define the script content as a string
// script := generateScript()
// // Write the script to a file
// err := os.WriteFile("myscript.sh", []byte(script), 0755)
// if err != nil {
// fmt.Println("Error writing script:", err)
// return "error writing script"
// }
// // Make the script executable
// cmd := exec.Command("chmod", "+x", "myscript.sh")
// err = cmd.Run()
// if err != nil {
// fmt.Println("Error making script executable:", err)
// return "error making script executable"
// }
// // Run the script and capture the output
// out := runScript("./myscript.sh")
// // Print the output
// fmt.Println(string(out))
// deleteTempFolder("../go_tranformations/test.zip")
// return string(out)
// }
// func generateScript() string {
// script := `
// #!/bin/bash
// echo "Starting script to run go-tranformation.."
// echo >> "running : [go mod tidy] to load dependencies of tranformation"
// go mod tidy
// echo >> "running : [ego-go build main.go]
// ego-go build main.go
// ego sign main
// echo >> "running : [modifying enclave.json config file] to include datafiles"
// cp ../../ego_server/enclave.json ./ -f
// ego sign main
// echo >> "running : [OE_SIMULATION=1 ego run main] to set mode to SIMULATION"
// OE_SIMULATION=1 ego run main
// `
// return script
// }
// func deleteTempFolder(tempFolderPath string) {
// // testFolderPath := "../go_tranformations/test.zip"
// err := os.RemoveAll(tempFolderPath)
// if err != nil {
// fmt.Errorf("not able to remove %s", tempFolderPath)
// }
// }
// // ------------------- CODE NOT IN USE-----------------------
// func runCommands(commands []*exec.Cmd, dir string) (string, error) {
// // Change the working directory to the specified path
// err := os.Chdir(dir)
// fmt.Print("changing directory to tranformation_code :", dir)
// fmt.Print("\n")
// if err != nil {
// return "", err
// }
// setEnvVariable()
// // Execute the commands in the specified directory
// var output string
// for _, cmd := range commands {
// fmt.Print("running : ")
// fmt.Print(cmd.String())
// fmt.Print("\n\n")
// out, err := cmd.Output()
// if err != nil {
// return "", err
// }
// // if i == len(commands)-1 {
// output += string(out)
// // }
// }
// return output, nil
// }
// func setEnvVariable() {
// // Get the current environment variables
// env := os.Environ()
// // Add a new environment variable to the slice
// env = append(env, "OE_SIMULATION=1")
// fmt.Println("Simulation mode on, OE_SIMULATION set to true")
// }
// func runTranformationGoCode() string {
// fmt.Print("running tranformation..\n")
// dir := "../go_tranformations/test.zip"
// cmd_tidy := exec.Command("go", "mod", "tidy")
// cmd_build := exec.Command("ego-go", "build", "main.go")
// cmd_sign := exec.Command("ego", "sign", "main")
// // cmd_source_bashrc := exec.Command("source", "~/.bashrc")
// cmd_generate_enclave := exec.Command("cp", "../../ego_server/enclave.json", "./", "-f")
// // cmd_set_to_simulation := exec.Command("/bin/sh", "-c", "export OE_SIMULATION=1")
// cmd_run := exec.Command("ego", "run", "main")
// commands := []*exec.Cmd{cmd_tidy, cmd_build, cmd_sign, cmd_generate_enclave, cmd_run}
// output, err := runCommands(commands, dir)
// if err != nil {
// fmt.Println("Error running commands:", err)
// return ""
// }
// fmt.Println()
// fmt.Println("Output:", output)
// // deleteTempFolder("../go_tranformations/test.zip")
// return output
// }