github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/cmd/present/local.go (about) 1 // Copyright 2013 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 // +build !appengine 6 7 package main 8 9 import ( 10 "flag" 11 "fmt" 12 "go/build" 13 "log" 14 "net" 15 "net/http" 16 "net/url" 17 "os" 18 "strings" 19 20 "golang.org/x/tools/present" 21 ) 22 23 const basePkg = "golang.org/x/tools/cmd/present" 24 25 var ( 26 httpAddr = flag.String("http", "127.0.0.1:3999", "HTTP service address (e.g., '127.0.0.1:3999')") 27 originHost = flag.String("orighost", "", "host component of web origin URL (e.g., 'localhost')") 28 basePath = flag.String("base", "", "base path for slide template and static resources") 29 nativeClient = flag.Bool("nacl", false, "use Native Client environment playground (prevents non-Go code execution)") 30 ) 31 32 func main() { 33 flag.BoolVar(&present.PlayEnabled, "play", true, "enable playground (permit execution of arbitrary user code)") 34 flag.Parse() 35 36 if *basePath == "" { 37 p, err := build.Default.Import(basePkg, "", build.FindOnly) 38 if err != nil { 39 fmt.Fprintf(os.Stderr, "Couldn't find gopresent files: %v\n", err) 40 fmt.Fprintf(os.Stderr, basePathMessage, basePkg) 41 os.Exit(1) 42 } 43 *basePath = p.Dir 44 } 45 err := initTemplates(*basePath) 46 if err != nil { 47 log.Fatalf("Failed to parse templates: %v", err) 48 } 49 50 ln, err := net.Listen("tcp", *httpAddr) 51 if err != nil { 52 log.Fatal(err) 53 } 54 defer ln.Close() 55 56 _, port, err := net.SplitHostPort(ln.Addr().String()) 57 if err != nil { 58 log.Fatal(err) 59 } 60 61 origin := &url.URL{Scheme: "http"} 62 if *originHost != "" { 63 origin.Host = net.JoinHostPort(*originHost, port) 64 } else if ln.Addr().(*net.TCPAddr).IP.IsUnspecified() { 65 name, _ := os.Hostname() 66 origin.Host = net.JoinHostPort(name, port) 67 } else { 68 reqHost, reqPort, err := net.SplitHostPort(*httpAddr) 69 if err != nil { 70 log.Fatal(err) 71 } 72 if reqPort == "0" { 73 origin.Host = net.JoinHostPort(reqHost, port) 74 } else { 75 origin.Host = *httpAddr 76 } 77 } 78 79 initPlayground(*basePath, origin) 80 http.Handle("/static/", http.FileServer(http.Dir(*basePath))) 81 82 if !ln.Addr().(*net.TCPAddr).IP.IsLoopback() && 83 present.PlayEnabled && !*nativeClient { 84 log.Print(localhostWarning) 85 } 86 87 log.Printf("Open your web browser and visit %s", origin.String()) 88 log.Fatal(http.Serve(ln, nil)) 89 } 90 91 func environ(vars ...string) []string { 92 env := os.Environ() 93 for _, r := range vars { 94 k := strings.SplitAfter(r, "=")[0] 95 var found bool 96 for i, v := range env { 97 if strings.HasPrefix(v, k) { 98 env[i] = r 99 found = true 100 } 101 } 102 if !found { 103 env = append(env, r) 104 } 105 } 106 return env 107 } 108 109 const basePathMessage = ` 110 By default, gopresent locates the slide template files and associated 111 static content by looking for a %q package 112 in your Go workspaces (GOPATH). 113 114 You may use the -base flag to specify an alternate location. 115 ` 116 117 const localhostWarning = ` 118 WARNING! WARNING! WARNING! 119 120 The present server appears to be listening on an address that is not localhost. 121 Anyone with access to this address and port will have access to this machine as 122 the user running present. 123 124 To avoid this message, listen on localhost or run with -play=false. 125 126 If you don't understand this message, hit Control-C to terminate this process. 127 128 WARNING! WARNING! WARNING! 129 `