-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
63 lines (55 loc) · 1.44 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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
syslutil "github.com/anz-bank/new-sysl-playground/syslUtil"
"github.com/gorilla/mux"
"github.com/rs/cors"
)
type compileContent struct {
Code string
Command []string
}
func compile(w http.ResponseWriter, r *http.Request) {
var c compileContent
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("FATAL IO reader issue %s ", err.Error())
}
json.Unmarshal(body, &c)
args := c.Command
compileResult, err := syslutil.Execute(c.Code, args)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(compileResult); err != nil {
panic(err)
}
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "3030"
}
r := mux.NewRouter()
api := r.PathPrefix("/api/").Subrouter()
api.HandleFunc("/compile", compile).Methods(http.MethodPost)
api.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"message": "pong"}`))
}).Methods(http.MethodGet)
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
})
handler := c.Handler(r)
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./client")))
log.Printf("Server is running on: %s\n", port)
log.Fatal(http.ListenAndServe(":"+port, handler))
}