| 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/binary" | 8 "encoding/binary" |
| 9 "encoding/json" | 9 "encoding/json" |
| 10 "flag" | 10 "flag" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 ) | 29 ) |
| 30 | 30 |
| 31 import ( | 31 import ( |
| 32 "github.com/fiorix/go-web/autogzip" | 32 "github.com/fiorix/go-web/autogzip" |
| 33 _ "github.com/go-sql-driver/mysql" | 33 _ "github.com/go-sql-driver/mysql" |
| 34 _ "github.com/mattn/go-sqlite3" | 34 _ "github.com/mattn/go-sqlite3" |
| 35 "github.com/rcrowley/go-metrics" | 35 "github.com/rcrowley/go-metrics" |
| 36 ) | 36 ) |
| 37 | 37 |
| 38 const ( | 38 const ( |
| 39 » 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` | 39 » RUN_GYP = `../../experimental/webtry/gyp_for_webtry %s` |
| 40 » 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/libetc1.a obj/gyp/libSkKTX.a obj/gyp/libwebp_dec.a
obj/gyp/libwebp_demux.a obj/gyp/libwebp_dsp.a obj/gyp/libwebp_enc.a obj/gyp/lib
webp_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 -lfr
eetype` | 40 » RUN_NINJA = `ninja -C ../../../inout/Release %s` |
| 41 | 41 |
| 42 DEFAULT_SAMPLE = `void draw(SkCanvas* canvas) { | 42 DEFAULT_SAMPLE = `void draw(SkCanvas* canvas) { |
| 43 SkPaint p; | 43 SkPaint p; |
| 44 p.setColor(SK_ColorRED); | 44 p.setColor(SK_ColorRED); |
| 45 p.setAntiAlias(true); | 45 p.setAntiAlias(true); |
| 46 p.setStyle(SkPaint::kStroke_Style); | 46 p.setStyle(SkPaint::kStroke_Style); |
| 47 p.setStrokeWidth(10); | 47 p.setStrokeWidth(10); |
| 48 | 48 |
| 49 canvas->drawLine(20, 20, 100, 100, p); | 49 canvas->drawLine(20, 20, 100, 100, p); |
| 50 }` | 50 }` |
| 51 // Don't increase above 2^16 w/o altering the db tables to accept someth
ing bigger than TEXT. | 51 // Don't increase above 2^16 w/o altering the db tables to accept someth
ing bigger than TEXT. |
| 52 MAX_TRY_SIZE = 64000 | 52 MAX_TRY_SIZE = 64000 |
| 53 ) | 53 ) |
| 54 | 54 |
| 55 var ( | 55 var ( |
| 56 // codeTemplate is the cpp code template the user's code is copied into. | 56 // codeTemplate is the cpp code template the user's code is copied into. |
| 57 codeTemplate *template.Template = nil | 57 codeTemplate *template.Template = nil |
| 58 | 58 |
| 59 // gypTemplate is the GYP file to build the executable containing the us
er's code. |
| 60 gypTemplate *template.Template = nil |
| 61 |
| 59 // indexTemplate is the main index.html page we serve. | 62 // indexTemplate is the main index.html page we serve. |
| 60 indexTemplate *htemplate.Template = nil | 63 indexTemplate *htemplate.Template = nil |
| 61 | 64 |
| 62 // iframeTemplate is the main index.html page we serve. | 65 // iframeTemplate is the main index.html page we serve. |
| 63 iframeTemplate *htemplate.Template = nil | 66 iframeTemplate *htemplate.Template = nil |
| 64 | 67 |
| 65 // recentTemplate is a list of recent images. | 68 // recentTemplate is a list of recent images. |
| 66 recentTemplate *htemplate.Template = nil | 69 recentTemplate *htemplate.Template = nil |
| 67 | 70 |
| 68 // workspaceTemplate is the page for workspaces, a series of webtrys. | 71 // workspaceTemplate is the page for workspaces, a series of webtrys. |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 cwd, err := filepath.Abs(filepath.Dir(os.Args[0])) | 146 cwd, err := filepath.Abs(filepath.Dir(os.Args[0])) |
| 144 if err != nil { | 147 if err != nil { |
| 145 log.Fatal(err) | 148 log.Fatal(err) |
| 146 } | 149 } |
| 147 os.Chdir(cwd) | 150 os.Chdir(cwd) |
| 148 | 151 |
| 149 codeTemplate, err = template.ParseFiles(filepath.Join(cwd, "templates/te
mplate.cpp")) | 152 codeTemplate, err = template.ParseFiles(filepath.Join(cwd, "templates/te
mplate.cpp")) |
| 150 if err != nil { | 153 if err != nil { |
| 151 panic(err) | 154 panic(err) |
| 152 } | 155 } |
| 156 gypTemplate, err = template.ParseFiles(filepath.Join(cwd, "templates/tem
plate.gyp")) |
| 157 if err != nil { |
| 158 panic(err) |
| 159 } |
| 153 indexTemplate, err = htemplate.ParseFiles( | 160 indexTemplate, err = htemplate.ParseFiles( |
| 154 filepath.Join(cwd, "templates/index.html"), | 161 filepath.Join(cwd, "templates/index.html"), |
| 155 filepath.Join(cwd, "templates/titlebar.html"), | 162 filepath.Join(cwd, "templates/titlebar.html"), |
| 156 filepath.Join(cwd, "templates/content.html"), | 163 filepath.Join(cwd, "templates/content.html"), |
| 157 filepath.Join(cwd, "templates/headercommon.html"), | 164 filepath.Join(cwd, "templates/headercommon.html"), |
| 158 filepath.Join(cwd, "templates/footercommon.html"), | 165 filepath.Join(cwd, "templates/footercommon.html"), |
| 159 ) | 166 ) |
| 160 if err != nil { | 167 if err != nil { |
| 161 panic(err) | 168 panic(err) |
| 162 } | 169 } |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 } | 337 } |
| 331 | 338 |
| 332 // userCode is used in template expansion. | 339 // userCode is used in template expansion. |
| 333 type userCode struct { | 340 type userCode struct { |
| 334 Code string | 341 Code string |
| 335 Hash string | 342 Hash string |
| 336 Source int | 343 Source int |
| 337 Titlebar Titlebar | 344 Titlebar Titlebar |
| 338 } | 345 } |
| 339 | 346 |
| 340 // expandToFile expands the template and writes the result to the file. | 347 // writeTemplate creates a given output file and writes the template |
| 341 func expandToFile(filename string, code string, t *template.Template) error { | 348 // result there. |
| 349 func writeTemplate(filename string, t *template.Template, context interface{}) e
rror { |
| 342 f, err := os.Create(filename) | 350 f, err := os.Create(filename) |
| 343 if err != nil { | 351 if err != nil { |
| 344 return err | 352 return err |
| 345 } | 353 } |
| 346 defer f.Close() | 354 defer f.Close() |
| 347 » return t.Execute(f, userCode{Code: code, Titlebar: Titlebar{GitHash: git
Hash, GitInfo: gitInfo}}) | 355 » return t.Execute( f, context ) |
| 356 } |
| 357 |
| 358 // expandToFile expands the template and writes the result to the file. |
| 359 func expandToFile(filename string, code string, t *template.Template) error { |
| 360 » return writeTemplate( filename, t, userCode{Code: code, Titlebar: Titleb
ar{GitHash: gitHash, GitInfo: gitInfo}} ) |
| 348 } | 361 } |
| 349 | 362 |
| 350 // expandCode expands the template into a file and calculates the MD5 hash. | 363 // expandCode expands the template into a file and calculates the MD5 hash. |
| 351 func expandCode(code string, source int) (string, error) { | 364 func expandCode(code string, source int) (string, error) { |
| 352 h := md5.New() | 365 h := md5.New() |
| 353 h.Write([]byte(code)) | 366 h.Write([]byte(code)) |
| 354 binary.Write(h, binary.LittleEndian, int64(source)) | 367 binary.Write(h, binary.LittleEndian, int64(source)) |
| 355 hash := fmt.Sprintf("%x", h.Sum(nil)) | 368 hash := fmt.Sprintf("%x", h.Sum(nil)) |
| 356 // At this point we are running in skia/experimental/webtry, making cach
e a | 369 // At this point we are running in skia/experimental/webtry, making cach
e a |
| 357 // peer directory to skia. | 370 // peer directory to skia. |
| 358 // TODO(jcgregorio) Make all relative directories into flags. | 371 // TODO(jcgregorio) Make all relative directories into flags. |
| 359 » err := expandToFile(fmt.Sprintf("../../../cache/%s.cpp", hash), code, co
deTemplate) | 372 » err := expandToFile(fmt.Sprintf("../../../cache/src/%s.cpp", hash), code
, codeTemplate) |
| 360 return hash, err | 373 return hash, err |
| 361 } | 374 } |
| 362 | 375 |
| 376 // expandGyp produces the GYP file needed to build the code |
| 377 func expandGyp(hash string) (error) { |
| 378 return writeTemplate(fmt.Sprintf("../../../cache/%s.gyp", hash), gypTemp
late, struct {Hash string}{hash} ) |
| 379 } |
| 380 |
| 363 // response is serialized to JSON as a response to POSTs. | 381 // response is serialized to JSON as a response to POSTs. |
| 364 type response struct { | 382 type response struct { |
| 365 Message string `json:"message"` | 383 Message string `json:"message"` |
| 366 StdOut string `json:"stdout"` | 384 StdOut string `json:"stdout"` |
| 367 Img string `json:"img"` | 385 Img string `json:"img"` |
| 368 Hash string `json:"hash"` | 386 Hash string `json:"hash"` |
| 369 } | 387 } |
| 370 | 388 |
| 371 // doCmd executes the given command line string in either the out/Debug | 389 // doCmd executes the given command line string in either the out/Debug |
| 372 // directory or the inout directory. Returns the stdout and stderr. | 390 // directory or the inout directory. Returns the stdout and stderr. |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 731 resp, err := json.Marshal(m) | 749 resp, err := json.Marshal(m) |
| 732 if err != nil { | 750 if err != nil { |
| 733 reportError(w, r, err, "Failed to serialize a response.") | 751 reportError(w, r, err, "Failed to serialize a response.") |
| 734 return | 752 return |
| 735 } | 753 } |
| 736 w.Header().Set("Content-Type", "application/json") | 754 w.Header().Set("Content-Type", "application/json") |
| 737 w.Write(resp) | 755 w.Write(resp) |
| 738 } | 756 } |
| 739 | 757 |
| 740 func cleanCompileOutput(s, hash string) string { | 758 func cleanCompileOutput(s, hash string) string { |
| 741 » old := "../../../cache/" + hash + ".cpp:" | 759 » old := "../../../cache/src/" + hash + ".cpp:" |
| 742 log.Printf("INFO: replacing %q\n", old) | 760 log.Printf("INFO: replacing %q\n", old) |
| 743 return strings.Replace(s, old, "usercode.cpp:", -1) | 761 return strings.Replace(s, old, "usercode.cpp:", -1) |
| 744 } | 762 } |
| 745 | 763 |
| 746 // mainHandler handles the GET and POST of the main page. | 764 // mainHandler handles the GET and POST of the main page. |
| 747 func mainHandler(w http.ResponseWriter, r *http.Request) { | 765 func mainHandler(w http.ResponseWriter, r *http.Request) { |
| 748 log.Printf("Main Handler: %q\n", r.URL.Path) | 766 log.Printf("Main Handler: %q\n", r.URL.Path) |
| 749 requestsCounter.Inc(1) | 767 requestsCounter.Inc(1) |
| 750 if r.Method == "GET" { | 768 if r.Method == "GET" { |
| 751 code := DEFAULT_SAMPLE | 769 code := DEFAULT_SAMPLE |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 791 err := fmt.Errorf("Found preprocessor macro in code.") | 809 err := fmt.Errorf("Found preprocessor macro in code.") |
| 792 reportTryError(w, r, err, "Preprocessor macros aren't al
lowed.", "") | 810 reportTryError(w, r, err, "Preprocessor macros aren't al
lowed.", "") |
| 793 return | 811 return |
| 794 } | 812 } |
| 795 hash, err := expandCode(LineNumbers(request.Code), request.Sourc
e) | 813 hash, err := expandCode(LineNumbers(request.Code), request.Sourc
e) |
| 796 if err != nil { | 814 if err != nil { |
| 797 reportTryError(w, r, err, "Failed to write the code to c
ompile.", hash) | 815 reportTryError(w, r, err, "Failed to write the code to c
ompile.", hash) |
| 798 return | 816 return |
| 799 } | 817 } |
| 800 writeToDatabase(hash, request.Code, request.Name, request.Source
) | 818 writeToDatabase(hash, request.Code, request.Name, request.Source
) |
| 801 » » message, err := doCmd(fmt.Sprintf(RESULT_COMPILE, hash, hash), t
rue) | 819 » » err = expandGyp(hash) |
| 820 » » if err != nil { |
| 821 » » » reportTryError(w, r, err, "Failed to write the gyp file.
", hash) |
| 822 » » » return |
| 823 » » } |
| 824 » » message, err := doCmd(fmt.Sprintf(RUN_GYP, hash), true) |
| 802 if err != nil { | 825 if err != nil { |
| 803 message = cleanCompileOutput(message, hash) | 826 message = cleanCompileOutput(message, hash) |
| 804 reportTryError(w, r, err, message, hash) | 827 reportTryError(w, r, err, message, hash) |
| 805 return | 828 return |
| 806 } | 829 } |
| 807 » » linkMessage, err := doCmd(fmt.Sprintf(LINK, hash, hash), true) | 830 » » linkMessage, err := doCmd(fmt.Sprintf(RUN_NINJA, hash), true) |
| 808 if err != nil { | 831 if err != nil { |
| 809 linkMessage = cleanCompileOutput(linkMessage, hash) | 832 linkMessage = cleanCompileOutput(linkMessage, hash) |
| 810 reportTryError(w, r, err, linkMessage, hash) | 833 reportTryError(w, r, err, linkMessage, hash) |
| 811 return | 834 return |
| 812 } | 835 } |
| 813 message += linkMessage | 836 message += linkMessage |
| 814 cmd := hash + " --out " + hash + ".png" | 837 cmd := hash + " --out " + hash + ".png" |
| 815 if request.Source > 0 { | 838 if request.Source > 0 { |
| 816 cmd += fmt.Sprintf(" --source image-%d.png", request.So
urce) | 839 cmd += fmt.Sprintf(" --source image-%d.png", request.So
urce) |
| 817 } | 840 } |
| 818 if *useChroot { | 841 if *useChroot { |
| 819 » » » cmd = "schroot -c webtry --directory=/inout -- /inout/"
+ cmd | 842 » » » cmd = "schroot -c webtry --directory=/inout -- /inout/Re
lease/" + cmd |
| 820 } else { | 843 } else { |
| 821 » » » abs, err := filepath.Abs("../../../inout") | 844 » » » abs, err := filepath.Abs("../../../inout/Release") |
| 822 if err != nil { | 845 if err != nil { |
| 823 reportTryError(w, r, err, "Failed to find execut
able directory.", hash) | 846 reportTryError(w, r, err, "Failed to find execut
able directory.", hash) |
| 824 return | 847 return |
| 825 } | 848 } |
| 826 cmd = abs + "/" + cmd | 849 cmd = abs + "/" + cmd |
| 827 } | 850 } |
| 828 | 851 |
| 829 execMessage, err := doCmd(cmd, false) | 852 execMessage, err := doCmd(cmd, false) |
| 830 if err != nil { | 853 if err != nil { |
| 831 reportTryError(w, r, err, "Failed to run the code:\n"+ex
ecMessage, hash) | 854 reportTryError(w, r, err, "Failed to run the code:\n"+ex
ecMessage, hash) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 863 http.HandleFunc("/sources/", autogzip.HandleFunc(sourcesHandler)) | 886 http.HandleFunc("/sources/", autogzip.HandleFunc(sourcesHandler)) |
| 864 | 887 |
| 865 // Resources are served directly | 888 // Resources are served directly |
| 866 // TODO add support for caching/etags/gzip | 889 // TODO add support for caching/etags/gzip |
| 867 http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./")))) | 890 http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./")))) |
| 868 | 891 |
| 869 // TODO Break out /c/ as it's own handler. | 892 // TODO Break out /c/ as it's own handler. |
| 870 http.HandleFunc("/", autogzip.HandleFunc(mainHandler)) | 893 http.HandleFunc("/", autogzip.HandleFunc(mainHandler)) |
| 871 log.Fatal(http.ListenAndServe(*port, nil)) | 894 log.Fatal(http.ListenAndServe(*port, nil)) |
| 872 } | 895 } |
| OLD | NEW |