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 <hyangah@gmail.com>
This commit is contained in:
Michael Matloob 2016-07-27 12:38:56 -04:00
parent 053ddb97bf
commit 9e7459099f
1 changed files with 21 additions and 5 deletions

View File

@ -6,14 +6,17 @@
package main package main
import ( import (
"flag"
"fmt"
"io" "io"
"log" "log"
"net/http" "net/http"
"os" "os"
"path"
"path/filepath" "path/filepath"
) )
var port = flag.Int("port", 8080, "service port")
var index = `<!DOCTYPE html> var index = `<!DOCTYPE html>
<script src="js/typescript.js"></script> <script src="js/typescript.js"></script>
<script src="js/moduleloader.js"></script> <script src="js/moduleloader.js"></script>
@ -36,23 +39,36 @@ func toolsDir() string {
return filepath.Join(filepath.SplitList(gopath)[0], "/src/golang.org/x/tools") 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. // Directly serve typescript code in client directory for development.
http.Handle("/client/", http.StripPrefix("/client", http.Handle("/client/", http.StripPrefix("/client",
http.FileServer(http.Dir(filepath.Join(toolsDir(), "cmd/heapview/client"))))) http.FileServer(http.Dir(filepath.Join(toolsDir(), "cmd/heapview/client")))))
// Serve typescript.js and moduleloader.js for development. // 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.HandleFunc("/js/typescript.js", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(toolsDir(), "third_party/typescript/typescript.js")) 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.HandleFunc("/js/moduleloader.js", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(toolsDir(), "third_party/moduleloader/moduleloader.js")) http.ServeFile(w, r, filepath.Join(toolsDir(), "third_party/moduleloader/moduleloader.js"))
}) })
// Serve index.html using html string above. // Serve index.html using html string above.
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html") w.Header().Set("Content-Type", "text/html")
io.WriteString(w, index) 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()
} }