Chromium Code Reviews| Index: fuzzer/go/fuzzer/main.go |
| diff --git a/fuzzer/go/fuzzer/main.go b/fuzzer/go/fuzzer/main.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4813ad6128d7072b6002e3d24351f685665cc7f2 |
| --- /dev/null |
| +++ b/fuzzer/go/fuzzer/main.go |
| @@ -0,0 +1,84 @@ |
| +package main |
| + |
| +import ( |
| + "flag" |
| + htemplate "html/template" |
| + "math/rand" |
| + "net/http" |
| + "os" |
| + "path/filepath" |
| + "runtime" |
| + "time" |
| + |
| + "github.com/BurntSushi/toml" |
| + "github.com/fiorix/go-web/autogzip" |
| + metrics "github.com/rcrowley/go-metrics" |
| + "github.com/skia-dev/glog" |
| + "skia.googlesource.com/buildbot.git/fuzzer/go/config" |
| + "skia.googlesource.com/buildbot.git/go/common" |
| +) |
| + |
| +var ( |
| + // indexTemplate is the main index.html page we serve. |
| + indexTemplate *htemplate.Template = nil |
| + |
| + requestsCounter = metrics.NewRegisteredCounter("requests", metrics.DefaultRegistry) |
| +) |
| + |
| +// Command line flags. |
| +var ( |
| + configFilename = flag.String("config", "fuzzer.toml", "Configuration filename") |
| +) |
| + |
| +func Init() { |
| + rand.Seed(time.Now().UnixNano()) |
| + |
| + common.Init() |
| + |
| + if _, err := toml.DecodeFile(*configFilename, &config.Fuzzer); err != nil { |
| + glog.Fatalf("Failed to decode config file: %s", err) |
| + } |
| + |
| + if config.Fuzzer.FrontEnd.ResourcePath == "" { |
| + _, filename, _, _ := runtime.Caller(0) |
| + config.Fuzzer.FrontEnd.ResourcePath = filepath.Join(filepath.Dir(filename), "../..") |
| + } |
| + |
| + path, err := filepath.Abs(config.Fuzzer.FrontEnd.ResourcePath) |
| + if err != nil { |
| + glog.Fatalf("Couldn't get absolute path to fuzzer resources: %s", err) |
| + } |
| + if err := os.Chdir(path); err != nil { |
| + glog.Fatal(err) |
| + } |
| + |
| + indexTemplate = htemplate.Must(htemplate.ParseFiles( |
| + filepath.Join(path, "templates/index.html"), |
| + filepath.Join(path, "templates/header.html"), |
| + filepath.Join(path, "templates/footer.html"), |
| + )) |
| + |
| + common.InitWithMetrics("fuzzer", &config.Fuzzer.FrontEnd.GraphiteServer) |
|
jcgregorio
2015/02/11 13:32:23
But you already called common.Init() above.
See t
humper
2015/02/12 17:45:23
Right, sorry -- thought I split them apart last we
|
| +} |
| + |
| +// mainHandler handles the GET and POST of the main page. |
| +func mainHandler(w http.ResponseWriter, r *http.Request) { |
| + glog.Infof("Main Handler: %q\n", r.URL.Path) |
| + requestsCounter.Inc(1) |
| + if r.Method == "GET" { |
| + // Expand the template. |
| + w.Header().Set("Content-Type", "text/html") |
| + if err := indexTemplate.Execute(w, struct{}{}); err != nil { |
| + glog.Errorf("Failed to expand template: %q\n", err) |
| + } |
| + } |
| +} |
| + |
| +func main() { |
| + flag.Parse() |
| + Init() |
| + // Resources are served directly |
| + http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./")))) |
| + http.HandleFunc("/", autogzip.HandleFunc(mainHandler)) |
| + glog.Fatal(http.ListenAndServe(config.Fuzzer.FrontEnd.Port, nil)) |
| +} |