diff options
author | 2014-10-17 14:25:07 -0700 | |
---|---|---|
committer | 2014-10-17 14:25:07 -0700 | |
commit | 3f752205a5d55b47c2d07486736c5e344b45a017 (patch) | |
tree | bdf253c97ce934ccf3836e71134a06d953523888 /tools | |
parent | 25956ce43032afdfcc996af8c7661c076f9a1d74 (diff) |
bug_chomper: Cleanup template initialization.
Use the same tricks used by webtry and perf. Code seems more robust and
easier to check for errors this way.
BUG=None
TEST=./run_server.sh, then navigate to 127.0.0.1:8000 and
127.0.0.1:8000/res
R=borenet@google.com
Review URL: https://codereview.chromium.org/661613004
Diffstat (limited to 'tools')
-rw-r--r-- | tools/bug_chomper/src/server/server.go | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/tools/bug_chomper/src/server/server.go b/tools/bug_chomper/src/server/server.go index 1388d00b93..7fdae93c7b 100644 --- a/tools/bug_chomper/src/server/server.go +++ b/tools/bug_chomper/src/server/server.go @@ -20,8 +20,9 @@ import ( "log" "net/http" "net/url" - "path" + "os" "path/filepath" + "runtime" "strconv" "strings" "time" @@ -49,20 +50,31 @@ var ( ) var ( - scheme = "http" - - curdir, _ = filepath.Abs(".") - templatePath, _ = filepath.Abs("templates") - templates = template.Must(template.ParseFiles( - path.Join(templatePath, "bug_chomper.html"), - path.Join(templatePath, "submitted.html"), - path.Join(templatePath, "error.html"))) + // templates is the list of html templates used by bug_chomper. + templates *template.Template = nil + scheme = "http" hashKey = securecookie.GenerateRandomKey(32) blockKey = securecookie.GenerateRandomKey(32) secureCookie = securecookie.New(hashKey, blockKey) ) +func init() { + // Change the current working directory to two directories up from this + // source file so that we can read templates. + _, filename, _, _ := runtime.Caller(0) + cwd := filepath.Join(filepath.Dir(filename), "../..") + if err := os.Chdir(cwd); err != nil { + log.Fatal(err) + } + + templates = template.Must(template.ParseFiles( + filepath.Join(cwd, "templates/bug_chomper.html"), + filepath.Join(cwd, "templates/submitted.html"), + filepath.Join(cwd, "templates/error.html"), + )) +} + // SessionState contains data for a given session. type SessionState struct { IssueTracker *issue_tracker.IssueTracker @@ -363,7 +375,7 @@ func main() { http.HandleFunc("/", handleRoot) http.HandleFunc(oauthCallbackPath, handleOAuth2Callback) - http.Handle("/res/", http.FileServer(http.Dir(curdir))) + http.Handle("/res/", http.FileServer(http.Dir("./"))) log.Println("Server is running at " + scheme + "://" + localHost + *port) var err error if *public { |