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:
parent
053ddb97bf
commit
9e7459099f
|
@ -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>
|
||||
|
@ -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()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue