From 9e7459099f9afd6a15464d69d93c6eed49bb545d Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Wed, 27 Jul 2016 12:38:56 -0400 Subject: [PATCH] x/tools/cmd/heapview: break out init code This change breaks out the code that adds handler funcs and starts the HTTP server into separate functions, so that they can be overridden in other environments, such as Google's. For instance, listenAndServe can be overridden in an init method in a different file to use a HTTP2 server. Updates golang/go#16410 Change-Id: I074242af10486c60c374e9ac7ebe9d0e61a8fa22 Reviewed-on: https://go-review.googlesource.com/25273 Reviewed-by: Hyang-Ah Hana Kim --- cmd/heapview/main.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/cmd/heapview/main.go b/cmd/heapview/main.go index 7cbb4b58..fee5999d 100644 --- a/cmd/heapview/main.go +++ b/cmd/heapview/main.go @@ -6,14 +6,17 @@ package main import ( + "flag" + "fmt" "io" "log" "net/http" "os" - "path" "path/filepath" ) +var port = flag.Int("port", 8080, "service port") + var index = ` @@ -36,23 +39,36 @@ func toolsDir() string { return filepath.Join(filepath.SplitList(gopath)[0], "/src/golang.org/x/tools") } -func main() { +var parseFlags = func() { + flag.Parse() +} + +var addHandlers = func() { // Directly serve typescript code in client directory for development. http.Handle("/client/", http.StripPrefix("/client", http.FileServer(http.Dir(filepath.Join(toolsDir(), "cmd/heapview/client"))))) + // Serve typescript.js and moduleloader.js for development. - log.Print(http.Dir(path.Join(toolsDir() + "/cmd/heapview/client"))) http.HandleFunc("/js/typescript.js", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, filepath.Join(toolsDir(), "third_party/typescript/typescript.js")) }) http.HandleFunc("/js/moduleloader.js", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, filepath.Join(toolsDir(), "third_party/moduleloader/moduleloader.js")) }) + // Serve index.html using html string above. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") io.WriteString(w, index) }) - - http.ListenAndServe(":8080", nil) +} + +var listenAndServe = func() { + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)) +} + +func main() { + parseFlags() + addHandlers() + listenAndServe() }