-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
56 lines (51 loc) · 1.42 KB
/
server.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
package main
import (
"fmt"
"github.com/gorilla/mux"
"html"
"log"
"net/http"
"os/exec"
)
func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", Index)
router.HandleFunc("/write", Write)
router.HandleFunc("/query", Query)
log.Fatal(http.ListenAndServe(":8080", router))
}
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}
func Write(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
out, err := exec.Command("go", "run", "utils.go", "write.go", "--data", r.URL.Query()["data"][0]).CombinedOutput()
if err == nil {
fmt.Fprintf(w, "1")
} else {
fmt.Fprintf(w, err.Error()+string(out))
}
}
func Query(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
params := r.URL.Query()
args := []string{"run", "comparison.go", "scanner.go", "parser.go", "token.go", "utils.go", "query.go"}
if query, ok := params["query"]; ok {
args = append(args, "--query", query[0])
} else {
fmt.Fprintf(w, "'query' param required")
return
}
if start, ok := params["start"]; ok {
args = append(args, "--start", start[0])
}
if end, ok := params["end"]; ok {
args = append(args, "--end", end[0])
}
out, err := exec.Command("go", args...).CombinedOutput()
if err == nil {
fmt.Fprintf(w, string(out))
} else {
fmt.Fprintf(w, err.Error()+string(out))
}
}