Chromium Code Reviews| 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 | |
|
jcgregorio
2014/09/17 15:00:07
no blank line here.
humper
2014/09/17 15:08:23
Done.
| |
| 350 func writeTemplate(filename string, t *template.Template, context interface{}) e rror { | |
| 342 f, err := os.Create(filename) | 351 f, err := os.Create(filename) |
| 343 if err != nil { | 352 if err != nil { |
| 344 return err | 353 return err |
| 345 } | 354 } |
| 346 defer f.Close() | 355 defer f.Close() |
| 347 » return t.Execute(f, userCode{Code: code, Titlebar: Titlebar{GitHash: git Hash, GitInfo: gitInfo}}) | 356 » return t.Execute( f, context ) |
| 357 } | |
| 358 | |
| 359 // expandToFile expands the template and writes the result to the file. | |
| 360 func expandToFile(filename string, code string, t *template.Template) error { | |
| 361 » return writeTemplate( filename, t, userCode{Code: code, Titlebar: Titleb ar{GitHash: gitHash, GitInfo: gitInfo}} ) | |
| 348 } | 362 } |
| 349 | 363 |
| 350 // expandCode expands the template into a file and calculates the MD5 hash. | 364 // expandCode expands the template into a file and calculates the MD5 hash. |
| 351 func expandCode(code string, source int) (string, error) { | 365 func expandCode(code string, source int) (string, error) { |
| 352 h := md5.New() | 366 h := md5.New() |
| 353 h.Write([]byte(code)) | 367 h.Write([]byte(code)) |
| 354 binary.Write(h, binary.LittleEndian, int64(source)) | 368 binary.Write(h, binary.LittleEndian, int64(source)) |
| 355 hash := fmt.Sprintf("%x", h.Sum(nil)) | 369 hash := fmt.Sprintf("%x", h.Sum(nil)) |
| 356 // At this point we are running in skia/experimental/webtry, making cach e a | 370 // At this point we are running in skia/experimental/webtry, making cach e a |
| 357 // peer directory to skia. | 371 // peer directory to skia. |
| 358 // TODO(jcgregorio) Make all relative directories into flags. | 372 // TODO(jcgregorio) Make all relative directories into flags. |
| 359 » err := expandToFile(fmt.Sprintf("../../../cache/%s.cpp", hash), code, co deTemplate) | 373 » err := expandToFile(fmt.Sprintf("../../../cache/src/%s.cpp", hash), code , codeTemplate) |
| 360 return hash, err | 374 return hash, err |
| 361 } | 375 } |
| 362 | 376 |
| 377 // expandGyp produces the GYP file needed to build the code | |
| 378 func expandGyp(hash string) (error) { | |
| 379 return writeTemplate(fmt.Sprintf("../../../cache/%s.gyp", hash), gypTemp late, struct {Hash string}{hash} ) | |
| 380 } | |
| 381 | |
| 363 // response is serialized to JSON as a response to POSTs. | 382 // response is serialized to JSON as a response to POSTs. |
| 364 type response struct { | 383 type response struct { |
| 365 Message string `json:"message"` | 384 Message string `json:"message"` |
| 366 StdOut string `json:"stdout"` | 385 StdOut string `json:"stdout"` |
| 367 Img string `json:"img"` | 386 Img string `json:"img"` |
| 368 Hash string `json:"hash"` | 387 Hash string `json:"hash"` |
| 369 } | 388 } |
| 370 | 389 |
| 371 // doCmd executes the given command line string in either the out/Debug | 390 // doCmd executes the given command line string in either the out/Debug |
| 372 // directory or the inout directory. Returns the stdout and stderr. | 391 // 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) | 750 resp, err := json.Marshal(m) |
| 732 if err != nil { | 751 if err != nil { |
| 733 reportError(w, r, err, "Failed to serialize a response.") | 752 reportError(w, r, err, "Failed to serialize a response.") |
| 734 return | 753 return |
| 735 } | 754 } |
| 736 w.Header().Set("Content-Type", "application/json") | 755 w.Header().Set("Content-Type", "application/json") |
| 737 w.Write(resp) | 756 w.Write(resp) |
| 738 } | 757 } |
| 739 | 758 |
| 740 func cleanCompileOutput(s, hash string) string { | 759 func cleanCompileOutput(s, hash string) string { |
| 741 » old := "../../../cache/" + hash + ".cpp:" | 760 » old := "../../../cache/src/" + hash + ".cpp:" |
| 742 log.Printf("INFO: replacing %q\n", old) | 761 log.Printf("INFO: replacing %q\n", old) |
| 743 return strings.Replace(s, old, "usercode.cpp:", -1) | 762 return strings.Replace(s, old, "usercode.cpp:", -1) |
| 744 } | 763 } |
| 745 | 764 |
| 746 // mainHandler handles the GET and POST of the main page. | 765 // mainHandler handles the GET and POST of the main page. |
| 747 func mainHandler(w http.ResponseWriter, r *http.Request) { | 766 func mainHandler(w http.ResponseWriter, r *http.Request) { |
| 748 log.Printf("Main Handler: %q\n", r.URL.Path) | 767 log.Printf("Main Handler: %q\n", r.URL.Path) |
| 749 requestsCounter.Inc(1) | 768 requestsCounter.Inc(1) |
| 750 if r.Method == "GET" { | 769 if r.Method == "GET" { |
| 751 code := DEFAULT_SAMPLE | 770 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.") | 810 err := fmt.Errorf("Found preprocessor macro in code.") |
| 792 reportTryError(w, r, err, "Preprocessor macros aren't al lowed.", "") | 811 reportTryError(w, r, err, "Preprocessor macros aren't al lowed.", "") |
| 793 return | 812 return |
| 794 } | 813 } |
| 795 hash, err := expandCode(LineNumbers(request.Code), request.Sourc e) | 814 hash, err := expandCode(LineNumbers(request.Code), request.Sourc e) |
| 796 if err != nil { | 815 if err != nil { |
| 797 reportTryError(w, r, err, "Failed to write the code to c ompile.", hash) | 816 reportTryError(w, r, err, "Failed to write the code to c ompile.", hash) |
| 798 return | 817 return |
| 799 } | 818 } |
| 800 writeToDatabase(hash, request.Code, request.Name, request.Source ) | 819 writeToDatabase(hash, request.Code, request.Name, request.Source ) |
| 801 » » message, err := doCmd(fmt.Sprintf(RESULT_COMPILE, hash, hash), t rue) | 820 » » err = expandGyp(hash) |
| 821 » » if err != nil { | |
| 822 » » » reportTryError(w, r, err, "Failed to write the gyp file. ", hash) | |
| 823 » » » return | |
| 824 » » } | |
| 825 » » message, err := doCmd(fmt.Sprintf(RUN_GYP, hash), true) | |
| 802 if err != nil { | 826 if err != nil { |
| 803 message = cleanCompileOutput(message, hash) | 827 message = cleanCompileOutput(message, hash) |
| 804 reportTryError(w, r, err, message, hash) | 828 reportTryError(w, r, err, message, hash) |
| 805 return | 829 return |
| 806 } | 830 } |
| 807 » » linkMessage, err := doCmd(fmt.Sprintf(LINK, hash, hash), true) | 831 » » linkMessage, err := doCmd(fmt.Sprintf(RUN_NINJA, hash), true) |
| 808 if err != nil { | 832 if err != nil { |
| 809 linkMessage = cleanCompileOutput(linkMessage, hash) | 833 linkMessage = cleanCompileOutput(linkMessage, hash) |
| 810 reportTryError(w, r, err, linkMessage, hash) | 834 reportTryError(w, r, err, linkMessage, hash) |
| 811 return | 835 return |
| 812 } | 836 } |
| 813 message += linkMessage | 837 message += linkMessage |
| 814 cmd := hash + " --out " + hash + ".png" | 838 cmd := hash + " --out " + hash + ".png" |
| 815 if request.Source > 0 { | 839 if request.Source > 0 { |
| 816 cmd += fmt.Sprintf(" --source image-%d.png", request.So urce) | 840 cmd += fmt.Sprintf(" --source image-%d.png", request.So urce) |
| 817 } | 841 } |
| 818 if *useChroot { | 842 if *useChroot { |
| 819 » » » cmd = "schroot -c webtry --directory=/inout -- /inout/" + cmd | 843 » » » cmd = "schroot -c webtry --directory=/inout -- /inout/Re lease/" + cmd |
| 820 } else { | 844 } else { |
| 821 » » » abs, err := filepath.Abs("../../../inout") | 845 » » » abs, err := filepath.Abs("../../../inout/Release") |
| 822 if err != nil { | 846 if err != nil { |
| 823 reportTryError(w, r, err, "Failed to find execut able directory.", hash) | 847 reportTryError(w, r, err, "Failed to find execut able directory.", hash) |
| 824 return | 848 return |
| 825 } | 849 } |
| 826 cmd = abs + "/" + cmd | 850 cmd = abs + "/" + cmd |
| 827 } | 851 } |
| 828 | 852 |
| 829 execMessage, err := doCmd(cmd, false) | 853 execMessage, err := doCmd(cmd, false) |
| 830 if err != nil { | 854 if err != nil { |
| 831 reportTryError(w, r, err, "Failed to run the code:\n"+ex ecMessage, hash) | 855 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)) | 887 http.HandleFunc("/sources/", autogzip.HandleFunc(sourcesHandler)) |
| 864 | 888 |
| 865 // Resources are served directly | 889 // Resources are served directly |
| 866 // TODO add support for caching/etags/gzip | 890 // TODO add support for caching/etags/gzip |
| 867 http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./")))) | 891 http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./")))) |
| 868 | 892 |
| 869 // TODO Break out /c/ as it's own handler. | 893 // TODO Break out /c/ as it's own handler. |
| 870 http.HandleFunc("/", autogzip.HandleFunc(mainHandler)) | 894 http.HandleFunc("/", autogzip.HandleFunc(mainHandler)) |
| 871 log.Fatal(http.ListenAndServe(*port, nil)) | 895 log.Fatal(http.ListenAndServe(*port, nil)) |
| 872 } | 896 } |
| OLD | NEW |