forked from coduno/compute
/
main.go
139 lines (115 loc) · 3.45 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"os/user"
"path"
"strings"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/cloud"
"google.golang.org/cloud/datastore"
"github.com/coduno/compute/runner"
"github.com/coduno/engine/cloud/model"
)
var client *http.Client
func init() {
user, err := user.Current()
if err != nil {
panic(err)
}
fileName := path.Join(user.HomeDir, "config", "secret.json")
secret, err := ioutil.ReadFile(fileName)
if err != nil {
panic(err)
}
config, err := google.JWTConfigFromJSON(secret, datastore.ScopeDatastore, datastore.ScopeUserEmail)
if err != nil {
panic(err)
}
client = config.Client(oauth2.NoContext)
}
func NewContext() context.Context {
return cloud.NewContext("coduno", client)
}
func main() {
http.HandleFunc("/", startHandler)
http.ListenAndServe(":8081", nil)
}
func startHandler(w http.ResponseWriter, req *http.Request) {
cors(w, req)
encodedKey := req.URL.Path[1:]
// TODO(victorbalan): Remove this after we can connect with the engine to localhost.
// Untill then leave it so we can get entity keys to query for.
// q := datastore.NewQuery(model.ChallengeKind).Filter("Runner =", "outputtest")
// var challenges []model.Challenge
// t, _ := q.GetAll(ctx, &challenges)
// fmt.Println(t[0])
// fmt.Println(t[0].Encode())
// return
// cKey = t[0].Encode()
key, err := datastore.DecodeKey(encodedKey)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var challenge model.Challenge
err = datastore.Get(NewContext(), key, &challenge)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
f := flag.NewFlagSet("challengeFlags", flag.ContinueOnError)
tests := f.String("tests", "", "Defines the tests path")
// TODO(victorbalan): Enable the image flage when we will use it
// image := f.String("image", "", "Defines a custom image")
flags := strings.Split(challenge.Flags, " ")
if len(flags) > 0 {
if err := f.Parse(flags); err != nil {
fmt.Printf(err.Error())
}
}
var rh runner.RunHandler
switch challenge.Runner {
case "simple":
rh = runner.SimpleRunHandler{}
case "javut":
rh = runner.JavaUnitTestHandler{}
case "outputtest":
if *tests == "" {
http.Error(w, "There is no test path provided", http.StatusInternalServerError)
return
}
rh = runner.OutputTestHandler{TestFilePath: *tests}
default:
http.Error(w, "Runner not available.", http.StatusInternalServerError)
return
}
config := rh.Handle(w, req)
// TODO(flowlo): Find out whether Handle completed successfully
// to check.
res, err := config.Run()
if err != nil {
http.Error(w, "docker: "+err.Error(), http.StatusInternalServerError)
}
rh.Respond(w, req, res)
}
// Rudimentary CORS checking. See
// https://developer.mozilla.org/docs/Web/HTTP/Access_control_CORS
func cors(w http.ResponseWriter, req *http.Request) {
origin := req.Header.Get("Origin")
// only allow CORS on localhost for development
if strings.HasPrefix(origin, "http://localhost") {
// The cookie related headers are used for the api requests authentication
w.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE, PATCH")
w.Header().Set("Access-Control-Allow-Headers", "Cookie, Content-Type")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Origin", origin)
if req.Method == "OPTIONS" {
w.Write([]byte("OK"))
}
}
}