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 | package main |
6 | |
7 | import ( |
8 | "flag" |
9 | "fmt" |
10 | "go/build" |
11 | "log" |
12 | "net" |
13 | "net/http" |
14 | "net/url" |
15 | "os" |
16 | "strings" |
17 | |
18 | "golang.org/x/tools/present" |
19 | ) |
20 | |
21 | const basePkg = "golang.org/x/tools/cmd/present" |
22 | |
23 | var ( |
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") |
30 | ) |
31 | |
32 | func main() { |
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)") |
35 | flag.Parse() |
36 | |
37 | if os.Getenv("GAE_ENV") == "standard" { |
38 | log.Print("Configuring for App Engine Standard") |
39 | port := os.Getenv("PORT") |
40 | if port == "" { |
41 | port = "8080" |
42 | } |
43 | *httpAddr = fmt.Sprintf("0.0.0.0:%s", port) |
44 | pwd, err := os.Getwd() |
45 | if err != nil { |
46 | log.Fatalf("Couldn't get pwd: %v\n", err) |
47 | } |
48 | *basePath = pwd |
49 | *usePlayground = true |
50 | *contentPath = "./content/" |
51 | } |
52 | |
53 | if *basePath == "" { |
54 | p, err := build.Default.Import(basePkg, "", build.FindOnly) |
55 | if err != nil { |
56 | fmt.Fprintf(os.Stderr, "Couldn't find gopresent files: %v\n", err) |
57 | fmt.Fprintf(os.Stderr, basePathMessage, basePkg) |
58 | os.Exit(1) |
59 | } |
60 | *basePath = p.Dir |
61 | } |
62 | err := initTemplates(*basePath) |
63 | if err != nil { |
64 | log.Fatalf("Failed to parse templates: %v", err) |
65 | } |
66 | |
67 | ln, err := net.Listen("tcp", *httpAddr) |
68 | if err != nil { |
69 | log.Fatal(err) |
70 | } |
71 | defer ln.Close() |
72 | |
73 | _, port, err := net.SplitHostPort(ln.Addr().String()) |
74 | if err != nil { |
75 | log.Fatal(err) |
76 | } |
77 | |
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" |
83 | } |
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) |
89 | } else { |
90 | reqHost, reqPort, err := net.SplitHostPort(*httpAddr) |
91 | if err != nil { |
92 | log.Fatal(err) |
93 | } |
94 | if reqPort == "0" { |
95 | origin.Host = net.JoinHostPort(reqHost, port) |
96 | } else { |
97 | origin.Host = *httpAddr |
98 | } |
99 | } |
100 | |
101 | initPlayground(*basePath, origin) |
102 | http.Handle("/static/", http.FileServer(http.Dir(*basePath))) |
103 | |
104 | if !ln.Addr().(*net.TCPAddr).IP.IsLoopback() && |
105 | present.PlayEnabled && !*nativeClient && !*usePlayground { |
106 | log.Print(localhostWarning) |
107 | } |
108 | |
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.") |
112 | } |
113 | log.Fatal(http.Serve(ln, nil)) |
114 | } |
115 | |
116 | func environ(vars ...string) []string { |
117 | env := os.Environ() |
118 | for _, r := range vars { |
119 | k := strings.SplitAfter(r, "=")[0] |
120 | var found bool |
121 | for i, v := range env { |
122 | if strings.HasPrefix(v, k) { |
123 | env[i] = r |
124 | found = true |
125 | } |
126 | } |
127 | if !found { |
128 | env = append(env, r) |
129 | } |
130 | } |
131 | return env |
132 | } |
133 | |
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). |
138 | |
139 | You may use the -base flag to specify an alternate location. |
140 | ` |
141 | |
142 | const localhostWarning = ` |
143 | WARNING! WARNING! WARNING! |
144 | |
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. |
148 | |
149 | To avoid this message, listen on localhost, run with -play=false, or run with |
150 | -play_socket=false. |
151 | |
152 | If you don't understand this message, hit Control-C to terminate this process. |
153 | |
154 | WARNING! WARNING! WARNING! |
155 | ` |
156 |
Members