Skip to content

Commit

Permalink
x/tools/cmd/heapview: break out init code
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
matloob committed Jul 27, 2016
1 parent 053ddb9 commit 9e74590
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions cmd/heapview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<!DOCTYPE html>
<script src="js/typescript.js"></script>
<script src="js/moduleloader.js"></script>
Expand All @@ -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()
}

0 comments on commit 9e74590

Please sign in to comment.