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/json" | 8 "encoding/json" |
| 9 "flag" | 9 "flag" |
| 10 "fmt" | 10 "fmt" |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 39 MAX_TRY_SIZE = 64000 | 39 MAX_TRY_SIZE = 64000 |
| 40 ) | 40 ) |
| 41 | 41 |
| 42 var ( | 42 var ( |
| 43 // codeTemplate is the cpp code template the user's code is copied into. | 43 // codeTemplate is the cpp code template the user's code is copied into. |
| 44 codeTemplate *template.Template = nil | 44 codeTemplate *template.Template = nil |
| 45 | 45 |
| 46 // indexTemplate is the main index.html page we serve. | 46 // indexTemplate is the main index.html page we serve. |
| 47 indexTemplate *htemplate.Template = nil | 47 indexTemplate *htemplate.Template = nil |
| 48 | 48 |
| 49 // iframeTemplate is the main index.html page we serve. | |
| 50 iframeTemplate *htemplate.Template = nil | |
| 51 | |
| 49 // recentTemplate is a list of recent images. | 52 // recentTemplate is a list of recent images. |
| 50 recentTemplate *htemplate.Template = nil | 53 recentTemplate *htemplate.Template = nil |
| 51 | 54 |
| 52 // workspaceTemplate is the page for workspaces, a series of webtrys. | 55 // workspaceTemplate is the page for workspaces, a series of webtrys. |
| 53 workspaceTemplate *htemplate.Template = nil | 56 workspaceTemplate *htemplate.Template = nil |
| 54 | 57 |
| 55 // db is the database, nil if we don't have an SQL database to store dat a into. | 58 // db is the database, nil if we don't have an SQL database to store dat a into. |
| 56 db *sql.DB = nil | 59 db *sql.DB = nil |
| 57 | 60 |
| 58 // directLink is the regex that matches URLs paths that are direct links . | 61 // directLink is the regex that matches URLs paths that are direct links . |
| 59 directLink = regexp.MustCompile("^/c/([a-f0-9]+)$") | 62 directLink = regexp.MustCompile("^/c/([a-f0-9]+)$") |
| 60 | 63 |
| 64 // iframeLink is the regex that matches URLs paths that are links to ifr ames. | |
| 65 iframeLink = regexp.MustCompile("^/iframe/([a-f0-9]+)$") | |
| 66 | |
| 61 // imageLink is the regex that matches URLs paths that are direct links to PNGs. | 67 // imageLink is the regex that matches URLs paths that are direct links to PNGs. |
| 62 imageLink = regexp.MustCompile("^/i/([a-f0-9]+.png)$") | 68 imageLink = regexp.MustCompile("^/i/([a-f0-9]+.png)$") |
| 63 | 69 |
| 64 // workspaceLink is the regex that matches URLs paths for workspaces. | 70 // workspaceLink is the regex that matches URLs paths for workspaces. |
| 65 workspaceLink = regexp.MustCompile("^/w/([a-z0-9-]+)$") | 71 workspaceLink = regexp.MustCompile("^/w/([a-z0-9-]+)$") |
| 66 | 72 |
| 67 // workspaceNameAdj is a list of adjectives for building workspace names . | 73 // workspaceNameAdj is a list of adjectives for building workspace names . |
| 68 workspaceNameAdj = []string{ | 74 workspaceNameAdj = []string{ |
| 69 "autumn", "hidden", "bitter", "misty", "silent", "empty", "dry", "dark", | 75 "autumn", "hidden", "bitter", "misty", "silent", "empty", "dry", "dark", |
| 70 "summer", "icy", "delicate", "quiet", "white", "cool", "spring", "winter", | 76 "summer", "icy", "delicate", "quiet", "white", "cool", "spring", "winter", |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 102 lines := strings.Split(c, "\n") | 108 lines := strings.Split(c, "\n") |
| 103 ret := []string{} | 109 ret := []string{} |
| 104 for i, line := range lines { | 110 for i, line := range lines { |
| 105 ret = append(ret, fmt.Sprintf("#line %d", i+1)) | 111 ret = append(ret, fmt.Sprintf("#line %d", i+1)) |
| 106 ret = append(ret, line) | 112 ret = append(ret, line) |
| 107 } | 113 } |
| 108 return strings.Join(ret, "\n") | 114 return strings.Join(ret, "\n") |
| 109 } | 115 } |
| 110 | 116 |
| 111 func init() { | 117 func init() { |
| 118 rand.Seed(time.Now().UnixNano()) | |
|
mtklein
2014/04/18 16:43:55
Goes with the other CL?
jcgregorio
2014/04/18 17:00:11
Yeah, caught this in this CL. Since their both goi
mtklein
2014/04/18 17:03:01
SGTM
| |
| 112 | 119 |
| 113 // Change the current working directory to the directory of the executab le. | 120 // Change the current working directory to the directory of the executab le. |
| 114 var err error | 121 var err error |
| 115 cwd, err := filepath.Abs(filepath.Dir(os.Args[0])) | 122 cwd, err := filepath.Abs(filepath.Dir(os.Args[0])) |
| 116 if err != nil { | 123 if err != nil { |
| 117 log.Fatal(err) | 124 log.Fatal(err) |
| 118 } | 125 } |
| 119 os.Chdir(cwd) | 126 os.Chdir(cwd) |
| 120 | 127 |
| 121 codeTemplate, err = template.ParseFiles(filepath.Join(cwd, "templates/te mplate.cpp")) | 128 codeTemplate, err = template.ParseFiles(filepath.Join(cwd, "templates/te mplate.cpp")) |
| 122 if err != nil { | 129 if err != nil { |
| 123 panic(err) | 130 panic(err) |
| 124 } | 131 } |
| 125 // Convert index.html into a template, which is expanded with the code. | |
| 126 indexTemplate, err = htemplate.ParseFiles( | 132 indexTemplate, err = htemplate.ParseFiles( |
| 127 filepath.Join(cwd, "templates/index.html"), | 133 filepath.Join(cwd, "templates/index.html"), |
| 128 filepath.Join(cwd, "templates/titlebar.html"), | 134 filepath.Join(cwd, "templates/titlebar.html"), |
| 129 ) | 135 ) |
| 130 if err != nil { | 136 if err != nil { |
| 131 panic(err) | 137 panic(err) |
| 132 } | 138 } |
| 139 iframeTemplate, err = htemplate.ParseFiles( | |
| 140 filepath.Join(cwd, "templates/iframe.html"), | |
| 141 ) | |
| 142 if err != nil { | |
| 143 panic(err) | |
| 144 } | |
| 133 recentTemplate, err = htemplate.ParseFiles( | 145 recentTemplate, err = htemplate.ParseFiles( |
| 134 filepath.Join(cwd, "templates/recent.html"), | 146 filepath.Join(cwd, "templates/recent.html"), |
| 135 filepath.Join(cwd, "templates/titlebar.html"), | 147 filepath.Join(cwd, "templates/titlebar.html"), |
| 136 ) | 148 ) |
| 137 if err != nil { | 149 if err != nil { |
| 138 panic(err) | 150 panic(err) |
| 139 } | 151 } |
| 140 workspaceTemplate, err = htemplate.ParseFiles( | 152 workspaceTemplate, err = htemplate.ParseFiles( |
| 141 filepath.Join(cwd, "templates/workspace.html"), | 153 filepath.Join(cwd, "templates/workspace.html"), |
| 142 filepath.Join(cwd, "templates/titlebar.html"), | 154 filepath.Join(cwd, "templates/titlebar.html"), |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 198 FOREIGN KEY (name) REFERENCES workspace(name) | 210 FOREIGN KEY (name) REFERENCES workspace(name) |
| 199 )` | 211 )` |
| 200 _, err = db.Exec(sql) | 212 _, err = db.Exec(sql) |
| 201 log.Printf("Info: status creating sqlite table for workspace try : %q\n", err) | 213 log.Printf("Info: status creating sqlite table for workspace try : %q\n", err) |
| 202 } | 214 } |
| 203 } | 215 } |
| 204 | 216 |
| 205 // userCode is used in template expansion. | 217 // userCode is used in template expansion. |
| 206 type userCode struct { | 218 type userCode struct { |
| 207 UserCode string | 219 UserCode string |
| 220 Hash string | |
| 208 } | 221 } |
| 209 | 222 |
| 210 // expandToFile expands the template and writes the result to the file. | 223 // expandToFile expands the template and writes the result to the file. |
| 211 func expandToFile(filename string, code string, t *template.Template) error { | 224 func expandToFile(filename string, code string, t *template.Template) error { |
| 212 f, err := os.Create(filename) | 225 f, err := os.Create(filename) |
| 213 if err != nil { | 226 if err != nil { |
| 214 return err | 227 return err |
| 215 } | 228 } |
| 216 defer f.Close() | 229 defer f.Close() |
| 217 return t.Execute(f, userCode{UserCode: code}) | 230 return t.Execute(f, userCode{UserCode: code}) |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 447 } | 460 } |
| 448 } | 461 } |
| 449 return false | 462 return false |
| 450 } | 463 } |
| 451 | 464 |
| 452 type TryRequest struct { | 465 type TryRequest struct { |
| 453 Code string `json:"code"` | 466 Code string `json:"code"` |
| 454 Name string `json:"name"` | 467 Name string `json:"name"` |
| 455 } | 468 } |
| 456 | 469 |
| 470 // iframeHandler handles the GET and POST of the main page. | |
| 471 func iframeHandler(w http.ResponseWriter, r *http.Request) { | |
| 472 log.Printf("IFrame Handler: %q\n", r.URL.Path) | |
| 473 if r.Method != "GET" { | |
| 474 http.NotFound(w, r) | |
| 475 return | |
| 476 } | |
| 477 match := iframeLink.FindStringSubmatch(r.URL.Path) | |
| 478 if len(match) != 2 { | |
| 479 http.NotFound(w, r) | |
| 480 return | |
| 481 } | |
| 482 hash := match[1] | |
| 483 if db == nil { | |
| 484 http.NotFound(w, r) | |
| 485 return | |
| 486 } | |
| 487 var code string | |
| 488 // Load 'code' with the code found in the database. | |
| 489 if err := db.QueryRow("SELECT code FROM webtry WHERE hash=?", hash).Scan (&code); err != nil { | |
| 490 http.NotFound(w, r) | |
| 491 return | |
| 492 } | |
| 493 // Expand the template. | |
| 494 if err := iframeTemplate.Execute(w, userCode{UserCode: code, Hash: hash} ); err != nil { | |
| 495 log.Printf("ERROR: Failed to expand template: %q\n", err) | |
| 496 } | |
| 497 } | |
| 498 | |
| 457 // mainHandler handles the GET and POST of the main page. | 499 // mainHandler handles the GET and POST of the main page. |
| 458 func mainHandler(w http.ResponseWriter, r *http.Request) { | 500 func mainHandler(w http.ResponseWriter, r *http.Request) { |
| 459 log.Printf("Main Handler: %q\n", r.URL.Path) | 501 log.Printf("Main Handler: %q\n", r.URL.Path) |
| 460 if r.Method == "GET" { | 502 if r.Method == "GET" { |
| 461 code := DEFAULT_SAMPLE | 503 code := DEFAULT_SAMPLE |
| 462 match := directLink.FindStringSubmatch(r.URL.Path) | 504 match := directLink.FindStringSubmatch(r.URL.Path) |
| 463 if len(match) == 2 && r.URL.Path != "/" { | 505 if len(match) == 2 && r.URL.Path != "/" { |
| 464 hash := match[1] | 506 hash := match[1] |
| 465 if db == nil { | 507 if db == nil { |
| 466 http.NotFound(w, r) | 508 http.NotFound(w, r) |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 551 } | 593 } |
| 552 w.Write(resp) | 594 w.Write(resp) |
| 553 } | 595 } |
| 554 } | 596 } |
| 555 | 597 |
| 556 func main() { | 598 func main() { |
| 557 flag.Parse() | 599 flag.Parse() |
| 558 http.HandleFunc("/i/", imageHandler) | 600 http.HandleFunc("/i/", imageHandler) |
| 559 http.HandleFunc("/w/", workspaceHandler) | 601 http.HandleFunc("/w/", workspaceHandler) |
| 560 http.HandleFunc("/recent/", recentHandler) | 602 http.HandleFunc("/recent/", recentHandler) |
| 603 http.HandleFunc("/iframe/", iframeHandler) | |
| 561 http.HandleFunc("/css/", cssHandler) | 604 http.HandleFunc("/css/", cssHandler) |
| 562 http.HandleFunc("/js/", jsHandler) | 605 http.HandleFunc("/js/", jsHandler) |
| 563 http.HandleFunc("/", mainHandler) | 606 http.HandleFunc("/", mainHandler) |
| 564 log.Fatal(http.ListenAndServe(*port, nil)) | 607 log.Fatal(http.ListenAndServe(*port, nil)) |
| 565 } | 608 } |
| OLD | NEW |