| 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 | |
| 40 DEFAULT_SAMPLE = `void draw(SkCanvas* canvas) { | 39 DEFAULT_SAMPLE = `void draw(SkCanvas* canvas) { |
| 41 SkPaint p; | 40 SkPaint p; |
| 42 p.setColor(SK_ColorRED); | 41 p.setColor(SK_ColorRED); |
| 43 p.setAntiAlias(true); | 42 p.setAntiAlias(true); |
| 44 p.setStyle(SkPaint::kStroke_Style); | 43 p.setStyle(SkPaint::kStroke_Style); |
| 45 p.setStrokeWidth(10); | 44 p.setStrokeWidth(10); |
| 46 | 45 |
| 47 canvas->drawLine(20, 20, 100, 100, p); | 46 canvas->drawLine(20, 20, 100, 100, p); |
| 48 }` | 47 }` |
| 49 // Don't increase above 2^16 w/o altering the db tables to accept someth
ing bigger than TEXT. | 48 // Don't increase above 2^16 w/o altering the db tables to accept someth
ing bigger than TEXT. |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 return t.Execute(f, context) | 360 return t.Execute(f, context) |
| 362 } | 361 } |
| 363 | 362 |
| 364 // expandToFile expands the template and writes the result to the file. | 363 // expandToFile expands the template and writes the result to the file. |
| 365 func expandToFile(filename string, code string, t *template.Template) error { | 364 func expandToFile(filename string, code string, t *template.Template) error { |
| 366 return writeTemplate(filename, t, userCode{Code: code, Titlebar: Titleba
r{GitHash: gitHash, GitInfo: gitInfo}}) | 365 return writeTemplate(filename, t, userCode{Code: code, Titlebar: Titleba
r{GitHash: gitHash, GitInfo: gitInfo}}) |
| 367 } | 366 } |
| 368 | 367 |
| 369 // expandCode expands the template into a file and calculates the MD5 hash. | 368 // expandCode expands the template into a file and calculates the MD5 hash. |
| 370 func expandCode(code string, source int) (string, error) { | 369 func expandCode(code string, source int) (string, error) { |
| 370 // in order to support fonts in the chroot jail, we need to make sure |
| 371 // we're using portable typefaces. |
| 372 // TODO(humper): Make this more robust, supporting things like setTypef
ace |
| 373 |
| 374 inputCodeLines := strings.Split(code, "\n") |
| 375 outputCodeLines := []string{} |
| 376 for _, line := range inputCodeLines { |
| 377 outputCodeLines = append(outputCodeLines, line) |
| 378 if strings.HasPrefix(strings.TrimSpace(line), "SkPaint ") { |
| 379 outputCodeLines = append(outputCodeLines, "sk_tool_utils
::set_portable_typeface(&p);") |
| 380 } |
| 381 } |
| 382 |
| 383 fontFriendlyCode := strings.Join(outputCodeLines, "\n") |
| 384 |
| 371 h := md5.New() | 385 h := md5.New() |
| 372 » h.Write([]byte(code)) | 386 » h.Write([]byte(fontFriendlyCode)) |
| 373 binary.Write(h, binary.LittleEndian, int64(source)) | 387 binary.Write(h, binary.LittleEndian, int64(source)) |
| 374 hash := fmt.Sprintf("%x", h.Sum(nil)) | 388 hash := fmt.Sprintf("%x", h.Sum(nil)) |
| 375 // At this point we are running in skia/experimental/webtry, making cach
e a | 389 // At this point we are running in skia/experimental/webtry, making cach
e a |
| 376 // peer directory to skia. | 390 // peer directory to skia. |
| 377 // TODO(jcgregorio) Make all relative directories into flags. | 391 // TODO(jcgregorio) Make all relative directories into flags. |
| 378 » err := expandToFile(fmt.Sprintf("../../../cache/src/%s.cpp", hash), code
, codeTemplate) | 392 » err := expandToFile(fmt.Sprintf("../../../cache/src/%s.cpp", hash), font
FriendlyCode, codeTemplate) |
| 379 return hash, err | 393 return hash, err |
| 380 } | 394 } |
| 381 | 395 |
| 382 // expandGyp produces the GYP file needed to build the code | 396 // expandGyp produces the GYP file needed to build the code |
| 383 func expandGyp(hash string) error { | 397 func expandGyp(hash string) error { |
| 384 return writeTemplate(fmt.Sprintf("../../../cache/%s.gyp", hash), gypTemp
late, struct{ Hash string }{hash}) | 398 return writeTemplate(fmt.Sprintf("../../../cache/%s.gyp", hash), gypTemp
late, struct{ Hash string }{hash}) |
| 385 } | 399 } |
| 386 | 400 |
| 387 // response is serialized to JSON as a response to POSTs. | 401 // response is serialized to JSON as a response to POSTs. |
| 388 type response struct { | 402 type response struct { |
| (...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 810 } | 824 } |
| 811 writeToDatabase(hash, request.Code, request.Name, request.Source
) | 825 writeToDatabase(hash, request.Code, request.Name, request.Source
) |
| 812 err = expandGyp(hash) | 826 err = expandGyp(hash) |
| 813 if err != nil { | 827 if err != nil { |
| 814 reportTryError(w, r, err, "Failed to write the gyp file.
", hash) | 828 reportTryError(w, r, err, "Failed to write the gyp file.
", hash) |
| 815 return | 829 return |
| 816 } | 830 } |
| 817 cmd := "scripts/fiddle_wrapper " + hash | 831 cmd := "scripts/fiddle_wrapper " + hash |
| 818 if *useChroot { | 832 if *useChroot { |
| 819 cmd = "schroot -c webtry --directory=/ -- /skia_build/sk
ia/experimental/webtry/" + cmd | 833 cmd = "schroot -c webtry --directory=/ -- /skia_build/sk
ia/experimental/webtry/" + cmd |
| 820 » » } | 834 » » } |
| 821 if request.Source > 0 { | 835 if request.Source > 0 { |
| 822 cmd += fmt.Sprintf(" image-%d.png", request.Source) | 836 cmd += fmt.Sprintf(" image-%d.png", request.Source) |
| 823 } | 837 } |
| 824 | 838 |
| 825 message, err := doCmd(cmd) | 839 message, err := doCmd(cmd) |
| 826 if err != nil { | 840 if err != nil { |
| 827 reportTryError(w, r, err, "Failed to run the code:\n"+me
ssage, hash) | 841 reportTryError(w, r, err, "Failed to run the code:\n"+me
ssage, hash) |
| 828 return | 842 return |
| 829 } | 843 } |
| 830 png, err := ioutil.ReadFile("../../../inout/" + hash + ".png") | 844 png, err := ioutil.ReadFile("../../../inout/" + hash + ".png") |
| (...skipping 27 matching lines...) Expand all Loading... |
| 858 http.HandleFunc("/sources/", autogzip.HandleFunc(sourcesHandler)) | 872 http.HandleFunc("/sources/", autogzip.HandleFunc(sourcesHandler)) |
| 859 | 873 |
| 860 // Resources are served directly | 874 // Resources are served directly |
| 861 // TODO add support for caching/etags/gzip | 875 // TODO add support for caching/etags/gzip |
| 862 http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./")))) | 876 http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./")))) |
| 863 | 877 |
| 864 // TODO Break out /c/ as it's own handler. | 878 // TODO Break out /c/ as it's own handler. |
| 865 http.HandleFunc("/", autogzip.HandleFunc(mainHandler)) | 879 http.HandleFunc("/", autogzip.HandleFunc(mainHandler)) |
| 866 log.Fatal(http.ListenAndServe(*port, nil)) | 880 log.Fatal(http.ListenAndServe(*port, nil)) |
| 867 } | 881 } |
| OLD | NEW |