OLD | NEW |
1 package main | 1 package main |
2 | 2 |
3 import ( | 3 import ( |
4 "bytes" | 4 "bytes" |
5 "crypto/md5" | 5 "crypto/md5" |
6 "database/sql" | 6 "database/sql" |
7 "encoding/base64" | 7 "encoding/base64" |
8 "encoding/json" | 8 "encoding/json" |
9 "flag" | 9 "flag" |
10 "fmt" | 10 "fmt" |
11 _ "github.com/go-sql-driver/mysql" | 11 _ "github.com/go-sql-driver/mysql" |
12 _ "github.com/mattn/go-sqlite3" | 12 _ "github.com/mattn/go-sqlite3" |
13 htemplate "html/template" | 13 htemplate "html/template" |
14 "io/ioutil" | 14 "io/ioutil" |
15 "log" | 15 "log" |
16 "math/rand" | 16 "math/rand" |
17 "net/http" | 17 "net/http" |
18 "os" | 18 "os" |
19 "os/exec" | 19 "os/exec" |
20 "path/filepath" | 20 "path/filepath" |
21 "regexp" | 21 "regexp" |
22 "strings" | 22 "strings" |
23 "text/template" | 23 "text/template" |
24 "time" | 24 "time" |
25 ) | 25 ) |
26 | 26 |
27 const ( | 27 const ( |
28 RESULT_COMPILE = `../../experimental/webtry/safec++ -DSK_GAMMA_SRGB -DSK
_GAMMA_APPLY_TO_A8 -DSK_SCALAR_TO_FLOAT_EXCLUDED -DSK_ALLOW_STATIC_GLOBAL_INITIA
LIZERS=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_UNI
X -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../../i
nclude/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` | 28 RESULT_COMPILE = `../../experimental/webtry/safec++ -DSK_GAMMA_SRGB -DSK
_GAMMA_APPLY_TO_A8 -DSK_SCALAR_TO_FLOAT_EXCLUDED -DSK_ALLOW_STATIC_GLOBAL_INITIA
LIZERS=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_UNI
X -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../../i
nclude/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` |
29 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_effect
s.a obj/gyp/libjpeg.a obj/gyp/libwebp_dec.a obj/gyp/libwebp_demux.a obj/gyp/libw
ebp_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` | 29 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_effect
s.a obj/gyp/libjpeg.a obj/gyp/libwebp_dec.a obj/gyp/libwebp_demux.a obj/gyp/libw
ebp_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` |
30 » DEFAULT_SAMPLE = `SkPaint p; | 30 » DEFAULT_SAMPLE = `void draw(SkCanvas* canvas) { |
31 p.setColor(SK_ColorRED); | 31 SkPaint p; |
32 p.setAntiAlias(true); | 32 p.setColor(SK_ColorRED); |
33 p.setStyle(SkPaint::kStroke_Style); | 33 p.setAntiAlias(true); |
34 p.setStrokeWidth(10); | 34 p.setStyle(SkPaint::kStroke_Style); |
| 35 p.setStrokeWidth(10); |
35 | 36 |
36 canvas->drawLine(20, 20, 100, 100, p); | 37 canvas->drawLine(20, 20, 100, 100, p); |
37 ` | 38 }` |
38 // Don't increase above 2^16 w/o altering the db tables to accept someth
ing bigger than TEXT. | 39 // Don't increase above 2^16 w/o altering the db tables to accept someth
ing bigger than TEXT. |
39 MAX_TRY_SIZE = 64000 | 40 MAX_TRY_SIZE = 64000 |
40 ) | 41 ) |
41 | 42 |
42 var ( | 43 var ( |
43 // codeTemplate is the cpp code template the user's code is copied into. | 44 // codeTemplate is the cpp code template the user's code is copied into. |
44 codeTemplate *template.Template = nil | 45 codeTemplate *template.Template = nil |
45 | 46 |
46 // indexTemplate is the main index.html page we serve. | 47 // indexTemplate is the main index.html page we serve. |
47 indexTemplate *htemplate.Template = nil | 48 indexTemplate *htemplate.Template = nil |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 if _, err := db.Exec("INSERT INTO webtry (code, hash) VALUES(?, ?)", cod
e, hash); err != nil { | 342 if _, err := db.Exec("INSERT INTO webtry (code, hash) VALUES(?, ?)", cod
e, hash); err != nil { |
342 log.Printf("ERROR: Failed to insert code into database: %q\n", e
rr) | 343 log.Printf("ERROR: Failed to insert code into database: %q\n", e
rr) |
343 } | 344 } |
344 if workspaceName != "" { | 345 if workspaceName != "" { |
345 if _, err := db.Exec("INSERT INTO workspacetry (name, hash) VALU
ES(?, ?)", workspaceName, hash); err != nil { | 346 if _, err := db.Exec("INSERT INTO workspacetry (name, hash) VALU
ES(?, ?)", workspaceName, hash); err != nil { |
346 log.Printf("ERROR: Failed to insert into workspacetry ta
ble: %q\n", err) | 347 log.Printf("ERROR: Failed to insert into workspacetry ta
ble: %q\n", err) |
347 } | 348 } |
348 } | 349 } |
349 } | 350 } |
350 | 351 |
351 func cssHandler(w http.ResponseWriter, r *http.Request) { | |
352 http.ServeFile(w, r, "css/webtry.css") | |
353 } | |
354 | |
355 // imageHandler serves up the PNG of a specific try. | 352 // imageHandler serves up the PNG of a specific try. |
356 func imageHandler(w http.ResponseWriter, r *http.Request) { | 353 func imageHandler(w http.ResponseWriter, r *http.Request) { |
357 log.Printf("Image Handler: %q\n", r.URL.Path) | 354 log.Printf("Image Handler: %q\n", r.URL.Path) |
358 if r.Method != "GET" { | 355 if r.Method != "GET" { |
359 http.NotFound(w, r) | 356 http.NotFound(w, r) |
360 return | 357 return |
361 } | 358 } |
362 match := imageLink.FindStringSubmatch(r.URL.Path) | 359 match := imageLink.FindStringSubmatch(r.URL.Path) |
363 if len(match) != 2 { | 360 if len(match) != 2 { |
364 http.NotFound(w, r) | 361 http.NotFound(w, r) |
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
671 } | 668 } |
672 } | 669 } |
673 | 670 |
674 func main() { | 671 func main() { |
675 flag.Parse() | 672 flag.Parse() |
676 http.HandleFunc("/i/", imageHandler) | 673 http.HandleFunc("/i/", imageHandler) |
677 http.HandleFunc("/w/", workspaceHandler) | 674 http.HandleFunc("/w/", workspaceHandler) |
678 http.HandleFunc("/recent/", recentHandler) | 675 http.HandleFunc("/recent/", recentHandler) |
679 http.HandleFunc("/iframe/", iframeHandler) | 676 http.HandleFunc("/iframe/", iframeHandler) |
680 http.HandleFunc("/json/", tryInfoHandler) | 677 http.HandleFunc("/json/", tryInfoHandler) |
681 » http.HandleFunc("/css/", cssHandler) | 678 |
682 » http.Handle("/js/", http.FileServer(http.Dir("./"))) | 679 » // Resources are served directly |
| 680 » // TODO add support for caching/etags/gzip |
| 681 » http.Handle("/res/", http.FileServer(http.Dir("./"))) |
| 682 |
683 // TODO Break out /c/ as it's own handler. | 683 // TODO Break out /c/ as it's own handler. |
684 http.HandleFunc("/", mainHandler) | 684 http.HandleFunc("/", mainHandler) |
685 log.Fatal(http.ListenAndServe(*port, nil)) | 685 log.Fatal(http.ListenAndServe(*port, nil)) |
686 } | 686 } |
OLD | NEW |