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" |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 8 | "encoding/binary" |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 9 | "encoding/json" |
| 10 | "flag" |
| 11 | "fmt" |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 12 | htemplate "html/template" |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 13 | "image" |
| 14 | _ "image/gif" |
| 15 | _ "image/jpeg" |
| 16 | "image/png" |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 17 | "io/ioutil" |
| 18 | "log" |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 19 | "math/rand" |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 20 | "net/http" |
| 21 | "os" |
| 22 | "os/exec" |
| 23 | "path/filepath" |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 24 | "regexp" |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 25 | "strings" |
| 26 | "text/template" |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 27 | "time" |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 28 | ) |
| 29 | |
commit-bot@chromium.org | a4c3677 | 2014-05-19 17:38:52 +0000 | [diff] [blame] | 30 | import ( |
| 31 | "github.com/fiorix/go-web/autogzip" |
| 32 | _ "github.com/go-sql-driver/mysql" |
| 33 | _ "github.com/mattn/go-sqlite3" |
| 34 | ) |
| 35 | |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 36 | const ( |
commit-bot@chromium.org | 89126a6 | 2014-04-30 19:38:51 +0000 | [diff] [blame] | 37 | RESULT_COMPILE = `../../experimental/webtry/safec++ -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 -m64 -fno-rtti -Wnon-virtual-dtor -c ../../../cache/%s.cpp -o ../../../cache/%s.o` |
| 38 | LINK = `../../experimental/webtry/safec++ -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` |
fmalita@google.com | 950306c | 2014-05-01 15:14:56 +0000 | [diff] [blame] | 39 | DEFAULT_SAMPLE = `void draw(SkCanvas* canvas) { |
| 40 | SkPaint p; |
| 41 | p.setColor(SK_ColorRED); |
| 42 | p.setAntiAlias(true); |
| 43 | p.setStyle(SkPaint::kStroke_Style); |
| 44 | p.setStrokeWidth(10); |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 45 | |
fmalita@google.com | 950306c | 2014-05-01 15:14:56 +0000 | [diff] [blame] | 46 | canvas->drawLine(20, 20, 100, 100, p); |
| 47 | }` |
commit-bot@chromium.org | 4bd8fdc | 2014-04-15 00:43:51 +0000 | [diff] [blame] | 48 | // Don't increase above 2^16 w/o altering the db tables to accept something bigger than TEXT. |
| 49 | MAX_TRY_SIZE = 64000 |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 50 | ) |
| 51 | |
| 52 | var ( |
| 53 | // codeTemplate is the cpp code template the user's code is copied into. |
| 54 | codeTemplate *template.Template = nil |
| 55 | |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 56 | // indexTemplate is the main index.html page we serve. |
| 57 | indexTemplate *htemplate.Template = nil |
| 58 | |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 59 | // iframeTemplate is the main index.html page we serve. |
| 60 | iframeTemplate *htemplate.Template = nil |
| 61 | |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 62 | // recentTemplate is a list of recent images. |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 63 | recentTemplate *htemplate.Template = nil |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 64 | |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 65 | // workspaceTemplate is the page for workspaces, a series of webtrys. |
| 66 | workspaceTemplate *htemplate.Template = nil |
| 67 | |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 68 | // db is the database, nil if we don't have an SQL database to store data into. |
| 69 | db *sql.DB = nil |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 70 | |
| 71 | // 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] | 72 | directLink = regexp.MustCompile("^/c/([a-f0-9]+)$") |
| 73 | |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 74 | // iframeLink is the regex that matches URLs paths that are links to iframes. |
| 75 | iframeLink = regexp.MustCompile("^/iframe/([a-f0-9]+)$") |
| 76 | |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 77 | // imageLink is the regex that matches URLs paths that are direct links to PNGs. |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 78 | imageLink = regexp.MustCompile("^/i/([a-z0-9-]+.png)$") |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 79 | |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 80 | // tryInfoLink is the regex that matches URLs paths that are direct links to data about a single try. |
| 81 | tryInfoLink = regexp.MustCompile("^/json/([a-f0-9]+)$") |
| 82 | |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 83 | // workspaceLink is the regex that matches URLs paths for workspaces. |
| 84 | workspaceLink = regexp.MustCompile("^/w/([a-z0-9-]+)$") |
| 85 | |
| 86 | // workspaceNameAdj is a list of adjectives for building workspace names. |
| 87 | workspaceNameAdj = []string{ |
| 88 | "autumn", "hidden", "bitter", "misty", "silent", "empty", "dry", "dark", |
| 89 | "summer", "icy", "delicate", "quiet", "white", "cool", "spring", "winter", |
| 90 | "patient", "twilight", "dawn", "crimson", "wispy", "weathered", "blue", |
| 91 | "billowing", "broken", "cold", "damp", "falling", "frosty", "green", |
| 92 | "long", "late", "lingering", "bold", "little", "morning", "muddy", "old", |
| 93 | "red", "rough", "still", "small", "sparkling", "throbbing", "shy", |
| 94 | "wandering", "withered", "wild", "black", "young", "holy", "solitary", |
| 95 | "fragrant", "aged", "snowy", "proud", "floral", "restless", "divine", |
| 96 | "polished", "ancient", "purple", "lively", "nameless", |
| 97 | } |
| 98 | |
| 99 | // workspaceNameNoun is a list of nouns for building workspace names. |
| 100 | workspaceNameNoun = []string{ |
| 101 | "waterfall", "river", "breeze", "moon", "rain", "wind", "sea", "morning", |
| 102 | "snow", "lake", "sunset", "pine", "shadow", "leaf", "dawn", "glitter", |
| 103 | "forest", "hill", "cloud", "meadow", "sun", "glade", "bird", "brook", |
| 104 | "butterfly", "bush", "dew", "dust", "field", "fire", "flower", "firefly", |
| 105 | "feather", "grass", "haze", "mountain", "night", "pond", "darkness", |
| 106 | "snowflake", "silence", "sound", "sky", "shape", "surf", "thunder", |
| 107 | "violet", "water", "wildflower", "wave", "water", "resonance", "sun", |
| 108 | "wood", "dream", "cherry", "tree", "fog", "frost", "voice", "paper", |
| 109 | "frog", "smoke", "star", |
| 110 | } |
commit-bot@chromium.org | 472f830 | 2014-04-28 15:33:31 +0000 | [diff] [blame] | 111 | |
| 112 | gitHash = "" |
| 113 | gitInfo = "" |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 114 | ) |
| 115 | |
| 116 | // flags |
| 117 | var ( |
| 118 | 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] | 119 | 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] | 120 | ) |
| 121 | |
| 122 | // lineNumbers adds #line numbering to the user's code. |
| 123 | func LineNumbers(c string) string { |
| 124 | lines := strings.Split(c, "\n") |
| 125 | ret := []string{} |
| 126 | for i, line := range lines { |
| 127 | ret = append(ret, fmt.Sprintf("#line %d", i+1)) |
| 128 | ret = append(ret, line) |
| 129 | } |
| 130 | return strings.Join(ret, "\n") |
| 131 | } |
| 132 | |
| 133 | func init() { |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 134 | rand.Seed(time.Now().UnixNano()) |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 135 | |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 136 | // Change the current working directory to the directory of the executable. |
| 137 | var err error |
| 138 | cwd, err := filepath.Abs(filepath.Dir(os.Args[0])) |
| 139 | if err != nil { |
| 140 | log.Fatal(err) |
| 141 | } |
| 142 | os.Chdir(cwd) |
| 143 | |
| 144 | codeTemplate, err = template.ParseFiles(filepath.Join(cwd, "templates/template.cpp")) |
| 145 | if err != nil { |
| 146 | panic(err) |
| 147 | } |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 148 | indexTemplate, err = htemplate.ParseFiles( |
| 149 | filepath.Join(cwd, "templates/index.html"), |
| 150 | filepath.Join(cwd, "templates/titlebar.html"), |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 151 | filepath.Join(cwd, "templates/content.html"), |
commit-bot@chromium.org | 015c08e | 2014-05-19 16:40:08 +0000 | [diff] [blame] | 152 | filepath.Join(cwd, "templates/headercommon.html"), |
| 153 | filepath.Join(cwd, "templates/footercommon.html"), |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 154 | ) |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 155 | if err != nil { |
| 156 | panic(err) |
| 157 | } |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 158 | iframeTemplate, err = htemplate.ParseFiles( |
| 159 | filepath.Join(cwd, "templates/iframe.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 | 015c08e | 2014-05-19 16:40:08 +0000 | [diff] [blame] | 161 | filepath.Join(cwd, "templates/headercommon.html"), |
| 162 | filepath.Join(cwd, "templates/footercommon.html"), |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 163 | ) |
| 164 | if err != nil { |
| 165 | panic(err) |
| 166 | } |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 167 | recentTemplate, err = htemplate.ParseFiles( |
| 168 | filepath.Join(cwd, "templates/recent.html"), |
| 169 | filepath.Join(cwd, "templates/titlebar.html"), |
commit-bot@chromium.org | 015c08e | 2014-05-19 16:40:08 +0000 | [diff] [blame] | 170 | filepath.Join(cwd, "templates/headercommon.html"), |
| 171 | filepath.Join(cwd, "templates/footercommon.html"), |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 172 | ) |
| 173 | if err != nil { |
| 174 | panic(err) |
| 175 | } |
| 176 | workspaceTemplate, err = htemplate.ParseFiles( |
| 177 | filepath.Join(cwd, "templates/workspace.html"), |
| 178 | filepath.Join(cwd, "templates/titlebar.html"), |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 179 | filepath.Join(cwd, "templates/content.html"), |
commit-bot@chromium.org | 015c08e | 2014-05-19 16:40:08 +0000 | [diff] [blame] | 180 | filepath.Join(cwd, "templates/headercommon.html"), |
| 181 | filepath.Join(cwd, "templates/footercommon.html"), |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 182 | ) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 183 | if err != nil { |
| 184 | panic(err) |
| 185 | } |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 186 | |
commit-bot@chromium.org | 472f830 | 2014-04-28 15:33:31 +0000 | [diff] [blame] | 187 | // The git command returns output of the format: |
| 188 | // |
| 189 | // f672cead70404080a991ebfb86c38316a4589b23 2014-04-27 19:21:51 +0000 |
| 190 | // |
| 191 | logOutput, err := doCmd(`git log --format=%H%x20%ai HEAD^..HEAD`, true) |
| 192 | if err != nil { |
| 193 | panic(err) |
| 194 | } |
| 195 | logInfo := strings.Split(logOutput, " ") |
| 196 | gitHash = logInfo[0] |
| 197 | gitInfo = logInfo[1] + " " + logInfo[2] + " " + logInfo[0][0:6] |
| 198 | |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 199 | // Connect to MySQL server. First, get the password from the metadata server. |
| 200 | // See https://developers.google.com/compute/docs/metadata#custom. |
| 201 | req, err := http.NewRequest("GET", "http://metadata/computeMetadata/v1/instance/attributes/password", nil) |
| 202 | if err != nil { |
| 203 | panic(err) |
| 204 | } |
| 205 | client := http.Client{} |
| 206 | req.Header.Add("X-Google-Metadata-Request", "True") |
| 207 | if resp, err := client.Do(req); err == nil { |
| 208 | password, err := ioutil.ReadAll(resp.Body) |
| 209 | if err != nil { |
| 210 | log.Printf("ERROR: Failed to read password from metadata server: %q\n", err) |
| 211 | panic(err) |
| 212 | } |
| 213 | // The IP address of the database is found here: |
| 214 | // https://console.developers.google.com/project/31977622648/sql/instances/webtry/overview |
| 215 | // And 3306 is the default port for MySQL. |
commit-bot@chromium.org | 4bd8fdc | 2014-04-15 00:43:51 +0000 | [diff] [blame] | 216 | 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] | 217 | if err != nil { |
| 218 | log.Printf("ERROR: Failed to open connection to SQL server: %q\n", err) |
| 219 | panic(err) |
| 220 | } |
| 221 | } else { |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 222 | 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] | 223 | // Fallback to sqlite for local use. |
| 224 | db, err = sql.Open("sqlite3", "./webtry.db") |
| 225 | if err != nil { |
| 226 | log.Printf("ERROR: Failed to open: %q\n", err) |
| 227 | panic(err) |
| 228 | } |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 229 | sql := `CREATE TABLE source_images ( |
| 230 | id INTEGER PRIMARY KEY NOT NULL, |
| 231 | image MEDIUMBLOB DEFAULT '' NOT NULL, -- formatted as a PNG. |
| 232 | width INTEGER DEFAULT 0 NOT NULL, |
| 233 | height INTEGER DEFAULT 0 NOT NULL, |
| 234 | create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 235 | hidden INTEGER DEFAULT 0 NOT NULL |
| 236 | )` |
| 237 | _, err = db.Exec(sql) |
| 238 | log.Printf("Info: status creating sqlite table for sources: %q\n", err) |
| 239 | |
| 240 | sql = `CREATE TABLE webtry ( |
| 241 | code TEXT DEFAULT '' NOT NULL, |
| 242 | create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 243 | hash CHAR(64) DEFAULT '' NOT NULL, |
| 244 | source_image_id INTEGER DEFAULT 0 NOT NULL, |
| 245 | |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 246 | PRIMARY KEY(hash) |
| 247 | )` |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 248 | _, err = db.Exec(sql) |
| 249 | log.Printf("Info: status creating sqlite table for webtry: %q\n", err) |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 250 | |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 251 | sql = `CREATE TABLE workspace ( |
| 252 | name CHAR(64) DEFAULT '' NOT NULL, |
| 253 | create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 254 | PRIMARY KEY(name) |
| 255 | )` |
| 256 | _, err = db.Exec(sql) |
| 257 | log.Printf("Info: status creating sqlite table for workspace: %q\n", err) |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 258 | |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 259 | sql = `CREATE TABLE workspacetry ( |
| 260 | name CHAR(64) DEFAULT '' NOT NULL, |
| 261 | create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 262 | hash CHAR(64) DEFAULT '' NOT NULL, |
| 263 | hidden INTEGER DEFAULT 0 NOT NULL, |
| 264 | source_image_id INTEGER DEFAULT 0 NOT NULL, |
| 265 | |
| 266 | FOREIGN KEY (name) REFERENCES workspace(name) |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 267 | )` |
| 268 | _, err = db.Exec(sql) |
| 269 | 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] | 270 | } |
commit-bot@chromium.org | 9f3b925 | 2014-05-14 12:55:34 +0000 | [diff] [blame] | 271 | |
| 272 | // Ping the database to keep the connection fresh. |
| 273 | go func() { |
| 274 | c := time.Tick(1 * time.Minute) |
| 275 | for _ = range c { |
| 276 | if err := db.Ping(); err != nil { |
| 277 | log.Printf("ERROR: Database failed to respond: %q\n", err) |
| 278 | } |
| 279 | } |
| 280 | }() |
| 281 | |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 282 | writeOutAllSourceImages() |
| 283 | } |
| 284 | |
| 285 | func writeOutAllSourceImages() { |
| 286 | // Pull all the source images from the db and write them out to inout. |
| 287 | rows, err := db.Query("SELECT id, image, create_ts FROM source_images ORDER BY create_ts DESC") |
| 288 | |
| 289 | if err != nil { |
| 290 | log.Printf("ERROR: Failed to open connection to SQL server: %q\n", err) |
| 291 | panic(err) |
| 292 | } |
| 293 | for rows.Next() { |
| 294 | var id int |
| 295 | var image []byte |
| 296 | var create_ts time.Time |
| 297 | if err := rows.Scan(&id, &image, &create_ts); err != nil { |
| 298 | log.Printf("Error: failed to fetch from database: %q", err) |
| 299 | continue |
| 300 | } |
| 301 | filename := fmt.Sprintf("../../../inout/image-%d.png", id) |
| 302 | if _, err := os.Stat(filename); os.IsExist(err) { |
| 303 | log.Printf("Skipping write since file exists: %q", filename) |
| 304 | continue |
| 305 | } |
| 306 | if err := ioutil.WriteFile(filename, image, 0666); err != nil { |
| 307 | log.Printf("Error: failed to write image file: %q", err) |
| 308 | } |
| 309 | } |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 310 | } |
| 311 | |
commit-bot@chromium.org | 472f830 | 2014-04-28 15:33:31 +0000 | [diff] [blame] | 312 | // Titlebar is used in titlebar template expansion. |
| 313 | type Titlebar struct { |
| 314 | GitHash string |
| 315 | GitInfo string |
| 316 | } |
| 317 | |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 318 | // userCode is used in template expansion. |
| 319 | type userCode struct { |
commit-bot@chromium.org | 472f830 | 2014-04-28 15:33:31 +0000 | [diff] [blame] | 320 | Code string |
| 321 | Hash string |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 322 | Source int |
commit-bot@chromium.org | 472f830 | 2014-04-28 15:33:31 +0000 | [diff] [blame] | 323 | Titlebar Titlebar |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | // expandToFile expands the template and writes the result to the file. |
| 327 | func expandToFile(filename string, code string, t *template.Template) error { |
| 328 | f, err := os.Create(filename) |
| 329 | if err != nil { |
| 330 | return err |
| 331 | } |
| 332 | defer f.Close() |
commit-bot@chromium.org | 472f830 | 2014-04-28 15:33:31 +0000 | [diff] [blame] | 333 | return t.Execute(f, userCode{Code: code, Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo}}) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 334 | } |
| 335 | |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 336 | // expandCode expands the template into a file and calculates the MD5 hash. |
| 337 | func expandCode(code string, source int) (string, error) { |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 338 | h := md5.New() |
| 339 | h.Write([]byte(code)) |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 340 | binary.Write(h, binary.LittleEndian, int64(source)) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 341 | hash := fmt.Sprintf("%x", h.Sum(nil)) |
| 342 | // At this point we are running in skia/experimental/webtry, making cache a |
| 343 | // peer directory to skia. |
| 344 | // TODO(jcgregorio) Make all relative directories into flags. |
| 345 | err := expandToFile(fmt.Sprintf("../../../cache/%s.cpp", hash), code, codeTemplate) |
| 346 | return hash, err |
| 347 | } |
| 348 | |
| 349 | // response is serialized to JSON as a response to POSTs. |
| 350 | type response struct { |
| 351 | Message string `json:"message"` |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 352 | StdOut string `json:"stdout"` |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 353 | Img string `json:"img"` |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 354 | Hash string `json:"hash"` |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | // doCmd executes the given command line string in either the out/Debug |
commit-bot@chromium.org | 15b2981 | 2014-04-28 14:56:32 +0000 | [diff] [blame] | 358 | // directory or the inout directory. Returns the stdout and stderr. |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 359 | func doCmd(commandLine string, moveToDebug bool) (string, error) { |
| 360 | log.Printf("Command: %q\n", commandLine) |
| 361 | programAndArgs := strings.SplitN(commandLine, " ", 2) |
| 362 | program := programAndArgs[0] |
| 363 | args := []string{} |
| 364 | if len(programAndArgs) > 1 { |
| 365 | args = strings.Split(programAndArgs[1], " ") |
| 366 | } |
| 367 | cmd := exec.Command(program, args...) |
| 368 | abs, err := filepath.Abs("../../out/Debug") |
| 369 | if err != nil { |
| 370 | return "", fmt.Errorf("Failed to find absolute path to Debug directory.") |
| 371 | } |
| 372 | if moveToDebug { |
| 373 | cmd.Dir = abs |
| 374 | } else if !*useChroot { // Don't set cmd.Dir when using chroot. |
| 375 | abs, err := filepath.Abs("../../../inout") |
| 376 | if err != nil { |
| 377 | return "", fmt.Errorf("Failed to find absolute path to inout directory.") |
| 378 | } |
| 379 | cmd.Dir = abs |
| 380 | } |
| 381 | log.Printf("Run in directory: %q\n", cmd.Dir) |
commit-bot@chromium.org | 15b2981 | 2014-04-28 14:56:32 +0000 | [diff] [blame] | 382 | message, err := cmd.CombinedOutput() |
| 383 | log.Printf("StdOut + StdErr: %s\n", string(message)) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 384 | if err != nil { |
| 385 | log.Printf("Exit status: %s\n", err.Error()) |
commit-bot@chromium.org | 15b2981 | 2014-04-28 14:56:32 +0000 | [diff] [blame] | 386 | return string(message), fmt.Errorf("Failed to run command.") |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 387 | } |
commit-bot@chromium.org | 15b2981 | 2014-04-28 14:56:32 +0000 | [diff] [blame] | 388 | return string(message), nil |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | // reportError formats an HTTP error response and also logs the detailed error message. |
| 392 | 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] | 393 | log.Printf("Error: %s\n%s", message, err.Error()) |
commit-bot@chromium.org | a4c3677 | 2014-05-19 17:38:52 +0000 | [diff] [blame] | 394 | w.Header().Set("Content-Type", "text/plain") |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 395 | http.Error(w, message, 500) |
| 396 | } |
| 397 | |
| 398 | // reportTryError formats an HTTP error response in JSON and also logs the detailed error message. |
| 399 | 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] | 400 | m := response{ |
| 401 | Message: message, |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 402 | Hash: hash, |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 403 | } |
| 404 | log.Printf("Error: %s\n%s", message, err.Error()) |
| 405 | resp, err := json.Marshal(m) |
| 406 | if err != nil { |
| 407 | http.Error(w, "Failed to serialize a response", 500) |
| 408 | return |
| 409 | } |
commit-bot@chromium.org | a4c3677 | 2014-05-19 17:38:52 +0000 | [diff] [blame] | 410 | w.Header().Set("Content-Type", "text/plain") |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 411 | w.Write(resp) |
| 412 | } |
| 413 | |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 414 | func writeToDatabase(hash string, code string, workspaceName string, source int) { |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 415 | if db == nil { |
| 416 | return |
| 417 | } |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 418 | if _, err := db.Exec("INSERT INTO webtry (code, hash, source_image_id) VALUES(?, ?, ?)", code, hash, source); err != nil { |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 419 | log.Printf("ERROR: Failed to insert code into database: %q\n", err) |
| 420 | } |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 421 | if workspaceName != "" { |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 422 | if _, err := db.Exec("INSERT INTO workspacetry (name, hash, source_image_id) VALUES(?, ?, ?)", workspaceName, hash, source); err != nil { |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 423 | log.Printf("ERROR: Failed to insert into workspacetry table: %q\n", err) |
| 424 | } |
| 425 | } |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 426 | } |
| 427 | |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 428 | type Sources struct { |
| 429 | Id int `json:"id"` |
| 430 | } |
| 431 | |
| 432 | // sourcesHandler serves up the PNG of a specific try. |
| 433 | func sourcesHandler(w http.ResponseWriter, r *http.Request) { |
| 434 | log.Printf("Sources Handler: %q\n", r.URL.Path) |
| 435 | if r.Method == "GET" { |
| 436 | rows, err := db.Query("SELECT id, create_ts FROM source_images WHERE hidden=0 ORDER BY create_ts DESC") |
| 437 | |
| 438 | if err != nil { |
| 439 | http.Error(w, fmt.Sprintf("Failed to query sources: %s.", err), 500) |
| 440 | } |
| 441 | sources := make([]Sources, 0, 0) |
| 442 | for rows.Next() { |
| 443 | var id int |
| 444 | var create_ts time.Time |
| 445 | if err := rows.Scan(&id, &create_ts); err != nil { |
| 446 | log.Printf("Error: failed to fetch from database: %q", err) |
| 447 | continue |
| 448 | } |
| 449 | sources = append(sources, Sources{Id: id}) |
| 450 | } |
| 451 | |
| 452 | resp, err := json.Marshal(sources) |
| 453 | if err != nil { |
| 454 | reportError(w, r, err, "Failed to serialize a response.") |
| 455 | return |
| 456 | } |
| 457 | w.Header().Set("Content-Type", "application/json") |
| 458 | w.Write(resp) |
| 459 | |
| 460 | } else if r.Method == "POST" { |
| 461 | if err := r.ParseMultipartForm(1000000); err != nil { |
| 462 | http.Error(w, fmt.Sprintf("Failed to load image: %s.", err), 500) |
| 463 | return |
| 464 | } |
| 465 | if _, ok := r.MultipartForm.File["upload"]; !ok { |
| 466 | http.Error(w, "Invalid upload.", 500) |
| 467 | return |
| 468 | } |
| 469 | if len(r.MultipartForm.File["upload"]) != 1 { |
| 470 | http.Error(w, "Wrong number of uploads.", 500) |
| 471 | return |
| 472 | } |
| 473 | f, err := r.MultipartForm.File["upload"][0].Open() |
| 474 | if err != nil { |
| 475 | http.Error(w, fmt.Sprintf("Failed to load image: %s.", err), 500) |
| 476 | return |
| 477 | } |
| 478 | defer f.Close() |
| 479 | m, _, err := image.Decode(f) |
| 480 | if err != nil { |
| 481 | http.Error(w, fmt.Sprintf("Failed to decode image: %s.", err), 500) |
| 482 | return |
| 483 | } |
| 484 | var b bytes.Buffer |
| 485 | png.Encode(&b, m) |
| 486 | bounds := m.Bounds() |
| 487 | width := bounds.Max.Y - bounds.Min.Y |
| 488 | height := bounds.Max.X - bounds.Min.X |
| 489 | if _, err := db.Exec("INSERT INTO source_images (image, width, height) VALUES(?, ?, ?)", b.Bytes(), width, height); err != nil { |
| 490 | log.Printf("ERROR: Failed to insert sources into database: %q\n", err) |
| 491 | http.Error(w, fmt.Sprintf("Failed to store image: %s.", err), 500) |
| 492 | return |
| 493 | } |
| 494 | go writeOutAllSourceImages() |
| 495 | |
| 496 | // Now redirect back to where we came from. |
| 497 | http.Redirect(w, r, r.Referer(), 302) |
| 498 | } else { |
| 499 | http.NotFound(w, r) |
| 500 | return |
| 501 | } |
| 502 | } |
| 503 | |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 504 | // imageHandler serves up the PNG of a specific try. |
| 505 | func imageHandler(w http.ResponseWriter, r *http.Request) { |
| 506 | log.Printf("Image Handler: %q\n", r.URL.Path) |
| 507 | if r.Method != "GET" { |
| 508 | http.NotFound(w, r) |
| 509 | return |
| 510 | } |
| 511 | match := imageLink.FindStringSubmatch(r.URL.Path) |
| 512 | if len(match) != 2 { |
| 513 | http.NotFound(w, r) |
| 514 | return |
| 515 | } |
| 516 | filename := match[1] |
commit-bot@chromium.org | a4c3677 | 2014-05-19 17:38:52 +0000 | [diff] [blame] | 517 | w.Header().Set("Content-Type", "image/png") |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 518 | http.ServeFile(w, r, fmt.Sprintf("../../../inout/%s", filename)) |
| 519 | } |
| 520 | |
| 521 | type Try struct { |
commit-bot@chromium.org | c3b738a | 2014-04-21 17:36:44 +0000 | [diff] [blame] | 522 | Hash string `json:"hash"` |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 523 | Source int |
commit-bot@chromium.org | c3b738a | 2014-04-21 17:36:44 +0000 | [diff] [blame] | 524 | CreateTS string `json:"create_ts"` |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | type Recent struct { |
commit-bot@chromium.org | 472f830 | 2014-04-28 15:33:31 +0000 | [diff] [blame] | 528 | Tries []Try |
| 529 | Titlebar Titlebar |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | // recentHandler shows the last 20 tries. |
| 533 | func recentHandler(w http.ResponseWriter, r *http.Request) { |
| 534 | log.Printf("Recent Handler: %q\n", r.URL.Path) |
| 535 | |
| 536 | var err error |
| 537 | rows, err := db.Query("SELECT create_ts, hash FROM webtry ORDER BY create_ts DESC LIMIT 20") |
| 538 | if err != nil { |
| 539 | http.NotFound(w, r) |
| 540 | return |
| 541 | } |
| 542 | recent := []Try{} |
| 543 | for rows.Next() { |
| 544 | var hash string |
| 545 | var create_ts time.Time |
| 546 | if err := rows.Scan(&create_ts, &hash); err != nil { |
| 547 | log.Printf("Error: failed to fetch from database: %q", err) |
| 548 | continue |
| 549 | } |
| 550 | recent = append(recent, Try{Hash: hash, CreateTS: create_ts.Format("2006-02-01")}) |
| 551 | } |
commit-bot@chromium.org | a4c3677 | 2014-05-19 17:38:52 +0000 | [diff] [blame] | 552 | w.Header().Set("Content-Type", "text/html") |
commit-bot@chromium.org | 472f830 | 2014-04-28 15:33:31 +0000 | [diff] [blame] | 553 | if err := recentTemplate.Execute(w, Recent{Tries: recent, Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo}}); err != nil { |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 554 | log.Printf("ERROR: Failed to expand template: %q\n", err) |
| 555 | } |
| 556 | } |
| 557 | |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 558 | type Workspace struct { |
commit-bot@chromium.org | 472f830 | 2014-04-28 15:33:31 +0000 | [diff] [blame] | 559 | Name string |
| 560 | Code string |
| 561 | Hash string |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 562 | Source int |
commit-bot@chromium.org | 472f830 | 2014-04-28 15:33:31 +0000 | [diff] [blame] | 563 | Tries []Try |
| 564 | Titlebar Titlebar |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | // newWorkspace generates a new random workspace name and stores it in the database. |
| 568 | func newWorkspace() (string, error) { |
| 569 | for i := 0; i < 10; i++ { |
| 570 | adj := workspaceNameAdj[rand.Intn(len(workspaceNameAdj))] |
| 571 | noun := workspaceNameNoun[rand.Intn(len(workspaceNameNoun))] |
| 572 | suffix := rand.Intn(1000) |
| 573 | name := fmt.Sprintf("%s-%s-%d", adj, noun, suffix) |
| 574 | if _, err := db.Exec("INSERT INTO workspace (name) VALUES(?)", name); err == nil { |
| 575 | return name, nil |
| 576 | } else { |
| 577 | log.Printf("ERROR: Failed to insert workspace into database: %q\n", err) |
| 578 | } |
| 579 | } |
| 580 | return "", fmt.Errorf("Failed to create a new workspace") |
| 581 | } |
| 582 | |
| 583 | // getCode returns the code for a given hash, or the empty string if not found. |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 584 | func getCode(hash string) (string, int, error) { |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 585 | code := "" |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 586 | source := 0 |
| 587 | if err := db.QueryRow("SELECT code, source_image_id FROM webtry WHERE hash=?", hash).Scan(&code, &source); err != nil { |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 588 | log.Printf("ERROR: Code for hash is missing: %q\n", err) |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 589 | return code, source, err |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 590 | } |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 591 | return code, source, nil |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | func workspaceHandler(w http.ResponseWriter, r *http.Request) { |
| 595 | log.Printf("Workspace Handler: %q\n", r.URL.Path) |
| 596 | if r.Method == "GET" { |
| 597 | tries := []Try{} |
| 598 | match := workspaceLink.FindStringSubmatch(r.URL.Path) |
| 599 | name := "" |
| 600 | if len(match) == 2 { |
| 601 | name = match[1] |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 602 | rows, err := db.Query("SELECT create_ts, hash, source_image_id FROM workspacetry WHERE name=? ORDER BY create_ts", name) |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 603 | if err != nil { |
| 604 | reportError(w, r, err, "Failed to select.") |
| 605 | return |
| 606 | } |
| 607 | for rows.Next() { |
| 608 | var hash string |
| 609 | var create_ts time.Time |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 610 | var source int |
| 611 | if err := rows.Scan(&create_ts, &hash, &source); err != nil { |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 612 | log.Printf("Error: failed to fetch from database: %q", err) |
| 613 | continue |
| 614 | } |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 615 | tries = append(tries, Try{Hash: hash, Source: source, CreateTS: create_ts.Format("2006-02-01")}) |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | var code string |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 619 | var hash string |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 620 | source := 0 |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 621 | if len(tries) == 0 { |
| 622 | code = DEFAULT_SAMPLE |
| 623 | } else { |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 624 | hash = tries[len(tries)-1].Hash |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 625 | code, source, _ = getCode(hash) |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 626 | } |
commit-bot@chromium.org | a4c3677 | 2014-05-19 17:38:52 +0000 | [diff] [blame] | 627 | w.Header().Set("Content-Type", "text/html") |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 628 | if err := workspaceTemplate.Execute(w, Workspace{Tries: tries, Code: code, Name: name, Hash: hash, Source: source, Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo}}); err != nil { |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 629 | log.Printf("ERROR: Failed to expand template: %q\n", err) |
| 630 | } |
| 631 | } else if r.Method == "POST" { |
| 632 | name, err := newWorkspace() |
| 633 | if err != nil { |
| 634 | http.Error(w, "Failed to create a new workspace.", 500) |
| 635 | return |
| 636 | } |
| 637 | http.Redirect(w, r, "/w/"+name, 302) |
| 638 | } |
| 639 | } |
| 640 | |
commit-bot@chromium.org | 4bd8fdc | 2014-04-15 00:43:51 +0000 | [diff] [blame] | 641 | // hasPreProcessor returns true if any line in the code begins with a # char. |
| 642 | func hasPreProcessor(code string) bool { |
| 643 | lines := strings.Split(code, "\n") |
| 644 | for _, s := range lines { |
| 645 | if strings.HasPrefix(strings.TrimSpace(s), "#") { |
| 646 | return true |
| 647 | } |
| 648 | } |
| 649 | return false |
| 650 | } |
| 651 | |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 652 | type TryRequest struct { |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 653 | Code string `json:"code"` |
| 654 | Name string `json:"name"` // Optional name of the workspace the code is in. |
| 655 | Source int `json:"source"` // ID of the source image, 0 if none. |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 656 | } |
| 657 | |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 658 | // iframeHandler handles the GET and POST of the main page. |
| 659 | func iframeHandler(w http.ResponseWriter, r *http.Request) { |
| 660 | log.Printf("IFrame Handler: %q\n", r.URL.Path) |
| 661 | if r.Method != "GET" { |
| 662 | http.NotFound(w, r) |
| 663 | return |
| 664 | } |
| 665 | match := iframeLink.FindStringSubmatch(r.URL.Path) |
| 666 | if len(match) != 2 { |
| 667 | http.NotFound(w, r) |
| 668 | return |
| 669 | } |
| 670 | hash := match[1] |
| 671 | if db == nil { |
| 672 | http.NotFound(w, r) |
| 673 | return |
| 674 | } |
| 675 | var code string |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 676 | code, source, err := getCode(hash) |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 677 | if err != nil { |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 678 | http.NotFound(w, r) |
| 679 | return |
| 680 | } |
| 681 | // Expand the template. |
commit-bot@chromium.org | a4c3677 | 2014-05-19 17:38:52 +0000 | [diff] [blame] | 682 | w.Header().Set("Content-Type", "text/html") |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 683 | if err := iframeTemplate.Execute(w, userCode{Code: code, Hash: hash, Source: source}); err != nil { |
commit-bot@chromium.org | 2dceeda | 2014-04-19 14:50:23 +0000 | [diff] [blame] | 684 | log.Printf("ERROR: Failed to expand template: %q\n", err) |
| 685 | } |
| 686 | } |
| 687 | |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 688 | type TryInfo struct { |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 689 | Hash string `json:"hash"` |
| 690 | Code string `json:"code"` |
| 691 | Source int `json:"source"` |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | // tryInfoHandler returns information about a specific try. |
| 695 | func tryInfoHandler(w http.ResponseWriter, r *http.Request) { |
| 696 | log.Printf("Try Info Handler: %q\n", r.URL.Path) |
| 697 | if r.Method != "GET" { |
| 698 | http.NotFound(w, r) |
| 699 | return |
| 700 | } |
| 701 | match := tryInfoLink.FindStringSubmatch(r.URL.Path) |
| 702 | if len(match) != 2 { |
| 703 | http.NotFound(w, r) |
| 704 | return |
| 705 | } |
| 706 | hash := match[1] |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 707 | code, source, err := getCode(hash) |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 708 | if err != nil { |
| 709 | http.NotFound(w, r) |
| 710 | return |
| 711 | } |
| 712 | m := TryInfo{ |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 713 | Hash: hash, |
| 714 | Code: code, |
| 715 | Source: source, |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 716 | } |
| 717 | resp, err := json.Marshal(m) |
| 718 | if err != nil { |
| 719 | reportError(w, r, err, "Failed to serialize a response.") |
| 720 | return |
| 721 | } |
| 722 | w.Header().Set("Content-Type", "application/json") |
| 723 | w.Write(resp) |
| 724 | } |
| 725 | |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 726 | func cleanCompileOutput(s, hash string) string { |
| 727 | old := "../../../cache/" + hash + ".cpp:" |
| 728 | log.Printf("INFO: replacing %q\n", old) |
| 729 | return strings.Replace(s, old, "usercode.cpp:", -1) |
| 730 | } |
| 731 | |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 732 | // mainHandler handles the GET and POST of the main page. |
| 733 | func mainHandler(w http.ResponseWriter, r *http.Request) { |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 734 | log.Printf("Main Handler: %q\n", r.URL.Path) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 735 | if r.Method == "GET" { |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 736 | code := DEFAULT_SAMPLE |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 737 | source := 0 |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 738 | match := directLink.FindStringSubmatch(r.URL.Path) |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 739 | var hash string |
commit-bot@chromium.org | 06aca01 | 2014-04-14 20:12:08 +0000 | [diff] [blame] | 740 | if len(match) == 2 && r.URL.Path != "/" { |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 741 | hash = match[1] |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 742 | if db == nil { |
| 743 | http.NotFound(w, r) |
| 744 | return |
| 745 | } |
| 746 | // Update 'code' with the code found in the database. |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 747 | if err := db.QueryRow("SELECT code, source_image_id FROM webtry WHERE hash=?", hash).Scan(&code, &source); err != nil { |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 748 | http.NotFound(w, r) |
| 749 | return |
| 750 | } |
| 751 | } |
| 752 | // Expand the template. |
commit-bot@chromium.org | a4c3677 | 2014-05-19 17:38:52 +0000 | [diff] [blame] | 753 | w.Header().Set("Content-Type", "text/html") |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 754 | if err := indexTemplate.Execute(w, userCode{Code: code, Hash: hash, Source: source, Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo}}); err != nil { |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 755 | log.Printf("ERROR: Failed to expand template: %q\n", err) |
| 756 | } |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 757 | } else if r.Method == "POST" { |
| 758 | w.Header().Set("Content-Type", "application/json") |
commit-bot@chromium.org | 4bd8fdc | 2014-04-15 00:43:51 +0000 | [diff] [blame] | 759 | buf := bytes.NewBuffer(make([]byte, 0, MAX_TRY_SIZE)) |
| 760 | n, err := buf.ReadFrom(r.Body) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 761 | if err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 762 | reportTryError(w, r, err, "Failed to read a request body.", "") |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 763 | return |
| 764 | } |
commit-bot@chromium.org | 4bd8fdc | 2014-04-15 00:43:51 +0000 | [diff] [blame] | 765 | if n == MAX_TRY_SIZE { |
| 766 | 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] | 767 | reportTryError(w, r, err, "Code too large.", "") |
commit-bot@chromium.org | 4bd8fdc | 2014-04-15 00:43:51 +0000 | [diff] [blame] | 768 | return |
| 769 | } |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 770 | request := TryRequest{} |
| 771 | if err := json.Unmarshal(buf.Bytes(), &request); err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 772 | reportTryError(w, r, err, "Coulnd't decode JSON.", "") |
commit-bot@chromium.org | d04e1dd | 2014-04-19 13:55:50 +0000 | [diff] [blame] | 773 | return |
| 774 | } |
| 775 | if hasPreProcessor(request.Code) { |
commit-bot@chromium.org | 4bd8fdc | 2014-04-15 00:43:51 +0000 | [diff] [blame] | 776 | err := fmt.Errorf("Found preprocessor macro in code.") |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 777 | reportTryError(w, r, err, "Preprocessor macros aren't allowed.", "") |
commit-bot@chromium.org | 4bd8fdc | 2014-04-15 00:43:51 +0000 | [diff] [blame] | 778 | return |
| 779 | } |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 780 | hash, err := expandCode(LineNumbers(request.Code), request.Source) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 781 | if err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 782 | 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] | 783 | return |
| 784 | } |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 785 | writeToDatabase(hash, request.Code, request.Name, request.Source) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 786 | message, err := doCmd(fmt.Sprintf(RESULT_COMPILE, hash, hash), true) |
| 787 | if err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 788 | message = cleanCompileOutput(message, hash) |
| 789 | reportTryError(w, r, err, message, hash) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 790 | return |
| 791 | } |
| 792 | linkMessage, err := doCmd(fmt.Sprintf(LINK, hash, hash), true) |
| 793 | if err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 794 | linkMessage = cleanCompileOutput(linkMessage, hash) |
| 795 | reportTryError(w, r, err, linkMessage, hash) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 796 | return |
| 797 | } |
| 798 | message += linkMessage |
| 799 | cmd := hash + " --out " + hash + ".png" |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 800 | if request.Source > 0 { |
| 801 | cmd += fmt.Sprintf(" --source image-%d.png", request.Source) |
| 802 | } |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 803 | if *useChroot { |
| 804 | cmd = "schroot -c webtry --directory=/inout -- /inout/" + cmd |
| 805 | } else { |
| 806 | abs, err := filepath.Abs("../../../inout") |
| 807 | if err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 808 | reportTryError(w, r, err, "Failed to find executable directory.", hash) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 809 | return |
| 810 | } |
| 811 | cmd = abs + "/" + cmd |
| 812 | } |
| 813 | |
| 814 | execMessage, err := doCmd(cmd, false) |
| 815 | if err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 816 | 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] | 817 | return |
| 818 | } |
| 819 | png, err := ioutil.ReadFile("../../../inout/" + hash + ".png") |
| 820 | if err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 821 | 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] | 822 | return |
| 823 | } |
| 824 | |
| 825 | m := response{ |
| 826 | Message: message, |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 827 | StdOut: execMessage, |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 828 | Img: base64.StdEncoding.EncodeToString([]byte(png)), |
commit-bot@chromium.org | c81d1c4 | 2014-04-14 18:53:10 +0000 | [diff] [blame] | 829 | Hash: hash, |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 830 | } |
| 831 | resp, err := json.Marshal(m) |
| 832 | if err != nil { |
commit-bot@chromium.org | 9004192 | 2014-04-22 21:13:45 +0000 | [diff] [blame] | 833 | reportTryError(w, r, err, "Failed to serialize a response.", hash) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 834 | return |
| 835 | } |
commit-bot@chromium.org | a4c3677 | 2014-05-19 17:38:52 +0000 | [diff] [blame] | 836 | w.Header().Set("Content-Type", "application/json") |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 837 | w.Write(resp) |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | func main() { |
| 842 | flag.Parse() |
commit-bot@chromium.org | a4c3677 | 2014-05-19 17:38:52 +0000 | [diff] [blame] | 843 | http.HandleFunc("/i/", autogzip.HandleFunc(imageHandler)) |
| 844 | http.HandleFunc("/w/", autogzip.HandleFunc(workspaceHandler)) |
| 845 | http.HandleFunc("/recent/", autogzip.HandleFunc(recentHandler)) |
| 846 | http.HandleFunc("/iframe/", autogzip.HandleFunc(iframeHandler)) |
| 847 | http.HandleFunc("/json/", autogzip.HandleFunc(tryInfoHandler)) |
commit-bot@chromium.org | 0a7e5b7 | 2014-05-29 15:58:00 +0000 | [diff] [blame^] | 848 | http.HandleFunc("/sources/", autogzip.HandleFunc(sourcesHandler)) |
fmalita@google.com | 950306c | 2014-05-01 15:14:56 +0000 | [diff] [blame] | 849 | |
| 850 | // Resources are served directly |
| 851 | // TODO add support for caching/etags/gzip |
commit-bot@chromium.org | a4c3677 | 2014-05-19 17:38:52 +0000 | [diff] [blame] | 852 | http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./")))) |
fmalita@google.com | 950306c | 2014-05-01 15:14:56 +0000 | [diff] [blame] | 853 | |
commit-bot@chromium.org | 35ffc44 | 2014-04-22 19:32:06 +0000 | [diff] [blame] | 854 | // TODO Break out /c/ as it's own handler. |
commit-bot@chromium.org | a4c3677 | 2014-05-19 17:38:52 +0000 | [diff] [blame] | 855 | http.HandleFunc("/", autogzip.HandleFunc(mainHandler)) |
commit-bot@chromium.org | 282333f | 2014-04-14 14:54:07 +0000 | [diff] [blame] | 856 | log.Fatal(http.ListenAndServe(*port, nil)) |
commit-bot@chromium.org | 6d036c2 | 2014-04-09 18:59:44 +0000 | [diff] [blame] | 857 | } |