commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/md5" |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 6 | "database/sql" |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 7 | "encoding/base64" |
| 8 | "encoding/json" |
| 9 | "flag" |
| 10 | "fmt" |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 11 | _ "github.com/go-sql-driver/mysql" |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 12 | _ "github.com/mattn/go-sqlite3" |
| 13 | htemplate "html/template" |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 14 | "io/ioutil" |
| 15 | "log" |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 16 | "math/rand" |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 17 | "net/http" |
| 18 | "os" |
| 19 | "os/exec" |
| 20 | "path/filepath" |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 21 | "regexp" |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 22 | "strings" |
| 23 | "text/template" |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 24 | "time" |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | const ( |
| 28 | 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.org | d6cab4a | 2014-04-09 21:35:18 +0000 | [diff] [blame] | 29 | 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.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 30 | DEFAULT_SAMPLE = `SkPaint p; |
| 31 | p.setColor(SK_ColorRED); |
| 32 | p.setAntiAlias(true); |
| 33 | p.setStyle(SkPaint::kStroke_Style); |
| 34 | p.setStrokeWidth(10); |
| 35 | |
| 36 | canvas->drawLine(20, 20, 100, 100, p); |
| 37 | ` |
commit-bot@chromium.org | 4bd8fdc | 2014-04-15 00:43:51 +0000 | [diff] [blame] | 38 | // Don't increase above 2^16 w/o altering the db tables to accept something bigger than TEXT. |
| 39 | MAX_TRY_SIZE = 64000 |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 40 | ) |
| 41 | |
| 42 | var ( |
| 43 | // codeTemplate is the cpp code template the user's code is copied into. |
| 44 | codeTemplate *template.Template = nil |
| 45 | |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 46 | // indexTemplate is the main index.html page we serve. |
| 47 | indexTemplate *htemplate.Template = nil |
| 48 | |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 49 | // iframeTemplate is the main index.html page we serve. |
| 50 | iframeTemplate *htemplate.Template = nil |
| 51 | |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 52 | // recentTemplate is a list of recent images. |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 53 | recentTemplate *htemplate.Template = nil |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 54 | |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 55 | // workspaceTemplate is the page for workspaces, a series of webtrys. |
| 56 | workspaceTemplate *htemplate.Template = nil |
| 57 | |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 58 | // db is the database, nil if we don't have an SQL database to store data into. |
| 59 | db *sql.DB = nil |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 60 | |
| 61 | // directLink is the regex that matches URLs paths that are direct links. |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 62 | directLink = regexp.MustCompile("^/c/([a-f0-9]+)$") |
| 63 | |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 64 | // iframeLink is the regex that matches URLs paths that are links to iframes. |
| 65 | iframeLink = regexp.MustCompile("^/iframe/([a-f0-9]+)$") |
| 66 | |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 67 | // imageLink is the regex that matches URLs paths that are direct links to PNGs. |
| 68 | imageLink = regexp.MustCompile("^/i/([a-f0-9]+.png)$") |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 69 | |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 70 | // tryInfoLink is the regex that matches URLs paths that are direct links to data about a single try. |
| 71 | tryInfoLink = regexp.MustCompile("^/json/([a-f0-9]+)$") |
| 72 | |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 73 | // workspaceLink is the regex that matches URLs paths for workspaces. |
| 74 | workspaceLink = regexp.MustCompile("^/w/([a-z0-9-]+)$") |
| 75 | |
| 76 | // workspaceNameAdj is a list of adjectives for building workspace names. |
| 77 | workspaceNameAdj = []string{ |
| 78 | "autumn", "hidden", "bitter", "misty", "silent", "empty", "dry", "dark", |
| 79 | "summer", "icy", "delicate", "quiet", "white", "cool", "spring", "winter", |
| 80 | "patient", "twilight", "dawn", "crimson", "wispy", "weathered", "blue", |
| 81 | "billowing", "broken", "cold", "damp", "falling", "frosty", "green", |
| 82 | "long", "late", "lingering", "bold", "little", "morning", "muddy", "old", |
| 83 | "red", "rough", "still", "small", "sparkling", "throbbing", "shy", |
| 84 | "wandering", "withered", "wild", "black", "young", "holy", "solitary", |
| 85 | "fragrant", "aged", "snowy", "proud", "floral", "restless", "divine", |
| 86 | "polished", "ancient", "purple", "lively", "nameless", |
| 87 | } |
| 88 | |
| 89 | // workspaceNameNoun is a list of nouns for building workspace names. |
| 90 | workspaceNameNoun = []string{ |
| 91 | "waterfall", "river", "breeze", "moon", "rain", "wind", "sea", "morning", |
| 92 | "snow", "lake", "sunset", "pine", "shadow", "leaf", "dawn", "glitter", |
| 93 | "forest", "hill", "cloud", "meadow", "sun", "glade", "bird", "brook", |
| 94 | "butterfly", "bush", "dew", "dust", "field", "fire", "flower", "firefly", |
| 95 | "feather", "grass", "haze", "mountain", "night", "pond", "darkness", |
| 96 | "snowflake", "silence", "sound", "sky", "shape", "surf", "thunder", |
| 97 | "violet", "water", "wildflower", "wave", "water", "resonance", "sun", |
| 98 | "wood", "dream", "cherry", "tree", "fog", "frost", "voice", "paper", |
| 99 | "frog", "smoke", "star", |
| 100 | } |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 101 | ) |
| 102 | |
| 103 | // flags |
| 104 | var ( |
| 105 | useChroot = flag.Bool("use_chroot", false, "Run the compiled code in the schroot jail.") |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 106 | port = flag.String("port", ":8000", "HTTP service address (e.g., ':8000')") |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 107 | ) |
| 108 | |
| 109 | // lineNumbers adds #line numbering to the user's code. |
| 110 | func LineNumbers(c string) string { |
| 111 | lines := strings.Split(c, "\n") |
| 112 | ret := []string{} |
| 113 | for i, line := range lines { |
| 114 | ret = append(ret, fmt.Sprintf("#line %d", i+1)) |
| 115 | ret = append(ret, line) |
| 116 | } |
| 117 | return strings.Join(ret, "\n") |
| 118 | } |
| 119 | |
| 120 | func init() { |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 121 | rand.Seed(time.Now().UnixNano()) |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 122 | |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 123 | // Change the current working directory to the directory of the executable. |
| 124 | var err error |
| 125 | cwd, err := filepath.Abs(filepath.Dir(os.Args[0])) |
| 126 | if err != nil { |
| 127 | log.Fatal(err) |
| 128 | } |
| 129 | os.Chdir(cwd) |
| 130 | |
| 131 | codeTemplate, err = template.ParseFiles(filepath.Join(cwd, "templates/template.cpp")) |
| 132 | if err != nil { |
| 133 | panic(err) |
| 134 | } |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 135 | indexTemplate, err = htemplate.ParseFiles( |
| 136 | filepath.Join(cwd, "templates/index.html"), |
| 137 | filepath.Join(cwd, "templates/titlebar.html"), |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 138 | filepath.Join(cwd, "templates/content.html"), |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 139 | ) |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 140 | if err != nil { |
| 141 | panic(err) |
| 142 | } |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 143 | iframeTemplate, err = htemplate.ParseFiles( |
| 144 | filepath.Join(cwd, "templates/iframe.html"), |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 145 | filepath.Join(cwd, "templates/content.html"), |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 146 | ) |
| 147 | if err != nil { |
| 148 | panic(err) |
| 149 | } |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 150 | recentTemplate, err = htemplate.ParseFiles( |
| 151 | filepath.Join(cwd, "templates/recent.html"), |
| 152 | filepath.Join(cwd, "templates/titlebar.html"), |
| 153 | ) |
| 154 | if err != nil { |
| 155 | panic(err) |
| 156 | } |
| 157 | workspaceTemplate, err = htemplate.ParseFiles( |
| 158 | filepath.Join(cwd, "templates/workspace.html"), |
| 159 | filepath.Join(cwd, "templates/titlebar.html"), |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 160 | filepath.Join(cwd, "templates/content.html"), |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 161 | ) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 162 | if err != nil { |
| 163 | panic(err) |
| 164 | } |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 165 | |
| 166 | // Connect to MySQL server. First, get the password from the metadata server. |
| 167 | // See https://developers.google.com/compute/docs/metadata#custom. |
| 168 | req, err := http.NewRequest("GET", "http://metadata/computeMetadata/v1/instance/attributes/password", nil) |
| 169 | if err != nil { |
| 170 | panic(err) |
| 171 | } |
| 172 | client := http.Client{} |
| 173 | req.Header.Add("X-Google-Metadata-Request", "True") |
| 174 | if resp, err := client.Do(req); err == nil { |
| 175 | password, err := ioutil.ReadAll(resp.Body) |
| 176 | if err != nil { |
| 177 | log.Printf("ERROR: Failed to read password from metadata server: %q\n", err) |
| 178 | panic(err) |
| 179 | } |
| 180 | // The IP address of the database is found here: |
| 181 | // https://console.developers.google.com/project/31977622648/sql/instances/webtry/overview |
| 182 | // And 3306 is the default port for MySQL. |
commit-bot@chromium.org | 4bd8fdc | 2014-04-15 00:43:51 +0000 | [diff] [blame] | 183 | db, err = sql.Open("mysql", fmt.Sprintf("webtry:%s@tcp(173.194.83.52:3306)/webtry?parseTime=true", password)) |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 184 | if err != nil { |
| 185 | log.Printf("ERROR: Failed to open connection to SQL server: %q\n", err) |
| 186 | panic(err) |
| 187 | } |
| 188 | } else { |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 189 | log.Printf("INFO: Failed to find metadata, unable to connect to MySQL server (Expected when running locally): %q\n", err) |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 190 | // Fallback to sqlite for local use. |
| 191 | db, err = sql.Open("sqlite3", "./webtry.db") |
| 192 | if err != nil { |
| 193 | log.Printf("ERROR: Failed to open: %q\n", err) |
| 194 | panic(err) |
| 195 | } |
| 196 | sql := `CREATE TABLE webtry ( |
| 197 | code TEXT DEFAULT '' NOT NULL, |
| 198 | create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 199 | hash CHAR(64) DEFAULT '' NOT NULL, |
| 200 | PRIMARY KEY(hash) |
| 201 | )` |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 202 | _, err = db.Exec(sql) |
| 203 | log.Printf("Info: status creating sqlite table for webtry: %q\n", err) |
| 204 | sql = `CREATE TABLE workspace ( |
| 205 | name CHAR(64) DEFAULT '' NOT NULL, |
| 206 | create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 207 | PRIMARY KEY(name) |
| 208 | )` |
| 209 | _, err = db.Exec(sql) |
| 210 | log.Printf("Info: status creating sqlite table for workspace: %q\n", err) |
| 211 | sql = `CREATE TABLE workspacetry ( |
| 212 | name CHAR(64) DEFAULT '' NOT NULL, |
| 213 | create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 214 | hash CHAR(64) DEFAULT '' NOT NULL, |
| 215 | hidden INTEGER DEFAULT 0 NOT NULL, |
| 216 | |
| 217 | FOREIGN KEY (name) REFERENCES workspace(name) |
| 218 | )` |
| 219 | _, err = db.Exec(sql) |
| 220 | log.Printf("Info: status creating sqlite table for workspace try: %q\n", err) |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 221 | } |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | // userCode is used in template expansion. |
| 225 | type userCode struct { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 226 | Code string |
| 227 | Hash string |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // expandToFile expands the template and writes the result to the file. |
| 231 | func expandToFile(filename string, code string, t *template.Template) error { |
| 232 | f, err := os.Create(filename) |
| 233 | if err != nil { |
| 234 | return err |
| 235 | } |
| 236 | defer f.Close() |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 237 | return t.Execute(f, userCode{Code: code}) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // expandCode expands the template into a file and calculate the MD5 hash. |
| 241 | func expandCode(code string) (string, error) { |
| 242 | h := md5.New() |
| 243 | h.Write([]byte(code)) |
| 244 | hash := fmt.Sprintf("%x", h.Sum(nil)) |
| 245 | // At this point we are running in skia/experimental/webtry, making cache a |
| 246 | // peer directory to skia. |
| 247 | // TODO(jcgregorio) Make all relative directories into flags. |
| 248 | err := expandToFile(fmt.Sprintf("../../../cache/%s.cpp", hash), code, codeTemplate) |
| 249 | return hash, err |
| 250 | } |
| 251 | |
| 252 | // response is serialized to JSON as a response to POSTs. |
| 253 | type response struct { |
| 254 | Message string `json:"message"` |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 255 | StdOut string `json:"stdout"` |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 256 | Img string `json:"img"` |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 257 | Hash string `json:"hash"` |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // doCmd executes the given command line string in either the out/Debug |
| 261 | // directory or the inout directory. Returns the stdout, and stderr in the case |
| 262 | // of a non-zero exit code. |
| 263 | func doCmd(commandLine string, moveToDebug bool) (string, error) { |
| 264 | log.Printf("Command: %q\n", commandLine) |
| 265 | programAndArgs := strings.SplitN(commandLine, " ", 2) |
| 266 | program := programAndArgs[0] |
| 267 | args := []string{} |
| 268 | if len(programAndArgs) > 1 { |
| 269 | args = strings.Split(programAndArgs[1], " ") |
| 270 | } |
| 271 | cmd := exec.Command(program, args...) |
| 272 | abs, err := filepath.Abs("../../out/Debug") |
| 273 | if err != nil { |
| 274 | return "", fmt.Errorf("Failed to find absolute path to Debug directory.") |
| 275 | } |
| 276 | if moveToDebug { |
| 277 | cmd.Dir = abs |
| 278 | } else if !*useChroot { // Don't set cmd.Dir when using chroot. |
| 279 | abs, err := filepath.Abs("../../../inout") |
| 280 | if err != nil { |
| 281 | return "", fmt.Errorf("Failed to find absolute path to inout directory.") |
| 282 | } |
| 283 | cmd.Dir = abs |
| 284 | } |
| 285 | log.Printf("Run in directory: %q\n", cmd.Dir) |
| 286 | var stdOut bytes.Buffer |
| 287 | cmd.Stdout = &stdOut |
| 288 | var stdErr bytes.Buffer |
| 289 | cmd.Stderr = &stdErr |
| 290 | cmd.Start() |
| 291 | err = cmd.Wait() |
| 292 | message := stdOut.String() |
| 293 | log.Printf("StdOut: %s\n", message) |
| 294 | if err != nil { |
| 295 | log.Printf("Exit status: %s\n", err.Error()) |
| 296 | log.Printf("StdErr: %s\n", stdErr.String()) |
| 297 | message += stdErr.String() |
| 298 | return message, fmt.Errorf("Failed to run command.") |
| 299 | } |
| 300 | return message, nil |
| 301 | } |
| 302 | |
| 303 | // reportError formats an HTTP error response and also logs the detailed error message. |
| 304 | func reportError(w http.ResponseWriter, r *http.Request, err error, message string) { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 305 | log.Printf("Error: %s\n%s", message, err.Error()) |
| 306 | http.Error(w, message, 500) |
| 307 | } |
| 308 | |
| 309 | // reportTryError formats an HTTP error response in JSON and also logs the detailed error message. |
| 310 | func reportTryError(w http.ResponseWriter, r *http.Request, err error, message, hash string) { |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 311 | m := response{ |
| 312 | Message: message, |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 313 | Hash: hash, |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 314 | } |
| 315 | log.Printf("Error: %s\n%s", message, err.Error()) |
| 316 | resp, err := json.Marshal(m) |
| 317 | if err != nil { |
| 318 | http.Error(w, "Failed to serialize a response", 500) |
| 319 | return |
| 320 | } |
| 321 | w.Write(resp) |
| 322 | } |
| 323 | |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 324 | func writeToDatabase(hash string, code string, workspaceName string) { |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 325 | if db == nil { |
| 326 | return |
| 327 | } |
| 328 | if _, err := db.Exec("INSERT INTO webtry (code, hash) VALUES(?, ?)", code, hash); err != nil { |
| 329 | log.Printf("ERROR: Failed to insert code into database: %q\n", err) |
| 330 | } |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 331 | if workspaceName != "" { |
| 332 | if _, err := db.Exec("INSERT INTO workspacetry (name, hash) VALUES(?, ?)", workspaceName, hash); err != nil { |
| 333 | log.Printf("ERROR: Failed to insert into workspacetry table: %q\n", err) |
| 334 | } |
| 335 | } |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 336 | } |
| 337 | |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 338 | func cssHandler(w http.ResponseWriter, r *http.Request) { |
| 339 | http.ServeFile(w, r, "css/webtry.css") |
| 340 | } |
| 341 | |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 342 | func jsHandler(w http.ResponseWriter, r *http.Request) { |
| 343 | http.ServeFile(w, r, "js/run.js") |
| 344 | } |
| 345 | |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 346 | // imageHandler serves up the PNG of a specific try. |
| 347 | func imageHandler(w http.ResponseWriter, r *http.Request) { |
| 348 | log.Printf("Image Handler: %q\n", r.URL.Path) |
| 349 | if r.Method != "GET" { |
| 350 | http.NotFound(w, r) |
| 351 | return |
| 352 | } |
| 353 | match := imageLink.FindStringSubmatch(r.URL.Path) |
| 354 | if len(match) != 2 { |
| 355 | http.NotFound(w, r) |
| 356 | return |
| 357 | } |
| 358 | filename := match[1] |
| 359 | http.ServeFile(w, r, fmt.Sprintf("../../../inout/%s", filename)) |
| 360 | } |
| 361 | |
| 362 | type Try struct { |
commit-bot@chromium.org | c3b738a | 2014-04-21 17:36:44 +0000 | [diff] [blame] | 363 | Hash string `json:"hash"` |
| 364 | CreateTS string `json:"create_ts"` |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | type Recent struct { |
| 368 | Tries []Try |
| 369 | } |
| 370 | |
| 371 | // recentHandler shows the last 20 tries. |
| 372 | func recentHandler(w http.ResponseWriter, r *http.Request) { |
| 373 | log.Printf("Recent Handler: %q\n", r.URL.Path) |
| 374 | |
| 375 | var err error |
| 376 | rows, err := db.Query("SELECT create_ts, hash FROM webtry ORDER BY create_ts DESC LIMIT 20") |
| 377 | if err != nil { |
| 378 | http.NotFound(w, r) |
| 379 | return |
| 380 | } |
| 381 | recent := []Try{} |
| 382 | for rows.Next() { |
| 383 | var hash string |
| 384 | var create_ts time.Time |
| 385 | if err := rows.Scan(&create_ts, &hash); err != nil { |
| 386 | log.Printf("Error: failed to fetch from database: %q", err) |
| 387 | continue |
| 388 | } |
| 389 | recent = append(recent, Try{Hash: hash, CreateTS: create_ts.Format("2006-02-01")}) |
| 390 | } |
| 391 | if err := recentTemplate.Execute(w, Recent{Tries: recent}); err != nil { |
| 392 | log.Printf("ERROR: Failed to expand template: %q\n", err) |
| 393 | } |
| 394 | } |
| 395 | |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 396 | type Workspace struct { |
| 397 | Name string |
| 398 | Code string |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 399 | Hash string |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 400 | Tries []Try |
| 401 | } |
| 402 | |
| 403 | // newWorkspace generates a new random workspace name and stores it in the database. |
| 404 | func newWorkspace() (string, error) { |
| 405 | for i := 0; i < 10; i++ { |
| 406 | adj := workspaceNameAdj[rand.Intn(len(workspaceNameAdj))] |
| 407 | noun := workspaceNameNoun[rand.Intn(len(workspaceNameNoun))] |
| 408 | suffix := rand.Intn(1000) |
| 409 | name := fmt.Sprintf("%s-%s-%d", adj, noun, suffix) |
| 410 | if _, err := db.Exec("INSERT INTO workspace (name) VALUES(?)", name); err == nil { |
| 411 | return name, nil |
| 412 | } else { |
| 413 | log.Printf("ERROR: Failed to insert workspace into database: %q\n", err) |
| 414 | } |
| 415 | } |
| 416 | return "", fmt.Errorf("Failed to create a new workspace") |
| 417 | } |
| 418 | |
| 419 | // getCode returns the code for a given hash, or the empty string if not found. |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 420 | func getCode(hash string) (string, error) { |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 421 | code := "" |
| 422 | if err := db.QueryRow("SELECT code FROM webtry WHERE hash=?", hash).Scan(&code); err != nil { |
| 423 | log.Printf("ERROR: Code for hash is missing: %q\n", err) |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 424 | return code, err |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 425 | } |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 426 | return code, nil |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | func workspaceHandler(w http.ResponseWriter, r *http.Request) { |
| 430 | log.Printf("Workspace Handler: %q\n", r.URL.Path) |
| 431 | if r.Method == "GET" { |
| 432 | tries := []Try{} |
| 433 | match := workspaceLink.FindStringSubmatch(r.URL.Path) |
| 434 | name := "" |
| 435 | if len(match) == 2 { |
| 436 | name = match[1] |
commit-bot@chromium.org | c3b738a | 2014-04-21 17:36:44 +0000 | [diff] [blame] | 437 | rows, err := db.Query("SELECT create_ts, hash FROM workspacetry WHERE name=? ORDER BY create_ts", name) |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 438 | if err != nil { |
| 439 | reportError(w, r, err, "Failed to select.") |
| 440 | return |
| 441 | } |
| 442 | for rows.Next() { |
| 443 | var hash string |
| 444 | var create_ts time.Time |
| 445 | if err := rows.Scan(&create_ts, &hash); err != nil { |
| 446 | log.Printf("Error: failed to fetch from database: %q", err) |
| 447 | continue |
| 448 | } |
| 449 | tries = append(tries, Try{Hash: hash, CreateTS: create_ts.Format("2006-02-01")}) |
| 450 | } |
| 451 | } |
| 452 | var code string |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 453 | var hash string |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 454 | if len(tries) == 0 { |
| 455 | code = DEFAULT_SAMPLE |
| 456 | } else { |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 457 | hash = tries[len(tries)-1].Hash |
| 458 | code, _ = getCode(hash) |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 459 | } |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 460 | if err := workspaceTemplate.Execute(w, Workspace{Tries: tries, Code: code, Name: name, Hash: hash}); err != nil { |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 461 | log.Printf("ERROR: Failed to expand template: %q\n", err) |
| 462 | } |
| 463 | } else if r.Method == "POST" { |
| 464 | name, err := newWorkspace() |
| 465 | if err != nil { |
| 466 | http.Error(w, "Failed to create a new workspace.", 500) |
| 467 | return |
| 468 | } |
| 469 | http.Redirect(w, r, "/w/"+name, 302) |
| 470 | } |
| 471 | } |
| 472 | |
commit-bot@chromium.org | 4bd8fdc | 2014-04-15 00:43:51 +0000 | [diff] [blame] | 473 | // hasPreProcessor returns true if any line in the code begins with a # char. |
| 474 | func hasPreProcessor(code string) bool { |
| 475 | lines := strings.Split(code, "\n") |
| 476 | for _, s := range lines { |
| 477 | if strings.HasPrefix(strings.TrimSpace(s), "#") { |
| 478 | return true |
| 479 | } |
| 480 | } |
| 481 | return false |
| 482 | } |
| 483 | |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 484 | type TryRequest struct { |
| 485 | Code string `json:"code"` |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 486 | Name string `json:"name"` // Optional name of the workspace the code is in. |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 487 | } |
| 488 | |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 489 | // iframeHandler handles the GET and POST of the main page. |
| 490 | func iframeHandler(w http.ResponseWriter, r *http.Request) { |
| 491 | log.Printf("IFrame Handler: %q\n", r.URL.Path) |
| 492 | if r.Method != "GET" { |
| 493 | http.NotFound(w, r) |
| 494 | return |
| 495 | } |
| 496 | match := iframeLink.FindStringSubmatch(r.URL.Path) |
| 497 | if len(match) != 2 { |
| 498 | http.NotFound(w, r) |
| 499 | return |
| 500 | } |
| 501 | hash := match[1] |
| 502 | if db == nil { |
| 503 | http.NotFound(w, r) |
| 504 | return |
| 505 | } |
| 506 | var code string |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 507 | code, err := getCode(hash) |
| 508 | if err != nil { |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 509 | http.NotFound(w, r) |
| 510 | return |
| 511 | } |
| 512 | // Expand the template. |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 513 | if err := iframeTemplate.Execute(w, userCode{Code: code, Hash: hash}); err != nil { |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 514 | log.Printf("ERROR: Failed to expand template: %q\n", err) |
| 515 | } |
| 516 | } |
| 517 | |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 518 | type TryInfo struct { |
| 519 | Hash string `json:"hash"` |
| 520 | Code string `json:"code"` |
| 521 | } |
| 522 | |
| 523 | // tryInfoHandler returns information about a specific try. |
| 524 | func tryInfoHandler(w http.ResponseWriter, r *http.Request) { |
| 525 | log.Printf("Try Info Handler: %q\n", r.URL.Path) |
| 526 | if r.Method != "GET" { |
| 527 | http.NotFound(w, r) |
| 528 | return |
| 529 | } |
| 530 | match := tryInfoLink.FindStringSubmatch(r.URL.Path) |
| 531 | if len(match) != 2 { |
| 532 | http.NotFound(w, r) |
| 533 | return |
| 534 | } |
| 535 | hash := match[1] |
| 536 | code, err := getCode(hash) |
| 537 | if err != nil { |
| 538 | http.NotFound(w, r) |
| 539 | return |
| 540 | } |
| 541 | m := TryInfo{ |
| 542 | Hash: hash, |
| 543 | Code: code, |
| 544 | } |
| 545 | resp, err := json.Marshal(m) |
| 546 | if err != nil { |
| 547 | reportError(w, r, err, "Failed to serialize a response.") |
| 548 | return |
| 549 | } |
| 550 | w.Header().Set("Content-Type", "application/json") |
| 551 | w.Write(resp) |
| 552 | } |
| 553 | |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 554 | func cleanCompileOutput(s, hash string) string { |
| 555 | old := "../../../cache/" + hash + ".cpp:" |
| 556 | log.Printf("INFO: replacing %q\n", old) |
| 557 | return strings.Replace(s, old, "usercode.cpp:", -1) |
| 558 | } |
| 559 | |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 560 | // mainHandler handles the GET and POST of the main page. |
| 561 | func mainHandler(w http.ResponseWriter, r *http.Request) { |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 562 | log.Printf("Main Handler: %q\n", r.URL.Path) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 563 | if r.Method == "GET" { |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 564 | code := DEFAULT_SAMPLE |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 565 | match := directLink.FindStringSubmatch(r.URL.Path) |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 566 | var hash string |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 567 | if len(match) == 2 && r.URL.Path != "/" { |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 568 | hash = match[1] |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 569 | if db == nil { |
| 570 | http.NotFound(w, r) |
| 571 | return |
| 572 | } |
| 573 | // Update 'code' with the code found in the database. |
| 574 | if err := db.QueryRow("SELECT code FROM webtry WHERE hash=?", hash).Scan(&code); err != nil { |
| 575 | http.NotFound(w, r) |
| 576 | return |
| 577 | } |
| 578 | } |
| 579 | // Expand the template. |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 580 | if err := indexTemplate.Execute(w, userCode{Code: code, Hash: hash}); err != nil { |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 581 | log.Printf("ERROR: Failed to expand template: %q\n", err) |
| 582 | } |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 583 | } else if r.Method == "POST" { |
| 584 | w.Header().Set("Content-Type", "application/json") |
commit-bot@chromium.org | 4bd8fdc | 2014-04-15 00:43:51 +0000 | [diff] [blame] | 585 | buf := bytes.NewBuffer(make([]byte, 0, MAX_TRY_SIZE)) |
| 586 | n, err := buf.ReadFrom(r.Body) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 587 | if err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 588 | reportTryError(w, r, err, "Failed to read a request body.", "") |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 589 | return |
| 590 | } |
commit-bot@chromium.org | 4bd8fdc | 2014-04-15 00:43:51 +0000 | [diff] [blame] | 591 | if n == MAX_TRY_SIZE { |
| 592 | err := fmt.Errorf("Code length equal to, or exceeded, %d", MAX_TRY_SIZE) |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 593 | reportTryError(w, r, err, "Code too large.", "") |
commit-bot@chromium.org | 4bd8fdc | 2014-04-15 00:43:51 +0000 | [diff] [blame] | 594 | return |
| 595 | } |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 596 | request := TryRequest{} |
| 597 | if err := json.Unmarshal(buf.Bytes(), &request); err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 598 | reportTryError(w, r, err, "Coulnd't decode JSON.", "") |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 599 | return |
| 600 | } |
| 601 | if hasPreProcessor(request.Code) { |
commit-bot@chromium.org | 4bd8fdc | 2014-04-15 00:43:51 +0000 | [diff] [blame] | 602 | err := fmt.Errorf("Found preprocessor macro in code.") |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 603 | reportTryError(w, r, err, "Preprocessor macros aren't allowed.", "") |
commit-bot@chromium.org | 4bd8fdc | 2014-04-15 00:43:51 +0000 | [diff] [blame] | 604 | return |
| 605 | } |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 606 | hash, err := expandCode(LineNumbers(request.Code)) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 607 | if err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 608 | reportTryError(w, r, err, "Failed to write the code to compile.", hash) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 609 | return |
| 610 | } |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 611 | writeToDatabase(hash, request.Code, request.Name) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 612 | message, err := doCmd(fmt.Sprintf(RESULT_COMPILE, hash, hash), true) |
| 613 | if err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 614 | message = cleanCompileOutput(message, hash) |
| 615 | reportTryError(w, r, err, message, hash) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 616 | return |
| 617 | } |
| 618 | linkMessage, err := doCmd(fmt.Sprintf(LINK, hash, hash), true) |
| 619 | if err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 620 | linkMessage = cleanCompileOutput(linkMessage, hash) |
| 621 | reportTryError(w, r, err, linkMessage, hash) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 622 | return |
| 623 | } |
| 624 | message += linkMessage |
| 625 | cmd := hash + " --out " + hash + ".png" |
| 626 | if *useChroot { |
| 627 | cmd = "schroot -c webtry --directory=/inout -- /inout/" + cmd |
| 628 | } else { |
| 629 | abs, err := filepath.Abs("../../../inout") |
| 630 | if err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 631 | reportTryError(w, r, err, "Failed to find executable directory.", hash) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 632 | return |
| 633 | } |
| 634 | cmd = abs + "/" + cmd |
| 635 | } |
| 636 | |
| 637 | execMessage, err := doCmd(cmd, false) |
| 638 | if err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 639 | reportTryError(w, r, err, "Failed to run the code:\n"+execMessage, hash) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 640 | return |
| 641 | } |
| 642 | png, err := ioutil.ReadFile("../../../inout/" + hash + ".png") |
| 643 | if err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 644 | reportTryError(w, r, err, "Failed to open the generated PNG.", hash) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 645 | return |
| 646 | } |
| 647 | |
| 648 | m := response{ |
| 649 | Message: message, |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 650 | StdOut: execMessage, |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 651 | Img: base64.StdEncoding.EncodeToString([]byte(png)), |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 652 | Hash: hash, |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 653 | } |
| 654 | resp, err := json.Marshal(m) |
| 655 | if err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame^] | 656 | reportTryError(w, r, err, "Failed to serialize a response.", hash) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 657 | return |
| 658 | } |
| 659 | w.Write(resp) |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | func main() { |
| 664 | flag.Parse() |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 665 | http.HandleFunc("/i/", imageHandler) |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 666 | http.HandleFunc("/w/", workspaceHandler) |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 667 | http.HandleFunc("/recent/", recentHandler) |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 668 | http.HandleFunc("/iframe/", iframeHandler) |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 669 | http.HandleFunc("/json/", tryInfoHandler) |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 670 | http.HandleFunc("/css/", cssHandler) |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 671 | http.HandleFunc("/js/", jsHandler) |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 672 | // TODO Break out /c/ as it's own handler. |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 673 | http.HandleFunc("/", mainHandler) |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 674 | log.Fatal(http.ListenAndServe(*port, nil)) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 675 | } |