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