2016-07-20 10:26:50 -06:00
|
|
|
// Copyright 2016 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-07-20 12:57:55 -06:00
|
|
|
// heapview is a tool for viewing Go heap dumps.
|
2016-07-20 10:26:50 -06:00
|
|
|
package main
|
|
|
|
|
2016-07-26 08:27:44 -06:00
|
|
|
import (
|
2016-07-27 10:38:56 -06:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2017-03-28 15:15:02 -06:00
|
|
|
"go/build"
|
2016-07-26 08:27:44 -06:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2016-08-10 09:43:06 -06:00
|
|
|
var host = flag.String("host", "", "host addr to listen on")
|
2016-07-27 10:38:56 -06:00
|
|
|
var port = flag.Int("port", 8080, "service port")
|
|
|
|
|
2016-07-26 08:27:44 -06:00
|
|
|
var index = `<!DOCTYPE html>
|
2016-08-04 12:24:04 -06:00
|
|
|
<script src="js/customelements.js"></script>
|
2016-07-26 08:27:44 -06:00
|
|
|
<script src="js/typescript.js"></script>
|
|
|
|
<script src="js/moduleloader.js"></script>
|
|
|
|
<script>
|
|
|
|
System.transpiler = 'typescript';
|
2016-08-04 12:24:04 -06:00
|
|
|
System.typescriptOptions = {target: ts.ScriptTarget.ES2015};
|
2016-07-26 08:27:44 -06:00
|
|
|
System.locate = (load) => load.name + '.ts';
|
|
|
|
</script>
|
|
|
|
<script type="module">
|
|
|
|
import {main} from './client/main';
|
|
|
|
main();
|
|
|
|
</script>
|
|
|
|
`
|
|
|
|
|
|
|
|
func toolsDir() string {
|
2017-03-28 15:15:02 -06:00
|
|
|
p, err := build.Import("golang.org/x/tools", "", build.FindOnly)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error: can't find client files:", err)
|
2016-07-26 08:27:44 -06:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-03-28 15:15:02 -06:00
|
|
|
return p.Dir
|
2016-07-26 08:27:44 -06:00
|
|
|
}
|
|
|
|
|
2016-07-27 10:38:56 -06:00
|
|
|
var parseFlags = func() {
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
var addHandlers = func() {
|
2017-03-28 15:15:02 -06:00
|
|
|
toolsDir := toolsDir()
|
|
|
|
|
2016-07-26 08:27:44 -06:00
|
|
|
// Directly serve typescript code in client directory for development.
|
|
|
|
http.Handle("/client/", http.StripPrefix("/client",
|
2017-03-28 15:15:02 -06:00
|
|
|
http.FileServer(http.Dir(filepath.Join(toolsDir, "cmd/heapview/client")))))
|
2016-07-27 10:38:56 -06:00
|
|
|
|
2016-07-26 08:27:44 -06:00
|
|
|
// Serve typescript.js and moduleloader.js for development.
|
|
|
|
http.HandleFunc("/js/typescript.js", func(w http.ResponseWriter, r *http.Request) {
|
2017-03-28 15:15:02 -06:00
|
|
|
http.ServeFile(w, r, filepath.Join(toolsDir, "third_party/typescript/typescript.js"))
|
2016-07-26 08:27:44 -06:00
|
|
|
})
|
|
|
|
http.HandleFunc("/js/moduleloader.js", func(w http.ResponseWriter, r *http.Request) {
|
2017-03-28 15:15:02 -06:00
|
|
|
http.ServeFile(w, r, filepath.Join(toolsDir, "third_party/moduleloader/moduleloader.js"))
|
2016-07-26 08:27:44 -06:00
|
|
|
})
|
2016-08-04 12:24:04 -06:00
|
|
|
http.HandleFunc("/js/customelements.js", func(w http.ResponseWriter, r *http.Request) {
|
2017-03-28 15:15:02 -06:00
|
|
|
http.ServeFile(w, r, filepath.Join(toolsDir, "third_party/webcomponents/customelements.js"))
|
2016-08-04 12:24:04 -06:00
|
|
|
})
|
2016-07-27 10:38:56 -06:00
|
|
|
|
2016-07-26 08:27:44 -06:00
|
|
|
// 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)
|
|
|
|
})
|
2016-07-27 10:38:56 -06:00
|
|
|
}
|
2016-07-26 08:27:44 -06:00
|
|
|
|
2016-08-10 09:43:06 -06:00
|
|
|
var listenAndServe = func() error {
|
|
|
|
return http.ListenAndServe(fmt.Sprintf("%s:%d", *host, *port), nil)
|
2016-07-27 10:38:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
parseFlags()
|
|
|
|
addHandlers()
|
2016-08-10 09:43:06 -06:00
|
|
|
log.Fatal(listenAndServe())
|
2016-07-20 10:26:50 -06:00
|
|
|
}
|