blob: 02c245a2444a03b1c87806ef5b36716dbf123385 [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.orgc81d1c42014-04-14 18:53:10 +000012 _ "github.com/mattn/go-sqlite3"
13 htemplate "html/template"
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +000014 "io/ioutil"
15 "log"
16 "net/http"
17 "os"
18 "os/exec"
19 "path/filepath"
commit-bot@chromium.orgc81d1c42014-04-14 18:53:10 +000020 "regexp"
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +000021 "strings"
22 "text/template"
23)
24
25const (
26 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 +000027 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.orgc81d1c42014-04-14 18:53:10 +000028 DEFAULT_SAMPLE = `SkPaint p;
29p.setColor(SK_ColorRED);
30p.setAntiAlias(true);
31p.setStyle(SkPaint::kStroke_Style);
32p.setStrokeWidth(10);
33
34canvas->drawLine(20, 20, 100, 100, p);
35`
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +000036)
37
38var (
39 // codeTemplate is the cpp code template the user's code is copied into.
40 codeTemplate *template.Template = nil
41
42 // index is the main index.html page we serve.
commit-bot@chromium.orgc81d1c42014-04-14 18:53:10 +000043 index *htemplate.Template = nil
commit-bot@chromium.org282333f2014-04-14 14:54:07 +000044
45 // db is the database, nil if we don't have an SQL database to store data into.
46 db *sql.DB = nil
commit-bot@chromium.orgc81d1c42014-04-14 18:53:10 +000047
48 // directLink is the regex that matches URLs paths that are direct links.
49 directLink = regexp.MustCompile("^c/([a-a0-9]+)$")
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +000050)
51
52// flags
53var (
54 useChroot = flag.Bool("use_chroot", false, "Run the compiled code in the schroot jail.")
commit-bot@chromium.org282333f2014-04-14 14:54:07 +000055 port = flag.String("port", ":8000", "HTTP service address (e.g., ':8000')")
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +000056)
57
58// lineNumbers adds #line numbering to the user's code.
59func LineNumbers(c string) string {
60 lines := strings.Split(c, "\n")
61 ret := []string{}
62 for i, line := range lines {
63 ret = append(ret, fmt.Sprintf("#line %d", i+1))
64 ret = append(ret, line)
65 }
66 return strings.Join(ret, "\n")
67}
68
69func init() {
commit-bot@chromium.orgc81d1c42014-04-14 18:53:10 +000070
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +000071 // Change the current working directory to the directory of the executable.
72 var err error
73 cwd, err := filepath.Abs(filepath.Dir(os.Args[0]))
74 if err != nil {
75 log.Fatal(err)
76 }
77 os.Chdir(cwd)
78
79 codeTemplate, err = template.ParseFiles(filepath.Join(cwd, "templates/template.cpp"))
80 if err != nil {
81 panic(err)
82 }
commit-bot@chromium.orgc81d1c42014-04-14 18:53:10 +000083 // Convert index.html into a template, which is expanded with the code.
84 index, err = htemplate.ParseFiles(filepath.Join(cwd, "templates/index.html"))
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +000085 if err != nil {
86 panic(err)
87 }
commit-bot@chromium.org282333f2014-04-14 14:54:07 +000088
89 // Connect to MySQL server. First, get the password from the metadata server.
90 // See https://developers.google.com/compute/docs/metadata#custom.
91 req, err := http.NewRequest("GET", "http://metadata/computeMetadata/v1/instance/attributes/password", nil)
92 if err != nil {
93 panic(err)
94 }
95 client := http.Client{}
96 req.Header.Add("X-Google-Metadata-Request", "True")
97 if resp, err := client.Do(req); err == nil {
98 password, err := ioutil.ReadAll(resp.Body)
99 if err != nil {
100 log.Printf("ERROR: Failed to read password from metadata server: %q\n", err)
101 panic(err)
102 }
103 // The IP address of the database is found here:
104 // https://console.developers.google.com/project/31977622648/sql/instances/webtry/overview
105 // And 3306 is the default port for MySQL.
106 db, err = sql.Open("mysql", fmt.Sprintf("webtry:%s@tcp(173.194.83.52:3306)/webtry", password))
107 if err != nil {
108 log.Printf("ERROR: Failed to open connection to SQL server: %q\n", err)
109 panic(err)
110 }
111 } else {
commit-bot@chromium.orgc81d1c42014-04-14 18:53:10 +0000112 // Fallback to sqlite for local use.
113 db, err = sql.Open("sqlite3", "./webtry.db")
114 if err != nil {
115 log.Printf("ERROR: Failed to open: %q\n", err)
116 panic(err)
117 }
118 sql := `CREATE TABLE webtry (
119 code TEXT DEFAULT '' NOT NULL,
120 create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
121 hash CHAR(64) DEFAULT '' NOT NULL,
122 PRIMARY KEY(hash)
123 )`
124 db.Exec(sql)
commit-bot@chromium.org282333f2014-04-14 14:54:07 +0000125 log.Printf("INFO: Failed to find metadata, unable to connect to MySQL server (Expected when running locally): %q\n", err)
126 }
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +0000127}
128
129// userCode is used in template expansion.
130type userCode struct {
131 UserCode string
132}
133
134// expandToFile expands the template and writes the result to the file.
135func expandToFile(filename string, code string, t *template.Template) error {
136 f, err := os.Create(filename)
137 if err != nil {
138 return err
139 }
140 defer f.Close()
commit-bot@chromium.orgc81d1c42014-04-14 18:53:10 +0000141 return t.Execute(f, userCode{UserCode: code})
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +0000142}
143
144// expandCode expands the template into a file and calculate the MD5 hash.
145func expandCode(code string) (string, error) {
146 h := md5.New()
147 h.Write([]byte(code))
148 hash := fmt.Sprintf("%x", h.Sum(nil))
149 // At this point we are running in skia/experimental/webtry, making cache a
150 // peer directory to skia.
151 // TODO(jcgregorio) Make all relative directories into flags.
152 err := expandToFile(fmt.Sprintf("../../../cache/%s.cpp", hash), code, codeTemplate)
153 return hash, err
154}
155
156// response is serialized to JSON as a response to POSTs.
157type response struct {
158 Message string `json:"message"`
159 Img string `json:"img"`
commit-bot@chromium.orgc81d1c42014-04-14 18:53:10 +0000160 Hash string `json:"hash"`
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +0000161}
162
163// doCmd executes the given command line string in either the out/Debug
164// directory or the inout directory. Returns the stdout, and stderr in the case
165// of a non-zero exit code.
166func doCmd(commandLine string, moveToDebug bool) (string, error) {
167 log.Printf("Command: %q\n", commandLine)
168 programAndArgs := strings.SplitN(commandLine, " ", 2)
169 program := programAndArgs[0]
170 args := []string{}
171 if len(programAndArgs) > 1 {
172 args = strings.Split(programAndArgs[1], " ")
173 }
174 cmd := exec.Command(program, args...)
175 abs, err := filepath.Abs("../../out/Debug")
176 if err != nil {
177 return "", fmt.Errorf("Failed to find absolute path to Debug directory.")
178 }
179 if moveToDebug {
180 cmd.Dir = abs
181 } else if !*useChroot { // Don't set cmd.Dir when using chroot.
182 abs, err := filepath.Abs("../../../inout")
183 if err != nil {
184 return "", fmt.Errorf("Failed to find absolute path to inout directory.")
185 }
186 cmd.Dir = abs
187 }
188 log.Printf("Run in directory: %q\n", cmd.Dir)
189 var stdOut bytes.Buffer
190 cmd.Stdout = &stdOut
191 var stdErr bytes.Buffer
192 cmd.Stderr = &stdErr
193 cmd.Start()
194 err = cmd.Wait()
195 message := stdOut.String()
196 log.Printf("StdOut: %s\n", message)
197 if err != nil {
198 log.Printf("Exit status: %s\n", err.Error())
199 log.Printf("StdErr: %s\n", stdErr.String())
200 message += stdErr.String()
201 return message, fmt.Errorf("Failed to run command.")
202 }
203 return message, nil
204}
205
206// reportError formats an HTTP error response and also logs the detailed error message.
207func reportError(w http.ResponseWriter, r *http.Request, err error, message string) {
208 m := response{
209 Message: message,
210 }
211 log.Printf("Error: %s\n%s", message, err.Error())
212 resp, err := json.Marshal(m)
213 if err != nil {
214 http.Error(w, "Failed to serialize a response", 500)
215 return
216 }
217 w.Write(resp)
218}
219
commit-bot@chromium.org282333f2014-04-14 14:54:07 +0000220func writeToDatabase(hash string, code string) {
221 if db == nil {
222 return
223 }
224 if _, err := db.Exec("INSERT INTO webtry (code, hash) VALUES(?, ?)", code, hash); err != nil {
225 log.Printf("ERROR: Failed to insert code into database: %q\n", err)
226 }
227}
228
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +0000229// mainHandler handles the GET and POST of the main page.
230func mainHandler(w http.ResponseWriter, r *http.Request) {
231 if r.Method == "GET" {
commit-bot@chromium.orgc81d1c42014-04-14 18:53:10 +0000232 code := DEFAULT_SAMPLE
233 directLink := regexp.MustCompile("^/c/([a-f0-9]+)$")
234 match := directLink.FindStringSubmatch(r.URL.Path)
235 if len(match) == 2 {
236 hash := match[1]
237 if db == nil {
238 http.NotFound(w, r)
239 return
240 }
241 // Update 'code' with the code found in the database.
242 if err := db.QueryRow("SELECT code FROM webtry WHERE hash=?", hash).Scan(&code); err != nil {
243 http.NotFound(w, r)
244 return
245 }
246 }
247 // Expand the template.
248 if err := index.Execute(w, userCode{UserCode: code}); err != nil {
249 log.Printf("ERROR: Failed to expand template: %q\n", err)
250 }
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +0000251 } else if r.Method == "POST" {
252 w.Header().Set("Content-Type", "application/json")
253 b, err := ioutil.ReadAll(r.Body)
254 if err != nil {
255 reportError(w, r, err, "Failed to read a request body.")
256 return
257 }
commit-bot@chromium.org282333f2014-04-14 14:54:07 +0000258 code := string(b)
259 hash, err := expandCode(LineNumbers(code))
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +0000260 if err != nil {
261 reportError(w, r, err, "Failed to write the code to compile.")
262 return
263 }
commit-bot@chromium.org282333f2014-04-14 14:54:07 +0000264 writeToDatabase(hash, code)
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +0000265 message, err := doCmd(fmt.Sprintf(RESULT_COMPILE, hash, hash), true)
266 if err != nil {
267 reportError(w, r, err, "Failed to compile the code:\n"+message)
268 return
269 }
270 linkMessage, err := doCmd(fmt.Sprintf(LINK, hash, hash), true)
271 if err != nil {
272 reportError(w, r, err, "Failed to link the code:\n"+linkMessage)
273 return
274 }
275 message += linkMessage
276 cmd := hash + " --out " + hash + ".png"
277 if *useChroot {
278 cmd = "schroot -c webtry --directory=/inout -- /inout/" + cmd
279 } else {
280 abs, err := filepath.Abs("../../../inout")
281 if err != nil {
282 reportError(w, r, err, "Failed to find executable directory.")
283 return
284 }
285 cmd = abs + "/" + cmd
286 }
287
288 execMessage, err := doCmd(cmd, false)
289 if err != nil {
290 reportError(w, r, err, "Failed to run the code:\n"+execMessage)
291 return
292 }
293 png, err := ioutil.ReadFile("../../../inout/" + hash + ".png")
294 if err != nil {
295 reportError(w, r, err, "Failed to open the generated PNG.")
296 return
297 }
298
299 m := response{
300 Message: message,
301 Img: base64.StdEncoding.EncodeToString([]byte(png)),
commit-bot@chromium.orgc81d1c42014-04-14 18:53:10 +0000302 Hash: hash,
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +0000303 }
304 resp, err := json.Marshal(m)
305 if err != nil {
306 reportError(w, r, err, "Failed to serialize a response.")
307 return
308 }
309 w.Write(resp)
310 }
311}
312
313func main() {
314 flag.Parse()
315
316 http.HandleFunc("/", mainHandler)
commit-bot@chromium.org282333f2014-04-14 14:54:07 +0000317 log.Fatal(http.ListenAndServe(*port, nil))
commit-bot@chromium.org6d036c22014-04-09 18:59:44 +0000318}