"log"
"net/http"
"net/url"
- "path"
+ "os"
"path/filepath"
+ "runtime"
"strconv"
"strings"
"time"
import "github.com/gorilla/securecookie"
-const certFile = "certs/cert.pem"
-const keyFile = "certs/key.pem"
-const issueComment = "Edited by BugChomper"
-const oauthCallbackPath = "/oauth2callback"
-const oauthConfigFile = "oauth_client_secret.json"
-const defaultPort = 8000
-const localHost = "127.0.0.1"
-const maxSessionLen = time.Duration(3600 * time.Second)
-const priorityPrefix = "Priority-"
-const project = "skia"
-const cookieName = "BugChomperCookie"
+const (
+ certFile = "certs/cert.pem"
+ keyFile = "certs/key.pem"
+ issueComment = "Edited by BugChomper"
+ oauthCallbackPath = "/oauth2callback"
+ oauthConfigFile = "oauth_client_secret.json"
+ localHost = "127.0.0.1"
+ maxSessionLen = time.Duration(3600 * time.Second)
+ priorityPrefix = "Priority-"
+ project = "skia"
+ cookieName = "BugChomperCookie"
+)
+
+// Flags:
+var (
+ port = flag.String("port", ":8000", "HTTP service address (e.g., ':8000')")
+ public = flag.Bool("public", false, "Make this server publicly accessible.")
+)
-var scheme = "http"
+var (
+ // templates is the list of html templates used by bug_chomper.
+ templates *template.Template = nil
-var curdir, _ = filepath.Abs(".")
-var templatePath, _ = filepath.Abs("templates")
-var templates = template.Must(template.ParseFiles(
- path.Join(templatePath, "bug_chomper.html"),
- path.Join(templatePath, "submitted.html"),
- path.Join(templatePath, "error.html")))
+ scheme = "http"
+ hashKey = securecookie.GenerateRandomKey(32)
+ blockKey = securecookie.GenerateRandomKey(32)
+ secureCookie = securecookie.New(hashKey, blockKey)
+)
-var hashKey = securecookie.GenerateRandomKey(32)
-var blockKey = securecookie.GenerateRandomKey(32)
-var 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 {
// Run the BugChomper server.
func main() {
- var public bool
- flag.BoolVar(
- &public, "public", false, "Make this server publicly accessible.")
flag.Parse()
http.HandleFunc("/", handleRoot)
http.HandleFunc(oauthCallbackPath, handleOAuth2Callback)
- http.Handle("/res/", http.FileServer(http.Dir(curdir)))
- port := ":" + strconv.Itoa(defaultPort)
- log.Println("Server is running at " + scheme + "://" + localHost + port)
+ http.Handle("/res/", http.FileServer(http.Dir("./")))
+ log.Println("Server is running at " + scheme + "://" + localHost + *port)
var err error
- if public {
+ if *public {
log.Println("WARNING: This server is not secure and should not be made " +
"publicly accessible.")
scheme = "https"
- err = http.ListenAndServeTLS(port, certFile, keyFile, nil)
+ err = http.ListenAndServeTLS(*port, certFile, keyFile, nil)
} else {
scheme = "http"
- err = http.ListenAndServe(localHost+port, nil)
+ err = http.ListenAndServe(localHost+*port, nil)
}
if err != nil {
log.Println(err.Error())