GoPLS Viewer

Home|gopls/cmd/present/main.go
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
5package main
6
7import (
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
21const basePkg = "golang.org/x/tools/cmd/present"
22
23var (
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
32func 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        pwderr := 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        perr := 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.StderrbasePathMessagebasePkg)
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    lnerr := net.Listen("tcp", *httpAddr)
68    if err != nil {
69        log.Fatal(err)
70    }
71    defer ln.Close()
72
73    _porterr := 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(*originHostport)
86    } else if ln.Addr().(*net.TCPAddr).IP.IsUnspecified() {
87        name_ := os.Hostname()
88        origin.Host = net.JoinHostPort(nameport)
89    } else {
90        reqHostreqPorterr := net.SplitHostPort(*httpAddr)
91        if err != nil {
92            log.Fatal(err)
93        }
94        if reqPort == "0" {
95            origin.Host = net.JoinHostPort(reqHostport)
96        } else {
97            origin.Host = *httpAddr
98        }
99    }
100
101    initPlayground(*basePathorigin)
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(lnnil))
114}
115
116func 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 iv := range env {
122            if strings.HasPrefix(vk) {
123                env[i] = r
124                found = true
125            }
126        }
127        if !found {
128            env = append(envr)
129        }
130    }
131    return env
132}
133
134const basePathMessage = `
135By default, gopresent locates the slide template files and associated
136static content by looking for a %q package
137in your Go workspaces (GOPATH).
138
139You may use the -base flag to specify an alternate location.
140`
141
142const localhostWarning = `
143WARNING!  WARNING!  WARNING!
144
145The present server appears to be listening on an address that is not localhost
146and is configured to run code snippets locally. Anyone with access to this address
147and port will have access to this machine as the user running present.
148
149To avoid this message, listen on localhost, run with -play=false, or run with
150-play_socket=false.
151
152If you don't understand this message, hit Control-C to terminate this process.
153
154WARNING!  WARNING!  WARNING!
155`
156
MembersX
environ.RangeStmt_3443.BlockStmt.RangeStmt_3524.v
main.port
main.BlockStmt._
localhostWarning
main.BlockStmt.name
basePkg
main.BlockStmt.reqPort
build
fmt
main.BlockStmt.port
main.BlockStmt.pwd
main.BlockStmt.err
main.BlockStmt.p
main.origin
flag
main.err
main.ln
main.BlockStmt.reqHost
environ.vars
environ.env
environ.RangeStmt_3443.r
environ.RangeStmt_3443.BlockStmt.RangeStmt_3524.i
url
basePathMessage
main._
main
environ.RangeStmt_3443.BlockStmt.found
environ
Members
X