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.
18 "golang.org/x/tools/present"
21 const basePkg = "golang.org/x/tools/cmd/present"
24 httpAddr = flag.String("http", "127.0.0.1:3999", "HTTP service address (e.g., '127.0.0.1:3999')")
25 originHost = flag.String("orighost", "", "host component of web origin URL (e.g., 'localhost')")
26 basePath = flag.String("base", "", "base path for slide template and static resources")
27 contentPath = flag.String("content", ".", "base path for presentation content")
28 usePlayground = flag.Bool("use_playground", false, "run code snippets using play.golang.org; if false, run them locally and deliver results by WebSocket transport")
29 nativeClient = flag.Bool("nacl", false, "use Native Client environment playground (prevents non-Go code execution) when using local WebSocket transport")
33 flag.BoolVar(&present.PlayEnabled, "play", true, "enable playground (permit execution of arbitrary user code)")
34 flag.BoolVar(&present.NotesEnabled, "notes", false, "enable presenter notes (press 'N' from the browser to display them)")
37 if os.Getenv("GAE_ENV") == "standard" {
38 log.Print("Configuring for App Engine Standard")
39 port := os.Getenv("PORT")
43 *httpAddr = fmt.Sprintf("0.0.0.0:%s", port)
44 pwd, err := os.Getwd()
46 log.Fatalf("Couldn't get pwd: %v\n", err)
50 *contentPath = "./content/"
54 p, err := build.Default.Import(basePkg, "", build.FindOnly)
56 fmt.Fprintf(os.Stderr, "Couldn't find gopresent files: %v\n", err)
57 fmt.Fprintf(os.Stderr, basePathMessage, basePkg)
62 err := initTemplates(*basePath)
64 log.Fatalf("Failed to parse templates: %v", err)
67 ln, err := net.Listen("tcp", *httpAddr)
73 _, port, err := net.SplitHostPort(ln.Addr().String())
78 origin := &url.URL{Scheme: "http"}
79 if *originHost != "" {
80 if strings.HasPrefix(*originHost, "https://") {
81 *originHost = strings.TrimPrefix(*originHost, "https://")
82 origin.Scheme = "https"
84 *originHost = strings.TrimPrefix(*originHost, "http://")
85 origin.Host = net.JoinHostPort(*originHost, port)
86 } else if ln.Addr().(*net.TCPAddr).IP.IsUnspecified() {
87 name, _ := os.Hostname()
88 origin.Host = net.JoinHostPort(name, port)
90 reqHost, reqPort, err := net.SplitHostPort(*httpAddr)
95 origin.Host = net.JoinHostPort(reqHost, port)
97 origin.Host = *httpAddr
101 initPlayground(*basePath, origin)
102 http.Handle("/static/", http.FileServer(http.Dir(*basePath)))
104 if !ln.Addr().(*net.TCPAddr).IP.IsLoopback() &&
105 present.PlayEnabled && !*nativeClient && !*usePlayground {
106 log.Print(localhostWarning)
109 log.Printf("Open your web browser and visit %s", origin.String())
110 if present.NotesEnabled {
111 log.Println("Notes are enabled, press 'N' from the browser to display them.")
113 log.Fatal(http.Serve(ln, nil))
116 func environ(vars ...string) []string {
118 for _, r := range vars {
119 k := strings.SplitAfter(r, "=")[0]
121 for i, v := range env {
122 if strings.HasPrefix(v, k) {
134 const basePathMessage = `
135 By default, gopresent locates the slide template files and associated
136 static content by looking for a %q package
137 in your Go workspaces (GOPATH).
139 You may use the -base flag to specify an alternate location.
142 const localhostWarning = `
143 WARNING! WARNING! WARNING!
145 The present server appears to be listening on an address that is not localhost
146 and is configured to run code snippets locally. Anyone with access to this address
147 and port will have access to this machine as the user running present.
149 To avoid this message, listen on localhost, run with -play=false, or run with
152 If you don't understand this message, hit Control-C to terminate this process.
154 WARNING! WARNING! WARNING!