-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
254 lines (206 loc) · 5.29 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
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"net/http"
"github.com/gorilla/mux"
"encoding/json"
)
type CodeStruct struct {
Code string `json:"code"`
Input string `json:"input"`
}
func createCompilerBinaries() {
// creates the executable files for the compiler
cmd := exec.Command("make")
cmd.Dir = "./compiler"
err := cmd.Run()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
}
func execCompiler(code string, input string) string {
// fmt.Println(code)
// fmt.Println(input)
// write code to testcase.txt
file_c, err := os.Create("compiler/testcase.txt")
if err != nil {
fmt.Println(err)
} else {
file_c.WriteString(code)
}
file_c.Close()
// write input to input.txt
file_i, err := os.Create("compiler/input.txt")
if err != nil {
fmt.Println(err)
} else {
file_i.WriteString(input)
}
file_i.Close()
// fmt.Println("Written to files")
outfile, _ := os.Create("./compiler/output.txt")
outfile.Sync()
defer outfile.Close()
// compile the code and check for errors
cmd := exec.Command("./compiler", "testcase.txt", "code.asm")
cmd.Stdout = outfile
cmd.Stderr = os.Stderr
cmd.Dir = "./compiler"
err = cmd.Run()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
outfile.Close()
output, err := ioutil.ReadFile("./compiler/output.txt")
if err != nil {
log.Fatal(err)
}
// check compiled output for errors
// Success is stored in case on no error
if string(output) == "Success" {
fmt.Println("Code compilation successful")
} else {
// in case of errors return the error stream stored in output.txt
// fmt.Println(string(output))
return string(output)
}
// creates the executable file from asm code
cmd = exec.Command("make", "executable")
cmd.Dir = "./compiler"
err = cmd.Run()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
outfile, _ = os.Create("./compiler/output.txt")
outfile.Sync()
defer outfile.Close()
bytes, err := ioutil.ReadFile("./compiler/input.txt")
if err != nil {
log.Fatal(err)
}
// get input for executable from input.txt
cmd = exec.Command("./exe", "< input.txt")
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
cmd.Stdout = outfile
cmd.Stderr = os.Stderr
cmd.Dir = "./compiler"
err = cmd.Start()
if err != nil {
log.Fatalf("cmd.Start() failed with %s\n", err)
}
_, err = io.WriteString(stdin, string(bytes))
stdin.Close()
if err != nil {
log.Fatal(err)
}
cmd.Wait()
// store output in output.txt and return that output as a string
output, err = ioutil.ReadFile("./compiler/output.txt")
if err != nil {
log.Fatal(err)
}
return string(output)
}
// function to create new router
func newRouter() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/", serveFiles).Methods("GET")
r.HandleFunc("/compile", handleCompiler).Methods("POST")
r.HandleFunc("/getCode", getData).Methods("POST")
// set static directory
staticFileDirectory := http.Dir("./static/")
staticFileHandler := http.StripPrefix("/static/", http.FileServer(staticFileDirectory))
r.PathPrefix("/static/").Handler(staticFileHandler).Methods("GET")
return r
}
// handler to serve files
func serveFiles(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Path, r.Method)
path := "./static" + r.URL.Path
if r.URL.Path == "/" {
path = path + "index.html"
}
http.ServeFile(w, r, path)
}
// handler to execute the compiler code
func handleCompiler(w http.ResponseWriter, r *http.Request) {
// parse data
err := r.ParseForm()
// In case of any error, report
if err != nil {
fmt.Println(fmt.Errorf("Error: %v", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
// read code and input
code := r.Form.Get("code")
input := r.Form.Get("input_values")
// get the output
output := execCompiler(code, input)
// fmt.Println(output)
// return output as json
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(output); err != nil {
panic(err)
}
}
func getData(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
// In case of any error, report
if err != nil {
fmt.Println(fmt.Errorf("Error: %v", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
id := r.Form.Get("id")
// fmt.Println(id)
flag := 0
code_path := "./compiler/sample_code/c" + id + ".txt"
input_path := "./compiler/sample_code/i" + id + ".txt"
code, err := ioutil.ReadFile(code_path)
if err != nil {
flag = 1
}
input, err := ioutil.ReadFile(input_path)
if err != nil {
flag = 1
}
if flag == 1 {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Something bad happened!"))
}
resp, err := json.Marshal(CodeStruct{Code: string(code), Input: string(input)})
if err != nil {
log.Fatal(err)
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, string(resp))
}
func getPort() string {
p := os.Getenv("PORT")
if p != "" {
return ":" + p
}
return ":3000"
}
func main() {
fmt.Println("Creating compiler Binaries")
// createCompilerBinaries()
// create new router to handle requests
fmt.Println("Creating router")
r := newRouter()
fmt.Println("Getting Port")
port := getPort()
fmt.Printf("Listening on %s\n", port)
log.Fatal(http.ListenAndServe(port, r))
}