OLD | NEW |
---|---|
1 package main | 1 package main |
2 | 2 |
3 import ( | 3 import ( |
4 "database/sql" | 4 "database/sql" |
5 "flag" | 5 "flag" |
6 "fmt" | 6 "fmt" |
7 "html/template" | 7 "html/template" |
8 "io/ioutil" | 8 "io/ioutil" |
9 "log" | 9 "log" |
10 "math/rand" | 10 "math/rand" |
(...skipping 12 matching lines...) Expand all Loading... | |
23 var ( | 23 var ( |
24 // indexTemplate is the main index.html page we serve. | 24 // indexTemplate is the main index.html page we serve. |
25 indexTemplate *template.Template = nil | 25 indexTemplate *template.Template = nil |
26 | 26 |
27 // db is the database, nil if we don't have an SQL database to store dat a into. | 27 // db is the database, nil if we don't have an SQL database to store dat a into. |
28 db *sql.DB = nil | 28 db *sql.DB = nil |
29 ) | 29 ) |
30 | 30 |
31 // flags | 31 // flags |
32 var ( | 32 var ( |
33 » port = flag.String("port", ":8000", "HTTP service address (e.g., ':8000' )") | 33 » port = flag.String("port", ":8000", "HTTP service address (e.g., ' :8000')") |
benchen
2014/06/13 19:51:29
I see tabs used in this file - is that fine?
jcgregorio
2014/06/13 19:54:01
Yes, Go is formatted with tabs.
On 2014/06/13 19:
| |
34 » doOauth = flag.Bool("oauth", true, "Run through the OAuth 2.0 flow on startup.") | |
35 » gitRepoDir = flag.String("git_repo_dir", "../../../skia", "Directory loc ation for the Skia repo.") | |
36 ) | |
37 | |
38 var ( | |
39 » data *Data | |
34 ) | 40 ) |
35 | 41 |
36 func init() { | 42 func init() { |
37 rand.Seed(time.Now().UnixNano()) | 43 rand.Seed(time.Now().UnixNano()) |
38 | 44 |
39 // Change the current working directory to the directory of the executab le. | 45 // Change the current working directory to the directory of the executab le. |
40 var err error | 46 var err error |
41 cwd, err := filepath.Abs(filepath.Dir(os.Args[0])) | 47 cwd, err := filepath.Abs(filepath.Dir(os.Args[0])) |
42 if err != nil { | 48 if err != nil { |
43 log.Fatalln(err) | 49 log.Fatalln(err) |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 func reportError(w http.ResponseWriter, r *http.Request, err error, message stri ng) { | 99 func reportError(w http.ResponseWriter, r *http.Request, err error, message stri ng) { |
94 log.Println("Error:", message, err) | 100 log.Println("Error:", message, err) |
95 w.Header().Set("Content-Type", "text/plain") | 101 w.Header().Set("Content-Type", "text/plain") |
96 http.Error(w, message, 500) | 102 http.Error(w, message, 500) |
97 } | 103 } |
98 | 104 |
99 // mainHandler handles the GET and POST of the main page. | 105 // mainHandler handles the GET and POST of the main page. |
100 func mainHandler(w http.ResponseWriter, r *http.Request) { | 106 func mainHandler(w http.ResponseWriter, r *http.Request) { |
101 log.Printf("Main Handler: %q\n", r.URL.Path) | 107 log.Printf("Main Handler: %q\n", r.URL.Path) |
102 if r.Method == "GET" { | 108 if r.Method == "GET" { |
103 » » w.Header().Set("Content-Type", "text/html") | 109 » » w.Header().Set("Content-Type", "text/plain") |
104 » » if err := indexTemplate.Execute(w, struct{}{}); err != nil { | 110 » » data.AsJSON(w) |
105 » » » log.Println("ERROR: Failed to expand template:", err) | 111 » » /* |
106 » » } | 112 » » » if err := indexTemplate.Execute(w, struct{}{}); err != n il { |
113 » » » » log.Println("ERROR: Failed to expand template:", err) | |
114 » » » } | |
115 » » */ | |
107 } | 116 } |
108 } | 117 } |
109 | 118 |
110 func main() { | 119 func main() { |
111 flag.Parse() | 120 flag.Parse() |
121 | |
122 log.Println("Begin loading data.") | |
123 var err error | |
124 data, err = NewData(*doOauth, *gitRepoDir) | |
125 if err != nil { | |
126 log.Fatalln("Failed initial load of data from BigQuery: ", err) | |
127 } | |
128 | |
112 // Resources are served directly. | 129 // Resources are served directly. |
113 http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./")))) | 130 http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./")))) |
114 | 131 |
115 http.HandleFunc("/", autogzip.HandleFunc(mainHandler)) | 132 http.HandleFunc("/", autogzip.HandleFunc(mainHandler)) |
133 | |
134 log.Println("Ready to serve.") | |
116 log.Fatal(http.ListenAndServe(*port, nil)) | 135 log.Fatal(http.ListenAndServe(*port, nil)) |
117 } | 136 } |
OLD | NEW |