| 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 |
"io/fs" |
| 11 |
"log" |
| 12 |
"net" |
| 13 |
"net/http" |
| 14 |
"net/url" |
| 15 |
"os" |
| 16 |
"strings" |
| 17 |
|
| 18 |
"golang.org/x/tools/present" |
| 19 |
) |
| 20 |
|
| 21 |
var ( |
| 22 |
httpAddr = flag.String("http", "127.0.0.1:3999", "HTTP service address (e.g., '127.0.0.1:3999')") |
| 23 |
originHost = flag.String("orighost", "", "host component of web origin URL (e.g., 'localhost')") |
| 24 |
basePath = flag.String("base", "", "base path for slide template and static resources") |
| 25 |
contentPath = flag.String("content", ".", "base path for presentation content") |
| 26 |
usePlayground = flag.Bool("use_playground", false, "run code snippets using play.golang.org; if false, run them locally and deliver results by WebSocket transport") |
| 27 |
) |
| 28 |
|
| 29 |
func main() { |
| 30 |
flag.BoolVar(&present.PlayEnabled, "play", true, "enable playground (permit execution of arbitrary user code)") |
| 31 |
flag.BoolVar(&present.NotesEnabled, "notes", false, "enable presenter notes (press 'N' from the browser to display them)") |
| 32 |
flag.Parse() |
| 33 |
|
| 34 |
if os.Getenv("GAE_ENV") == "standard" { |
| 35 |
log.Print("Configuring for App Engine Standard") |
| 36 |
port := os.Getenv("PORT") |
| 37 |
if port == "" { |
| 38 |
port = "8080" |
| 39 |
} |
| 40 |
*httpAddr = fmt.Sprintf("0.0.0.0:%s", port) |
| 41 |
pwd, err := os.Getwd() |
| 42 |
if err != nil { |
| 43 |
log.Fatalf("Couldn't get pwd: %v\n", err) |
| 44 |
} |
| 45 |
*basePath = pwd |
| 46 |
*usePlayground = true |
| 47 |
*contentPath = "./content/" |
| 48 |
} |
| 49 |
|
| •50 |
var fsys fs.FS = os.DirFS("/usr/share/golang-golang-x-tools/present") |
| 51 |
if *basePath != "" { |
| 52 |
fsys = os.DirFS(*basePath) |
| 53 |
} |
| 54 |
err := initTemplates(fsys) |
| 55 |
if err != nil { |
| 56 |
log.Fatalf("Failed to parse templates: %v", err) |
| 57 |
} |
| 58 |
|
| 59 |
ln, err := net.Listen("tcp", *httpAddr) |
| 60 |
if err != nil { |
| 61 |
log.Fatal(err) |
| 62 |
} |
| 63 |
defer ln.Close() |
| 64 |
|
| 65 |
_, port, err := net.SplitHostPort(ln.Addr().String()) |
| 66 |
if err != nil { |
| 67 |
log.Fatal(err) |
| 68 |
} |
| 69 |
|
| 70 |
origin := &url.URL{Scheme: "http"} |
| 71 |
if *originHost != "" { |
| 72 |
if after, ok := strings.CutPrefix(*originHost, "https://"); ok { |
| 73 |
*originHost = after |
| 74 |
origin.Scheme = "https" |
| 75 |
} |
| 76 |
*originHost = strings.TrimPrefix(*originHost, "http://") |
| 77 |
origin.Host = net.JoinHostPort(*originHost, port) |
| 78 |
} else if ln.Addr().(*net.TCPAddr).IP.IsUnspecified() { |
| 79 |
name, _ := os.Hostname() |
| 80 |
origin.Host = net.JoinHostPort(name, port) |
| 81 |
} else { |
| 82 |
reqHost, reqPort, err := net.SplitHostPort(*httpAddr) |
| 83 |
if err != nil { |
| 84 |
log.Fatal(err) |
| 85 |
} |
| 86 |
if reqPort == "0" { |
| 87 |
origin.Host = net.JoinHostPort(reqHost, port) |
| 88 |
} else { |
| 89 |
origin.Host = *httpAddr |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
initPlayground(fsys, origin) |
| 94 |
http.Handle("/static/", http.FileServer(http.FS(fsys))) |
| 95 |
|
| 96 |
if !ln.Addr().(*net.TCPAddr).IP.IsLoopback() && |
| 97 |
present.PlayEnabled && !*usePlayground { |
| 98 |
log.Print(localhostWarning) |
| 99 |
} |
| 100 |
|
| 101 |
log.Printf("Open your web browser and visit %s", origin.String()) |
| 102 |
if present.NotesEnabled { |
| 103 |
log.Println("Notes are enabled, press 'N' from the browser to display them.") |
| 104 |
} |
| 105 |
log.Fatal(http.Serve(ln, nil)) |
| 106 |
} |
| 107 |
|
| 108 |
const localhostWarning = ` |
| 109 |
WARNING! WARNING! WARNING! |
| 110 |
|
| 111 |
The present server appears to be listening on an address that is not localhost |
| 112 |
and is configured to run code snippets locally. Anyone with access to this address |
| 113 |
and port will have access to this machine as the user running present. |
| 114 |
|
| 115 |
To avoid this message, listen on localhost, run with -play=false, or run with |
| 116 |
-play_socket=false. |
| 117 |
|
| 118 |
If you don't understand this message, hit Control-C to terminate this process. |
| 119 |
|
| 120 |
WARNING! WARNING! WARNING! |
| 121 |
` |