-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.go
More file actions
277 lines (228 loc) · 6.11 KB
/
backend.go
File metadata and controls
277 lines (228 loc) · 6.11 KB
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
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
const internalUrl = "http://localhost:4000"
const url = "http://iomics.ugent.be/msplayground" //internalUrl
const name = "/prog.go"
const mypath = "C:\\Users\\compomics\\msplayground" //"/home/pieter/compomics/msplayground/"
const sel_ldr = mypath + "/nacl_sdk/pepper_34/tools/sel_ldr_x86_64"
type limitedCmd struct {
*exec.Cmd
}
type RunResponse struct {
Errors string
Events []Event
}
type Event struct {
Message string
Delay int
}
type prog struct {
Directory string
RunResponse RunResponse
Downloads map[string]string
}
//mapping of source strings to executables and output
var progs map[string]prog = make(map[string]prog)
func cache(code string, p prog) {
progs[code] = p
}
func getCached(code string) (p prog) {
if _, ok := progs[code]; !ok {
p = makeProg(code)
progs[code] = p
}
return progs[code]
}
func makeProg(code string) prog {
dir, err := ioutil.TempDir("", strings.TrimSuffix(strings.TrimPrefix(name, "/"), ".go"))
if err != nil {
log.Println("error creating temporary directory:", err)
}
ioutil.WriteFile(dir+name, []byte(code), 0644)
return prog{dir, RunResponse{}, nil}
}
func (cmd *limitedCmd) killAfter(t time.Duration) (err error) {
done := make(chan error)
go func() {
done <- cmd.Wait()
}()
select {
case <-time.After(t):
if err = cmd.Process.Kill(); err != nil {
log.Fatal("failed to kill: ", err)
}
<-done // allow goroutine to exit
log.Println("process killed")
case err = <-done:
}
return err
}
func isServed(s string) bool {
response, err := http.Get(s)
if err != nil {
log.Println(err)
return false
}
defer response.Body.Close()
text, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Println(err)
return false
}
return !strings.Contains(string(text), "404 page not found")
}
//compile the source for multiple platforms and architectures
//if successful, serve the working folder on http
//otherwise return with an empty string map so nothing gets back to the client
func (p *prog) compileandserve() {
ds := make(map[string]string, 6)
errs := make([]error, 6)
ds["32-bit Linux"], errs[0] = p.compile("linux", "386")
ds["64-bit Linux"], errs[1] = p.compile("linux", "amd64")
ds["32-bit Windows"], errs[2] = p.compile("windows", "386")
ds["64-bit Windows"], errs[3] = p.compile("windows", "amd64")
ds["32-bit MacOS X"], errs[4] = p.compile("darwin", "386")
ds["64-bit MacOS X"], errs[5] = p.compile("darwin", "amd64")
for _, err := range errs {
if err != nil {
p.Downloads = make(map[string]string, 1)
p.Downloads["error: "+err.Error()] = ""
return
}
}
fileBasePath := "/" + filepath.Base(p.Directory) + "/"
for k := range ds {
ds[k] = url + fileBasePath + filepath.Base(ds[k])
}
p.Downloads = ds
if !isServed(url+fileBasePath) {
log.Println("serving", p.Directory)
http.Handle(fileBasePath, http.StripPrefix(fileBasePath, http.FileServer(http.Dir(p.Directory))))
}
}
//compile cross-platform, in the same directory as the source,
//preconditions are a GOROOT and GOPATH that can do this
func (p *prog) compile(goos string, goarch string) (string, error) {
oldgoarch := os.Getenv("GOARCH")
oldgoos := os.Getenv("GOOS")
os.Setenv("GOARCH", goarch)
os.Setenv("GOOS", goos)
defer os.Setenv("GOARCH", oldgoarch)
defer os.Setenv("GOOS", oldgoos)
ifile := p.Directory + name
ofile := strings.TrimSuffix(ifile, ".go") + "-" + goos + "-" + goarch
if goos == "windows" {
ofile += ".exe"
}
cmd := limitedCmd{exec.Command("go", "build", "-o", ofile, ifile)}
//only capture stderr, a successful build has no output
var b bytes.Buffer
cmd.Stderr = &b
err := cmd.Start()
if err != nil {
log.Println(err)
}
cmd.killAfter(5 * time.Second)
if b.String() != "" {
err = errors.New(b.String())
}
return ofile, err
}
//compile and run prog.go with ./sel_ldr_x86_64
func (p *prog) run() {
oldgopath := os.Getenv("GOPATH")
os.Setenv("GOPATH", mypath)
defer os.Setenv("GOPATH", oldgopath)
bin, err := p.compile("nacl", "amd64p32")
if err != nil {
p.RunResponse = RunResponse{
Errors: err.Error(),
Events: []Event{Event{Message: "", Delay: 0}},
}
return
}
cmd := limitedCmd{exec.Command(sel_ldr, bin)}
log.Println("running", p.Directory)
//only capture stdout, as the Go output streams are merged in NaCl
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Start()
if err != nil {
log.Println(err)
}
cmd.killAfter(5 * time.Second)
p.RunResponse = RunResponse{
Errors: "",
Events: []Event{Event{Message: out.String(), Delay: 0}},
}
}
//handle post requests that contain source code. cache the code, check for
//preexisting output, run, and marshal stdout in json as response
var run = func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
code := r.FormValue("body")
p := getCached(code)
if p.RunResponse.Events == nil {
p.run()
cache(code, p)
}
b, err := json.Marshal(p.RunResponse)
if err != nil {
log.Println(err)
}
w.Write(b)
default:
fmt.Fprint(w, "I only answer to POST requests.")
}
}
//handle post requests that contain source code, cache the code, check for
//preexisting binaries, compile, serve all we have
//and marshal the download links in json as response
var download = func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
code := r.FormValue("body")
p := getCached(code)
if p.Downloads == nil {
p.compileandserve()
cache(code, p)
}
b, err := json.Marshal(p.Downloads)
if err != nil {
log.Println(err)
}
w.Write(b)
default:
fmt.Fprint(w, "I only answer to POST requests.")
}
}
//start up the http server and clean up the created folders when yser quits
func main() {
http.HandleFunc("/run", run)
http.HandleFunc("/download", download)
go http.ListenAndServe(strings.TrimPrefix(internalUrl, "http://"), nil)
fmt.Println("Terminate with Ctrl+D to clean up temporary directories.")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
//wait for the user to EOF
}
//cleanup
for _, p := range progs {
os.RemoveAll(p.Directory)
}
}