-
Notifications
You must be signed in to change notification settings - Fork 11
/
compiler.go
71 lines (60 loc) · 1.49 KB
/
compiler.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
package main
import (
"io/ioutil"
"net/http"
"regexp"
"fmt"
"os/exec"
"os"
"time"
)
type Script struct {
Title string
Body []byte
}
/*
func loadPage(title string) (*Page, error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Page{Title: title, Body: body}, nil
}*/
func compileHandler(w http.ResponseWriter, r *http.Request, title string) {
fmt.Printf("Received %s\n", title)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
s := &Script{Title: title, Body: []byte(body)}
filename := s.Title + ".mq4";
err = ioutil.WriteFile("MQL4/Experts/" + filename, s.Body, 0600)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
exec.Command("./compile.sh", filename).Run()
file, err := os.Open("MQL4/Experts/" + s.Title + ".ex4")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.ServeContent(w, r, "result.ex4", time.Now(), file)
}
var validPath = regexp.MustCompile("^/(compile)/([a-zA-Z0-9]+)$")
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
m := validPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.NotFound(w, r)
return
}
fn(w, r, m[2])
}
}
func main() {
http.HandleFunc("/compile/", makeHandler(compileHandler))
http.ListenAndServe(":8080", nil)
}