OLD | NEW |
---|---|
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be found | |
3 // in the LICENSE file. | |
4 | |
1 package main | 5 package main |
2 | 6 |
3 import ( | 7 import ( |
4 "database/sql" | 8 "database/sql" |
5 "flag" | 9 "flag" |
6 "fmt" | 10 "fmt" |
7 "html/template" | 11 "html/template" |
8 "io/ioutil" | 12 "io/ioutil" |
9 "log" | 13 "log" |
10 "math/rand" | 14 "math/rand" |
(...skipping 12 matching lines...) Expand all Loading... | |
23 var ( | 27 var ( |
24 // indexTemplate is the main index.html page we serve. | 28 // indexTemplate is the main index.html page we serve. |
25 indexTemplate *template.Template = nil | 29 indexTemplate *template.Template = nil |
26 | 30 |
27 // db is the database, nil if we don't have an SQL database to store dat a into. | 31 // db is the database, nil if we don't have an SQL database to store dat a into. |
28 db *sql.DB = nil | 32 db *sql.DB = nil |
29 ) | 33 ) |
30 | 34 |
31 // flags | 35 // flags |
32 var ( | 36 var ( |
33 » port = flag.String("port", ":8000", "HTTP service address (e.g., ':8000' )") | 37 » port = flag.String("port", ":8000", "HTTP service address (e.g., ' :8000')") |
38 » doOauth = flag.Bool("oauth", true, "Run through the OAuth 2.0 flow on startup.") | |
39 » gitRepoDir = flag.String("git_repo_dir", "../../../skia", "Directory loc ation for the Skia repo.") | |
40 ) | |
41 | |
42 var ( | |
43 » data *Data | |
mtklein
2014/06/15 15:12:12
Having data global smells a little to me. I think
jcgregorio
2014/06/16 02:46:25
Data will be protected with a Mutex once the auto
| |
34 ) | 44 ) |
35 | 45 |
36 func init() { | 46 func init() { |
37 rand.Seed(time.Now().UnixNano()) | 47 rand.Seed(time.Now().UnixNano()) |
38 | 48 |
39 // Change the current working directory to the directory of the executab le. | 49 // Change the current working directory to the directory of the executab le. |
40 var err error | 50 var err error |
41 cwd, err := filepath.Abs(filepath.Dir(os.Args[0])) | 51 cwd, err := filepath.Abs(filepath.Dir(os.Args[0])) |
42 if err != nil { | 52 if err != nil { |
43 log.Fatalln(err) | 53 log.Fatalln(err) |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 }() | 99 }() |
90 } | 100 } |
91 | 101 |
92 // reportError formats an HTTP error response and also logs the detailed error m essage. | 102 // reportError formats an HTTP error response and also logs the detailed error m essage. |
93 func reportError(w http.ResponseWriter, r *http.Request, err error, message stri ng) { | 103 func reportError(w http.ResponseWriter, r *http.Request, err error, message stri ng) { |
94 log.Println("Error:", message, err) | 104 log.Println("Error:", message, err) |
95 w.Header().Set("Content-Type", "text/plain") | 105 w.Header().Set("Content-Type", "text/plain") |
96 http.Error(w, message, 500) | 106 http.Error(w, message, 500) |
97 } | 107 } |
98 | 108 |
109 // jsonHandler handles the GET for the JSON requests. | |
110 func jsonHandler(w http.ResponseWriter, r *http.Request) { | |
111 log.Printf("Main Handler: %q\n", r.URL.Path) | |
mtklein
2014/06/15 15:12:12
-> JSON Handler: ?
jcgregorio
2014/06/16 02:46:25
Done.
jcgregorio
2014/06/16 02:46:25
Done.
| |
112 if r.Method == "GET" { | |
113 w.Header().Set("Content-Type", "application/json") | |
114 data.AsJSON(w) | |
115 } | |
116 } | |
117 | |
99 // mainHandler handles the GET and POST of the main page. | 118 // mainHandler handles the GET and POST of the main page. |
100 func mainHandler(w http.ResponseWriter, r *http.Request) { | 119 func mainHandler(w http.ResponseWriter, r *http.Request) { |
101 log.Printf("Main Handler: %q\n", r.URL.Path) | 120 log.Printf("Main Handler: %q\n", r.URL.Path) |
102 if r.Method == "GET" { | 121 if r.Method == "GET" { |
103 w.Header().Set("Content-Type", "text/html") | 122 w.Header().Set("Content-Type", "text/html") |
104 if err := indexTemplate.Execute(w, struct{}{}); err != nil { | 123 if err := indexTemplate.Execute(w, struct{}{}); err != nil { |
105 log.Println("ERROR: Failed to expand template:", err) | 124 log.Println("ERROR: Failed to expand template:", err) |
106 } | 125 } |
107 } | 126 } |
108 } | 127 } |
109 | 128 |
110 func main() { | 129 func main() { |
111 flag.Parse() | 130 flag.Parse() |
131 | |
132 log.Println("Begin loading data.") | |
133 var err error | |
134 data, err = NewData(*doOauth, *gitRepoDir) | |
135 if err != nil { | |
136 log.Fatalln("Failed initial load of data from BigQuery: ", err) | |
137 } | |
138 | |
112 // Resources are served directly. | 139 // Resources are served directly. |
113 http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./")))) | 140 http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./")))) |
114 | 141 |
115 http.HandleFunc("/", autogzip.HandleFunc(mainHandler)) | 142 http.HandleFunc("/", autogzip.HandleFunc(mainHandler)) |
143 http.HandleFunc("/json/", autogzip.HandleFunc(jsonHandler)) | |
144 | |
145 log.Println("Ready to serve.") | |
116 log.Fatal(http.ListenAndServe(*port, nil)) | 146 log.Fatal(http.ListenAndServe(*port, nil)) |
117 } | 147 } |
OLD | NEW |