You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.4 KiB

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