blob: fb53b593e36e0e3d67ca6c413d20bdc47b4adfa9 [file] [log] [blame]
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +00001package main
2
3import (
4 "bytes"
5 "crypto/md5"
commit-bot@chromium.org282333f2014-04-14 14:54:07 +00006 "database/sql"
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +00007 "encoding/base64"
8 "encoding/json"
9 "flag"
10 "fmt"
commit-bot@chromium.org282333f2014-04-14 14:54:07 +000011 _ "github.com/go-sql-driver/mysql"
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +000012 "io/ioutil"
13 "log"
14 "net/http"
15 "os"
16 "os/exec"
17 "path/filepath"
18 "strings"
19 "text/template"
20)
21
22const (
23 RESULT_COMPILE = `c++ -DSK_GAMMA_SRGB -DSK_GAMMA_APPLY_TO_A8 -DSK_SCALAR_TO_FLOAT_EXCLUDED -DSK_ALLOW_STATIC_GLOBAL_INITIALIZERS=1 -DSK_SUPPORT_GPU=0 -DSK_SUPPORT_OPENCL=0 -DSK_FORCE_DISTANCEFIELD_FONTS=0 -DSK_SCALAR_IS_FLOAT -DSK_CAN_USE_FLOAT -DSK_SAMPLES_FOR_X -DSK_BUILD_FOR_UNIX -DSK_USE_POSIX_THREADS -DSK_SYSTEM_ZLIB=1 -DSK_DEBUG -DSK_DEVELOPER=1 -I../../src/core -I../../src/images -I../../tools/flags -I../../include/config -I../../include/core -I../../include/pathops -I../../include/pipe -I../../include/effects -I../../include/ports -I../../src/sfnt -I../../include/utils -I../../src/utils -I../../include/images -g -fno-exceptions -fstrict-aliasing -Wall -Wextra -Winit-self -Wpointer-arith -Wno-unused-parameter -Wno-c++11-extensions -Werror -m64 -fno-rtti -Wnon-virtual-dtor -c ../../../cache/%s.cpp -o ../../../cache/%s.o`
commit-bot@chromium.orgd6cab4a2014-04-09 21:35:18 +000024 LINK = `c++ -m64 -lstdc++ -lm -o ../../../inout/%s -Wl,--start-group ../../../cache/%s.o obj/experimental/webtry/webtry.main.o obj/gyp/libflags.a libskia_images.a libskia_core.a libskia_effects.a obj/gyp/libjpeg.a obj/gyp/libwebp_dec.a obj/gyp/libwebp_demux.a obj/gyp/libwebp_dsp.a obj/gyp/libwebp_enc.a obj/gyp/libwebp_utils.a libskia_utils.a libskia_opts.a libskia_opts_ssse3.a libskia_ports.a libskia_sfnt.a -Wl,--end-group -lpng -lz -lgif -lpthread -lfontconfig -ldl -lfreetype`
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +000025)
26
27var (
28 // codeTemplate is the cpp code template the user's code is copied into.
29 codeTemplate *template.Template = nil
30
31 // index is the main index.html page we serve.
32 index []byte
commit-bot@chromium.org282333f2014-04-14 14:54:07 +000033
34 // db is the database, nil if we don't have an SQL database to store data into.
35 db *sql.DB = nil
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +000036)
37
38// flags
39var (
40 useChroot = flag.Bool("use_chroot", false, "Run the compiled code in the schroot jail.")
commit-bot@chromium.org282333f2014-04-14 14:54:07 +000041 port = flag.String("port", ":8000", "HTTP service address (e.g., ':8000')")
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +000042)
43
44// lineNumbers adds #line numbering to the user's code.
45func LineNumbers(c string) string {
46 lines := strings.Split(c, "\n")
47 ret := []string{}
48 for i, line := range lines {
49 ret = append(ret, fmt.Sprintf("#line %d", i+1))
50 ret = append(ret, line)
51 }
52 return strings.Join(ret, "\n")
53}
54
55func init() {
56 // Change the current working directory to the directory of the executable.
57 var err error
58 cwd, err := filepath.Abs(filepath.Dir(os.Args[0]))
59 if err != nil {
60 log.Fatal(err)
61 }
62 os.Chdir(cwd)
63
64 codeTemplate, err = template.ParseFiles(filepath.Join(cwd, "templates/template.cpp"))
65 if err != nil {
66 panic(err)
67 }
68 index, err = ioutil.ReadFile(filepath.Join(cwd, "templates/index.html"))
69 if err != nil {
70 panic(err)
71 }
commit-bot@chromium.org282333f2014-04-14 14:54:07 +000072
73 // Connect to MySQL server. First, get the password from the metadata server.
74 // See https://developers.google.com/compute/docs/metadata#custom.
75 req, err := http.NewRequest("GET", "http://metadata/computeMetadata/v1/instance/attributes/password", nil)
76 if err != nil {
77 panic(err)
78 }
79 client := http.Client{}
80 req.Header.Add("X-Google-Metadata-Request", "True")
81 if resp, err := client.Do(req); err == nil {
82 password, err := ioutil.ReadAll(resp.Body)
83 if err != nil {
84 log.Printf("ERROR: Failed to read password from metadata server: %q\n", err)
85 panic(err)
86 }
87 // The IP address of the database is found here:
88 // https://console.developers.google.com/project/31977622648/sql/instances/webtry/overview
89 // And 3306 is the default port for MySQL.
90 db, err = sql.Open("mysql", fmt.Sprintf("webtry:%s@tcp(173.194.83.52:3306)/webtry", password))
91 if err != nil {
92 log.Printf("ERROR: Failed to open connection to SQL server: %q\n", err)
93 panic(err)
94 }
95 } else {
96 log.Printf("INFO: Failed to find metadata, unable to connect to MySQL server (Expected when running locally): %q\n", err)
97 }
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +000098}
99
100// userCode is used in template expansion.
101type userCode struct {
102 UserCode string
103}
104
105// expandToFile expands the template and writes the result to the file.
106func expandToFile(filename string, code string, t *template.Template) error {
107 f, err := os.Create(filename)
108 if err != nil {
109 return err
110 }
111 defer f.Close()
112 return t.Execute(f, struct{ UserCode string }{UserCode: code})
113}
114
115// expandCode expands the template into a file and calculate the MD5 hash.
116func expandCode(code string) (string, error) {
117 h := md5.New()
118 h.Write([]byte(code))
119 hash := fmt.Sprintf("%x", h.Sum(nil))
120 // At this point we are running in skia/experimental/webtry, making cache a
121 // peer directory to skia.
122 // TODO(jcgregorio) Make all relative directories into flags.
123 err := expandToFile(fmt.Sprintf("../../../cache/%s.cpp", hash), code, codeTemplate)
124 return hash, err
125}
126
127// response is serialized to JSON as a response to POSTs.
128type response struct {
129 Message string `json:"message"`
130 Img string `json:"img"`
131}
132
133// doCmd executes the given command line string in either the out/Debug
134// directory or the inout directory. Returns the stdout, and stderr in the case
135// of a non-zero exit code.
136func doCmd(commandLine string, moveToDebug bool) (string, error) {
137 log.Printf("Command: %q\n", commandLine)
138 programAndArgs := strings.SplitN(commandLine, " ", 2)
139 program := programAndArgs[0]
140 args := []string{}
141 if len(programAndArgs) > 1 {
142 args = strings.Split(programAndArgs[1], " ")
143 }
144 cmd := exec.Command(program, args...)
145 abs, err := filepath.Abs("../../out/Debug")
146 if err != nil {
147 return "", fmt.Errorf("Failed to find absolute path to Debug directory.")
148 }
149 if moveToDebug {
150 cmd.Dir = abs
151 } else if !*useChroot { // Don't set cmd.Dir when using chroot.
152 abs, err := filepath.Abs("../../../inout")
153 if err != nil {
154 return "", fmt.Errorf("Failed to find absolute path to inout directory.")
155 }
156 cmd.Dir = abs
157 }
158 log.Printf("Run in directory: %q\n", cmd.Dir)
159 var stdOut bytes.Buffer
160 cmd.Stdout = &stdOut
161 var stdErr bytes.Buffer
162 cmd.Stderr = &stdErr
163 cmd.Start()
164 err = cmd.Wait()
165 message := stdOut.String()
166 log.Printf("StdOut: %s\n", message)
167 if err != nil {
168 log.Printf("Exit status: %s\n", err.Error())
169 log.Printf("StdErr: %s\n", stdErr.String())
170 message += stdErr.String()
171 return message, fmt.Errorf("Failed to run command.")
172 }
173 return message, nil
174}
175
176// reportError formats an HTTP error response and also logs the detailed error message.
177func reportError(w http.ResponseWriter, r *http.Request, err error, message string) {
178 m := response{
179 Message: message,
180 }
181 log.Printf("Error: %s\n%s", message, err.Error())
182 resp, err := json.Marshal(m)
183 if err != nil {
184 http.Error(w, "Failed to serialize a response", 500)
185 return
186 }
187 w.Write(resp)
188}
189
commit-bot@chromium.org282333f2014-04-14 14:54:07 +0000190func writeToDatabase(hash string, code string) {
191 if db == nil {
192 return
193 }
194 if _, err := db.Exec("INSERT INTO webtry (code, hash) VALUES(?, ?)", code, hash); err != nil {
195 log.Printf("ERROR: Failed to insert code into database: %q\n", err)
196 }
197}
198
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +0000199// mainHandler handles the GET and POST of the main page.
200func mainHandler(w http.ResponseWriter, r *http.Request) {
201 if r.Method == "GET" {
202 w.Write(index)
203 } else if r.Method == "POST" {
204 w.Header().Set("Content-Type", "application/json")
205 b, err := ioutil.ReadAll(r.Body)
206 if err != nil {
207 reportError(w, r, err, "Failed to read a request body.")
208 return
209 }
commit-bot@chromium.org282333f2014-04-14 14:54:07 +0000210 code := string(b)
211 hash, err := expandCode(LineNumbers(code))
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +0000212 if err != nil {
213 reportError(w, r, err, "Failed to write the code to compile.")
214 return
215 }
commit-bot@chromium.org282333f2014-04-14 14:54:07 +0000216 writeToDatabase(hash, code)
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +0000217 message, err := doCmd(fmt.Sprintf(RESULT_COMPILE, hash, hash), true)
218 if err != nil {
219 reportError(w, r, err, "Failed to compile the code:\n"+message)
220 return
221 }
222 linkMessage, err := doCmd(fmt.Sprintf(LINK, hash, hash), true)
223 if err != nil {
224 reportError(w, r, err, "Failed to link the code:\n"+linkMessage)
225 return
226 }
227 message += linkMessage
228 cmd := hash + " --out " + hash + ".png"
229 if *useChroot {
230 cmd = "schroot -c webtry --directory=/inout -- /inout/" + cmd
231 } else {
232 abs, err := filepath.Abs("../../../inout")
233 if err != nil {
234 reportError(w, r, err, "Failed to find executable directory.")
235 return
236 }
237 cmd = abs + "/" + cmd
238 }
239
240 execMessage, err := doCmd(cmd, false)
241 if err != nil {
242 reportError(w, r, err, "Failed to run the code:\n"+execMessage)
243 return
244 }
245 png, err := ioutil.ReadFile("../../../inout/" + hash + ".png")
246 if err != nil {
247 reportError(w, r, err, "Failed to open the generated PNG.")
248 return
249 }
250
251 m := response{
252 Message: message,
253 Img: base64.StdEncoding.EncodeToString([]byte(png)),
254 }
255 resp, err := json.Marshal(m)
256 if err != nil {
257 reportError(w, r, err, "Failed to serialize a response.")
258 return
259 }
260 w.Write(resp)
261 }
262}
263
264func main() {
265 flag.Parse()
266
267 http.HandleFunc("/", mainHandler)
commit-bot@chromium.org282333f2014-04-14 14:54:07 +0000268 log.Fatal(http.ListenAndServe(*port, nil))
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +0000269}