You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.3 KiB

  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // heapview is a tool for viewing Go heap dumps.
  5. package main
  6. import (
  7. "flag"
  8. "fmt"
  9. "go/build"
  10. "io"
  11. "log"
  12. "net/http"
  13. "os"
  14. "path/filepath"
  15. )
  16. var host = flag.String("host", "", "host addr to listen on")
  17. var port = flag.Int("port", 8080, "service port")
  18. var index = `<!DOCTYPE html>
  19. <script src="js/customelements.js"></script>
  20. <script src="js/typescript.js"></script>
  21. <script src="js/moduleloader.js"></script>
  22. <script>
  23. System.transpiler = 'typescript';
  24. System.typescriptOptions = {target: ts.ScriptTarget.ES2015};
  25. System.locate = (load) => load.name + '.ts';
  26. </script>
  27. <script type="module">
  28. import {main} from './client/main';
  29. main();
  30. </script>
  31. `
  32. func toolsDir() string {
  33. p, err := build.Import("golang.org/x/tools", "", build.FindOnly)
  34. if err != nil {
  35. log.Println("error: can't find client files:", err)
  36. os.Exit(1)
  37. }
  38. return p.Dir
  39. }
  40. var parseFlags = func() {
  41. flag.Parse()
  42. }
  43. var addHandlers = func() {
  44. toolsDir := toolsDir()
  45. // Directly serve typescript code in client directory for development.
  46. http.Handle("/client/", http.StripPrefix("/client",
  47. http.FileServer(http.Dir(filepath.Join(toolsDir, "cmd/heapview/client")))))
  48. // Serve typescript.js and moduleloader.js for development.
  49. http.HandleFunc("/js/typescript.js", func(w http.ResponseWriter, r *http.Request) {
  50. http.ServeFile(w, r, filepath.Join(toolsDir, "third_party/typescript/typescript.js"))
  51. })
  52. http.HandleFunc("/js/moduleloader.js", func(w http.ResponseWriter, r *http.Request) {
  53. http.ServeFile(w, r, filepath.Join(toolsDir, "third_party/moduleloader/moduleloader.js"))
  54. })
  55. http.HandleFunc("/js/customelements.js", func(w http.ResponseWriter, r *http.Request) {
  56. http.ServeFile(w, r, filepath.Join(toolsDir, "third_party/webcomponents/customelements.js"))
  57. })
  58. // Serve index.html using html string above.
  59. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  60. w.Header().Set("Content-Type", "text/html")
  61. io.WriteString(w, index)
  62. })
  63. }
  64. var listenAndServe = func() error {
  65. return http.ListenAndServe(fmt.Sprintf("%s:%d", *host, *port), nil)
  66. }
  67. func main() {
  68. parseFlags()
  69. addHandlers()
  70. log.Fatal(listenAndServe())
  71. }