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 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 if err != nil { | 237 if err != nil { |
238 glog.Errorf("Creating source_images table failed: %s", e
rr) | 238 glog.Errorf("Creating source_images table failed: %s", e
rr) |
239 } | 239 } |
240 | 240 |
241 sql = `CREATE TABLE IF NOT EXISTS webtry ( | 241 sql = `CREATE TABLE IF NOT EXISTS webtry ( |
242 code TEXT DEFAULT '' NOT NULL, | 242 code TEXT DEFAULT '' NOT NULL, |
243 create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, | 243 create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
244 hash CHAR(64) DEFAULT '' NOT NULL, | 244 hash CHAR(64) DEFAULT '' NOT NULL, |
245 width INTEGER DEFAULT 256 NOT NULL, | 245 width INTEGER DEFAULT 256 NOT NULL, |
246 height INTEGER DEFAULT 256 NOT NULL, | 246 height INTEGER DEFAULT 256 NOT NULL, |
247 gpu BOOL DEFAULT 0 NOT NULL, | |
248 source_image_id INTEGER DEFAULT 0 NOT NULL, | 247 source_image_id INTEGER DEFAULT 0 NOT NULL, |
249 | 248 |
250 PRIMARY KEY(hash) | 249 PRIMARY KEY(hash) |
251 )` | 250 )` |
252 _, err = db.Exec(sql) | 251 _, err = db.Exec(sql) |
253 if err != nil { | 252 if err != nil { |
254 glog.Errorf("Creating webtry table failed: %s", err) | 253 glog.Errorf("Creating webtry table failed: %s", err) |
255 } | 254 } |
256 | 255 |
257 sql = `CREATE TABLE IF NOT EXISTS workspace ( | 256 sql = `CREATE TABLE IF NOT EXISTS workspace ( |
258 name CHAR(64) DEFAULT '' NOT NULL, | 257 name CHAR(64) DEFAULT '' NOT NULL, |
259 create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, | 258 create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
260 PRIMARY KEY(name) | 259 PRIMARY KEY(name) |
261 )` | 260 )` |
262 _, err = db.Exec(sql) | 261 _, err = db.Exec(sql) |
263 if err != nil { | 262 if err != nil { |
264 glog.Errorf("Creating workspace table failed: %s", err) | 263 glog.Errorf("Creating workspace table failed: %s", err) |
265 } | 264 } |
266 | 265 |
267 sql = `CREATE TABLE IF NOT EXISTS workspacetry ( | 266 sql = `CREATE TABLE IF NOT EXISTS workspacetry ( |
268 name CHAR(64) DEFAULT '' NOT NULL, | 267 name CHAR(64) DEFAULT '' NOT NULL, |
269 create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, | 268 create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
270 hash CHAR(64) DEFAULT '' NOT NULL, | 269 hash CHAR(64) DEFAULT '' NOT NULL, |
271 width INTEGER DEFAULT 256 NOT NULL, | 270 width INTEGER DEFAULT 256 NOT NULL, |
272 height INTEGER DEFAULT 256 NOT NULL, | 271 height INTEGER DEFAULT 256 NOT NULL, |
273 gpu BOOL DEFAULT 0 NOT NULL, | |
274 hidden INTEGER DEFAULT 0 NOT NULL, | 272 hidden INTEGER DEFAULT 0 NOT NULL, |
275 source_image_id INTEGER DEFAULT 0 NOT NULL, | 273 source_image_id INTEGER DEFAULT 0 NOT NULL, |
276 | 274 |
277 FOREIGN KEY (name) REFERENCES workspace(name) | 275 FOREIGN KEY (name) REFERENCES workspace(name) |
278 )` | 276 )` |
279 _, err = db.Exec(sql) | 277 _, err = db.Exec(sql) |
280 if err != nil { | 278 if err != nil { |
281 glog.Errorf("Creating workspacetry table failed: %s", er
r) | 279 glog.Errorf("Creating workspacetry table failed: %s", er
r) |
282 } | 280 } |
283 } | 281 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 GitHash string | 334 GitHash string |
337 GitInfo string | 335 GitInfo string |
338 } | 336 } |
339 | 337 |
340 // userCode is used in template expansion. | 338 // userCode is used in template expansion. |
341 type userCode struct { | 339 type userCode struct { |
342 Code string | 340 Code string |
343 Hash string | 341 Hash string |
344 Width int | 342 Width int |
345 Height int | 343 Height int |
346 GPU bool | |
347 Source int | 344 Source int |
348 Titlebar Titlebar | 345 Titlebar Titlebar |
349 } | 346 } |
350 | 347 |
351 // writeTemplate creates a given output file and writes the template | 348 // writeTemplate creates a given output file and writes the template |
352 // result there. | 349 // result there. |
353 func writeTemplate(filename string, t *template.Template, context interface{}) e
rror { | 350 func writeTemplate(filename string, t *template.Template, context interface{}) e
rror { |
354 f, err := os.Create(filename) | 351 f, err := os.Create(filename) |
355 if err != nil { | 352 if err != nil { |
356 return err | 353 return err |
357 } | 354 } |
358 defer f.Close() | 355 defer f.Close() |
359 return t.Execute(f, context) | 356 return t.Execute(f, context) |
360 } | 357 } |
361 | 358 |
362 // expandToFile expands the template and writes the result to the file. | 359 // expandToFile expands the template and writes the result to the file. |
363 func expandToFile(filename string, code string, t *template.Template) error { | 360 func expandToFile(filename string, code string, t *template.Template) error { |
364 return writeTemplate(filename, t, userCode{ | 361 return writeTemplate(filename, t, userCode{ |
365 Code: code, | 362 Code: code, |
366 Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo}, | 363 Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo}, |
367 }) | 364 }) |
368 } | 365 } |
369 | 366 |
370 // expandCode expands the template into a file and calculates the MD5 hash. | 367 // expandCode expands the template into a file and calculates the MD5 hash. |
371 // We include the width and height here so that a single hash can capture | 368 // We include the width and height here so that a single hash can capture |
372 // both the code and the supplied width/height parameters. | 369 // both the code and the supplied width/height parameters. |
373 func expandCode(code string, source int, width, height int, gpu bool) (string, e
rror) { | 370 func expandCode(code string, source int, width, height int) (string, error) { |
374 // in order to support fonts in the chroot jail, we need to make sure | 371 // in order to support fonts in the chroot jail, we need to make sure |
375 // we're using portable typefaces. | 372 // we're using portable typefaces. |
376 // TODO(humper): Make this more robust, supporting things like setTypef
ace | 373 // TODO(humper): Make this more robust, supporting things like setTypef
ace |
377 | 374 |
378 inputCodeLines := strings.Split(code, "\n") | 375 inputCodeLines := strings.Split(code, "\n") |
379 outputCodeLines := []string{ | 376 outputCodeLines := []string{ |
380 "DECLARE_bool(portableFonts);", | 377 "DECLARE_bool(portableFonts);", |
381 fmt.Sprintf("// WxH: %d, %d", width, height), | 378 fmt.Sprintf("// WxH: %d, %d", width, height), |
382 fmt.Sprintf("// GPU: %v", gpu), | |
383 } | 379 } |
384 for _, line := range inputCodeLines { | 380 for _, line := range inputCodeLines { |
385 outputCodeLines = append(outputCodeLines, line) | 381 outputCodeLines = append(outputCodeLines, line) |
386 if strings.HasPrefix(strings.TrimSpace(line), "SkPaint p") { | 382 if strings.HasPrefix(strings.TrimSpace(line), "SkPaint p") { |
387 outputCodeLines = append(outputCodeLines, "FLAGS_portabl
eFonts = true;") | 383 outputCodeLines = append(outputCodeLines, "FLAGS_portabl
eFonts = true;") |
388 outputCodeLines = append(outputCodeLines, "sk_tool_utils
::set_portable_typeface(&p, \"Helvetica\", SkTypeface::kNormal);") | 384 outputCodeLines = append(outputCodeLines, "sk_tool_utils
::set_portable_typeface(&p, \"Helvetica\", SkTypeface::kNormal);") |
389 } | 385 } |
390 } | 386 } |
391 | 387 |
392 fontFriendlyCode := strings.Join(outputCodeLines, "\n") | 388 fontFriendlyCode := strings.Join(outputCodeLines, "\n") |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 resp, err := json.Marshal(m) | 465 resp, err := json.Marshal(m) |
470 | 466 |
471 if err != nil { | 467 if err != nil { |
472 http.Error(w, "Failed to serialize a response", 500) | 468 http.Error(w, "Failed to serialize a response", 500) |
473 return | 469 return |
474 } | 470 } |
475 w.Header().Set("Content-Type", "text/plain") | 471 w.Header().Set("Content-Type", "text/plain") |
476 w.Write(resp) | 472 w.Write(resp) |
477 } | 473 } |
478 | 474 |
479 func writeToDatabase(hash string, code string, workspaceName string, source int,
width, height int, gpu bool) { | 475 func writeToDatabase(hash string, code string, workspaceName string, source int,
width, height int) { |
480 if db == nil { | 476 if db == nil { |
481 return | 477 return |
482 } | 478 } |
483 » if _, err := db.Exec("INSERT INTO webtry (code, hash, width, height, gpu
, source_image_id) VALUES(?, ?, ?, ?, ?, ?)", code, hash, width, height, gpu, so
urce); err != nil { | 479 » if _, err := db.Exec("INSERT INTO webtry (code, hash, width, height, sou
rce_image_id) VALUES(?, ?, ?, ?, ?)", code, hash, width, height, source); err !=
nil { |
484 glog.Errorf("Failed to insert code into database: %q\n", err) | 480 glog.Errorf("Failed to insert code into database: %q\n", err) |
485 } | 481 } |
486 if workspaceName != "" { | 482 if workspaceName != "" { |
487 » » if _, err := db.Exec("INSERT INTO workspacetry (name, hash, widt
h, height, gpu, source_image_id) VALUES(?, ?, ?, ?, ?, ?)", workspaceName, hash,
width, height, gpu, source); err != nil { | 483 » » if _, err := db.Exec("INSERT INTO workspacetry (name, hash, widt
h, height, source_image_id) VALUES(?, ?, ?, ?, ?)", workspaceName, hash, width,
height, source); err != nil { |
488 glog.Errorf("Failed to insert into workspacetry table: %
q\n", err) | 484 glog.Errorf("Failed to insert into workspacetry table: %
q\n", err) |
489 } | 485 } |
490 } | 486 } |
491 } | 487 } |
492 | 488 |
493 type Sources struct { | 489 type Sources struct { |
494 Id int `json:"id"` | 490 Id int `json:"id"` |
495 } | 491 } |
496 | 492 |
497 // sourcesHandler serves up the PNG of a specific try. | 493 // sourcesHandler serves up the PNG of a specific try. |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
620 } | 616 } |
621 } | 617 } |
622 | 618 |
623 type Workspace struct { | 619 type Workspace struct { |
624 Name string | 620 Name string |
625 Code string | 621 Code string |
626 Hash string | 622 Hash string |
627 Width int | 623 Width int |
628 Height int | 624 Height int |
629 Source int | 625 Source int |
630 GPU bool | |
631 Tries []Try | 626 Tries []Try |
632 Titlebar Titlebar | 627 Titlebar Titlebar |
633 } | 628 } |
634 | 629 |
635 // newWorkspace generates a new random workspace name and stores it in the datab
ase. | 630 // newWorkspace generates a new random workspace name and stores it in the datab
ase. |
636 func newWorkspace() (string, error) { | 631 func newWorkspace() (string, error) { |
637 for i := 0; i < 10; i++ { | 632 for i := 0; i < 10; i++ { |
638 adj := workspaceNameAdj[rand.Intn(len(workspaceNameAdj))] | 633 adj := workspaceNameAdj[rand.Intn(len(workspaceNameAdj))] |
639 noun := workspaceNameNoun[rand.Intn(len(workspaceNameNoun))] | 634 noun := workspaceNameNoun[rand.Intn(len(workspaceNameNoun))] |
640 suffix := rand.Intn(1000) | 635 suffix := rand.Intn(1000) |
641 name := fmt.Sprintf("%s-%s-%d", adj, noun, suffix) | 636 name := fmt.Sprintf("%s-%s-%d", adj, noun, suffix) |
642 if _, err := db.Exec("INSERT INTO workspace (name) VALUES(?)", n
ame); err == nil { | 637 if _, err := db.Exec("INSERT INTO workspace (name) VALUES(?)", n
ame); err == nil { |
643 return name, nil | 638 return name, nil |
644 } else { | 639 } else { |
645 glog.Errorf("Failed to insert workspace into database: %
q\n", err) | 640 glog.Errorf("Failed to insert workspace into database: %
q\n", err) |
646 } | 641 } |
647 } | 642 } |
648 return "", fmt.Errorf("Failed to create a new workspace") | 643 return "", fmt.Errorf("Failed to create a new workspace") |
649 } | 644 } |
650 | 645 |
651 // getCode returns the code for a given hash, or the empty string if not found. | 646 // getCode returns the code for a given hash, or the empty string if not found. |
652 func getCode(hash string) (string, int, int, int, bool, error) { | 647 func getCode(hash string) (string, int, int, int, error) { |
653 code := "" | 648 code := "" |
654 width := 0 | 649 width := 0 |
655 height := 0 | 650 height := 0 |
656 source := 0 | 651 source := 0 |
657 » gpu := false | 652 » if err := db.QueryRow("SELECT code, width, height, source_image_id FROM
webtry WHERE hash=?", hash).Scan(&code, &width, &height, &source); err != nil { |
658 » if err := db.QueryRow("SELECT code, width, height, gpu, source_image_id
FROM webtry WHERE hash=?", hash).Scan(&code, &width, &height, &gpu, &source); er
r != nil { | |
659 glog.Errorf("Code for hash is missing: %q\n", err) | 653 glog.Errorf("Code for hash is missing: %q\n", err) |
660 » » return code, width, height, source, gpu, err | 654 » » return code, width, height, source, err |
661 } | 655 } |
662 » return code, width, height, source, gpu, nil | 656 » return code, width, height, source, nil |
663 } | 657 } |
664 | 658 |
665 func workspaceHandler(w http.ResponseWriter, r *http.Request) { | 659 func workspaceHandler(w http.ResponseWriter, r *http.Request) { |
666 glog.Infof("Workspace Handler: %q\n", r.URL.Path) | 660 glog.Infof("Workspace Handler: %q\n", r.URL.Path) |
667 if r.Method == "GET" { | 661 if r.Method == "GET" { |
668 tries := []Try{} | 662 tries := []Try{} |
669 match := workspaceLink.FindStringSubmatch(r.URL.Path) | 663 match := workspaceLink.FindStringSubmatch(r.URL.Path) |
670 name := "" | 664 name := "" |
671 if len(match) == 2 { | 665 if len(match) == 2 { |
672 name = match[1] | 666 name = match[1] |
(...skipping 12 matching lines...) Expand all Loading... |
685 continue | 679 continue |
686 } | 680 } |
687 tries = append(tries, Try{Hash: hash, Source: so
urce, CreateTS: create_ts.Format("2006-02-01")}) | 681 tries = append(tries, Try{Hash: hash, Source: so
urce, CreateTS: create_ts.Format("2006-02-01")}) |
688 } | 682 } |
689 } | 683 } |
690 var code string | 684 var code string |
691 var hash string | 685 var hash string |
692 var width int | 686 var width int |
693 var height int | 687 var height int |
694 source := 0 | 688 source := 0 |
695 gpu := false | |
696 if len(tries) == 0 { | 689 if len(tries) == 0 { |
697 code = DEFAULT_SAMPLE | 690 code = DEFAULT_SAMPLE |
698 width = 256 | 691 width = 256 |
699 height = 256 | 692 height = 256 |
700 } else { | 693 } else { |
701 hash = tries[len(tries)-1].Hash | 694 hash = tries[len(tries)-1].Hash |
702 » » » code, width, height, source, gpu, _ = getCode(hash) | 695 » » » code, width, height, source, _ = getCode(hash) |
703 } | 696 } |
704 w.Header().Set("Content-Type", "text/html") | 697 w.Header().Set("Content-Type", "text/html") |
705 » » if err := workspaceTemplate.Execute(w, Workspace{Tries: tries, C
ode: code, Name: name, Hash: hash, Width: width, Height: height, GPU: gpu, Sourc
e: source, Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo}}); err != nil
{ | 698 » » if err := workspaceTemplate.Execute(w, Workspace{Tries: tries, C
ode: code, Name: name, Hash: hash, Width: width, Height: height, Source: source,
Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo}}); err != nil { |
706 glog.Errorf("Failed to expand template: %q\n", err) | 699 glog.Errorf("Failed to expand template: %q\n", err) |
707 } | 700 } |
708 } else if r.Method == "POST" { | 701 } else if r.Method == "POST" { |
709 name, err := newWorkspace() | 702 name, err := newWorkspace() |
710 if err != nil { | 703 if err != nil { |
711 http.Error(w, "Failed to create a new workspace.", 500) | 704 http.Error(w, "Failed to create a new workspace.", 500) |
712 return | 705 return |
713 } | 706 } |
714 http.Redirect(w, r, "/w/"+name, 302) | 707 http.Redirect(w, r, "/w/"+name, 302) |
715 } | 708 } |
(...skipping 30 matching lines...) Expand all Loading... |
746 if len(match) != 2 { | 739 if len(match) != 2 { |
747 http.NotFound(w, r) | 740 http.NotFound(w, r) |
748 return | 741 return |
749 } | 742 } |
750 hash := match[1] | 743 hash := match[1] |
751 if db == nil { | 744 if db == nil { |
752 http.NotFound(w, r) | 745 http.NotFound(w, r) |
753 return | 746 return |
754 } | 747 } |
755 var code string | 748 var code string |
756 » code, width, height, source, gpu, err := getCode(hash) | 749 » code, width, height, source, err := getCode(hash) |
757 if err != nil { | 750 if err != nil { |
758 http.NotFound(w, r) | 751 http.NotFound(w, r) |
759 return | 752 return |
760 } | 753 } |
761 // Expand the template. | 754 // Expand the template. |
762 w.Header().Set("Content-Type", "text/html") | 755 w.Header().Set("Content-Type", "text/html") |
763 » if err := iframeTemplate.Execute(w, userCode{Code: code, Width: width, H
eight: height, GPU: gpu, Hash: hash, Source: source}); err != nil { | 756 » if err := iframeTemplate.Execute(w, userCode{Code: code, Width: width, H
eight: height, Hash: hash, Source: source}); err != nil { |
764 glog.Errorf("Failed to expand template: %q\n", err) | 757 glog.Errorf("Failed to expand template: %q\n", err) |
765 } | 758 } |
766 } | 759 } |
767 | 760 |
768 type TryInfo struct { | 761 type TryInfo struct { |
769 Hash string `json:"hash"` | 762 Hash string `json:"hash"` |
770 Code string `json:"code"` | 763 Code string `json:"code"` |
771 Width int `json:"width"` | 764 Width int `json:"width"` |
772 Height int `json:"height"` | 765 Height int `json:"height"` |
773 GPU bool `json:"gpu"` | |
774 Source int `json:"source"` | 766 Source int `json:"source"` |
775 } | 767 } |
776 | 768 |
777 // tryInfoHandler returns information about a specific try. | 769 // tryInfoHandler returns information about a specific try. |
778 func tryInfoHandler(w http.ResponseWriter, r *http.Request) { | 770 func tryInfoHandler(w http.ResponseWriter, r *http.Request) { |
779 glog.Infof("Try Info Handler: %q\n", r.URL.Path) | 771 glog.Infof("Try Info Handler: %q\n", r.URL.Path) |
780 if r.Method != "GET" { | 772 if r.Method != "GET" { |
781 http.NotFound(w, r) | 773 http.NotFound(w, r) |
782 return | 774 return |
783 } | 775 } |
784 match := tryInfoLink.FindStringSubmatch(r.URL.Path) | 776 match := tryInfoLink.FindStringSubmatch(r.URL.Path) |
785 if len(match) != 2 { | 777 if len(match) != 2 { |
786 http.NotFound(w, r) | 778 http.NotFound(w, r) |
787 return | 779 return |
788 } | 780 } |
789 hash := match[1] | 781 hash := match[1] |
790 » code, width, height, source, gpu, err := getCode(hash) | 782 » code, width, height, source, err := getCode(hash) |
791 if err != nil { | 783 if err != nil { |
792 http.NotFound(w, r) | 784 http.NotFound(w, r) |
793 return | 785 return |
794 } | 786 } |
795 m := TryInfo{ | 787 m := TryInfo{ |
796 Hash: hash, | 788 Hash: hash, |
797 Code: code, | 789 Code: code, |
798 Width: width, | 790 Width: width, |
799 Height: height, | 791 Height: height, |
800 GPU: gpu, | |
801 Source: source, | 792 Source: source, |
802 } | 793 } |
803 resp, err := json.Marshal(m) | 794 resp, err := json.Marshal(m) |
804 if err != nil { | 795 if err != nil { |
805 reportError(w, r, err, "Failed to serialize a response.") | 796 reportError(w, r, err, "Failed to serialize a response.") |
806 return | 797 return |
807 } | 798 } |
808 w.Header().Set("Content-Type", "application/json") | 799 w.Header().Set("Content-Type", "application/json") |
809 w.Write(resp) | 800 w.Write(resp) |
810 } | 801 } |
(...skipping 12 matching lines...) Expand all Loading... |
823 | 814 |
824 // mainHandler handles the GET and POST of the main page. | 815 // mainHandler handles the GET and POST of the main page. |
825 func mainHandler(w http.ResponseWriter, r *http.Request) { | 816 func mainHandler(w http.ResponseWriter, r *http.Request) { |
826 glog.Infof("Main Handler: %q\n", r.URL.Path) | 817 glog.Infof("Main Handler: %q\n", r.URL.Path) |
827 requestsCounter.Inc(1) | 818 requestsCounter.Inc(1) |
828 if r.Method == "GET" { | 819 if r.Method == "GET" { |
829 code := DEFAULT_SAMPLE | 820 code := DEFAULT_SAMPLE |
830 source := 0 | 821 source := 0 |
831 width := 256 | 822 width := 256 |
832 height := 256 | 823 height := 256 |
833 gpu := false | |
834 match := directLink.FindStringSubmatch(r.URL.Path) | 824 match := directLink.FindStringSubmatch(r.URL.Path) |
835 var hash string | 825 var hash string |
836 if len(match) == 2 && r.URL.Path != "/" { | 826 if len(match) == 2 && r.URL.Path != "/" { |
837 hash = match[1] | 827 hash = match[1] |
838 if db == nil { | 828 if db == nil { |
839 http.NotFound(w, r) | 829 http.NotFound(w, r) |
840 return | 830 return |
841 } | 831 } |
842 // Update 'code' with the code found in the database. | 832 // Update 'code' with the code found in the database. |
843 » » » if err := db.QueryRow("SELECT code, width, height, gpu,
source_image_id FROM webtry WHERE hash=?", hash).Scan(&code, &width, &height, &g
pu, &source); err != nil { | 833 » » » if err := db.QueryRow("SELECT code, width, height, sourc
e_image_id FROM webtry WHERE hash=?", hash).Scan(&code, &width, &height, &source
); err != nil { |
844 http.NotFound(w, r) | 834 http.NotFound(w, r) |
845 return | 835 return |
846 } | 836 } |
847 } | 837 } |
848 // Expand the template. | 838 // Expand the template. |
849 w.Header().Set("Content-Type", "text/html") | 839 w.Header().Set("Content-Type", "text/html") |
850 » » if err := indexTemplate.Execute(w, userCode{Code: code, Hash: ha
sh, Source: source, Width: width, Height: height, GPU: gpu, Titlebar: Titlebar{G
itHash: gitHash, GitInfo: gitInfo}}); err != nil { | 840 » » if err := indexTemplate.Execute(w, userCode{Code: code, Hash: ha
sh, Source: source, Width: width, Height: height, Titlebar: Titlebar{GitHash: gi
tHash, GitInfo: gitInfo}}); err != nil { |
851 glog.Errorf("Failed to expand template: %q\n", err) | 841 glog.Errorf("Failed to expand template: %q\n", err) |
852 } | 842 } |
853 } else if r.Method == "POST" { | 843 } else if r.Method == "POST" { |
854 w.Header().Set("Content-Type", "application/json") | 844 w.Header().Set("Content-Type", "application/json") |
855 buf := bytes.NewBuffer(make([]byte, 0, MAX_TRY_SIZE)) | 845 buf := bytes.NewBuffer(make([]byte, 0, MAX_TRY_SIZE)) |
856 n, err := buf.ReadFrom(r.Body) | 846 n, err := buf.ReadFrom(r.Body) |
857 if err != nil { | 847 if err != nil { |
858 reportTryError(w, r, err, "Failed to read a request body
.", "") | 848 reportTryError(w, r, err, "Failed to read a request body
.", "") |
859 return | 849 return |
860 } | 850 } |
861 if n == MAX_TRY_SIZE { | 851 if n == MAX_TRY_SIZE { |
862 err := fmt.Errorf("Code length equal to, or exceeded, %d
", MAX_TRY_SIZE) | 852 err := fmt.Errorf("Code length equal to, or exceeded, %d
", MAX_TRY_SIZE) |
863 reportTryError(w, r, err, "Code too large.", "") | 853 reportTryError(w, r, err, "Code too large.", "") |
864 return | 854 return |
865 } | 855 } |
866 request := TryRequest{} | 856 request := TryRequest{} |
867 if err := json.Unmarshal(buf.Bytes(), &request); err != nil { | 857 if err := json.Unmarshal(buf.Bytes(), &request); err != nil { |
868 reportTryError(w, r, err, "Coulnd't decode JSON.", "") | 858 reportTryError(w, r, err, "Coulnd't decode JSON.", "") |
869 return | 859 return |
870 } | 860 } |
871 if hasPreProcessor(request.Code) { | 861 if hasPreProcessor(request.Code) { |
872 err := fmt.Errorf("Found preprocessor macro in code.") | 862 err := fmt.Errorf("Found preprocessor macro in code.") |
873 reportTryError(w, r, err, "Preprocessor macros aren't al
lowed.", "") | 863 reportTryError(w, r, err, "Preprocessor macros aren't al
lowed.", "") |
874 return | 864 return |
875 } | 865 } |
876 » » hash, err := expandCode(LineNumbers(request.Code), request.Sourc
e, request.Width, request.Height, request.GPU) | 866 » » hash, err := expandCode(LineNumbers(request.Code), request.Sourc
e, request.Width, request.Height) |
877 if err != nil { | 867 if err != nil { |
878 reportTryError(w, r, err, "Failed to write the code to c
ompile.", hash) | 868 reportTryError(w, r, err, "Failed to write the code to c
ompile.", hash) |
879 return | 869 return |
880 } | 870 } |
881 » » writeToDatabase(hash, request.Code, request.Name, request.Source
, request.Width, request.Height, request.GPU) | 871 » » writeToDatabase(hash, request.Code, request.Name, request.Source
, request.Width, request.Height) |
882 err = expandGyp(hash) | 872 err = expandGyp(hash) |
883 if err != nil { | 873 if err != nil { |
884 reportTryError(w, r, err, "Failed to write the gyp file.
", hash) | 874 reportTryError(w, r, err, "Failed to write the gyp file.
", hash) |
885 return | 875 return |
886 } | 876 } |
887 cmd := fmt.Sprintf("scripts/fiddle_wrapper %s --width %d --heigh
t %d", hash, request.Width, request.Height) | 877 cmd := fmt.Sprintf("scripts/fiddle_wrapper %s --width %d --heigh
t %d", hash, request.Width, request.Height) |
888 if request.GPU { | 878 if request.GPU { |
889 cmd += " --gpu" | 879 cmd += " --gpu" |
890 } | 880 } |
891 if *useChroot { | 881 if *useChroot { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
961 http.HandleFunc("/sources/", autogzip.HandleFunc(sourcesHandler)) | 951 http.HandleFunc("/sources/", autogzip.HandleFunc(sourcesHandler)) |
962 | 952 |
963 // Resources are served directly | 953 // Resources are served directly |
964 // TODO add support for caching/etags/gzip | 954 // TODO add support for caching/etags/gzip |
965 http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./")))) | 955 http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./")))) |
966 | 956 |
967 // TODO Break out /c/ as it's own handler. | 957 // TODO Break out /c/ as it's own handler. |
968 http.HandleFunc("/", autogzip.HandleFunc(mainHandler)) | 958 http.HandleFunc("/", autogzip.HandleFunc(mainHandler)) |
969 glog.Fatal(http.ListenAndServe(*port, nil)) | 959 glog.Fatal(http.ListenAndServe(*port, nil)) |
970 } | 960 } |
OLD | NEW |