GoPLS Viewer

Home|gopls/cmd/present/play.go
1// Copyright 2012 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    "bytes"
9    "fmt"
10    "io/ioutil"
11    "net/http"
12    "net/url"
13    "path/filepath"
14    "runtime"
15    "time"
16
17    "golang.org/x/tools/godoc/static"
18    "golang.org/x/tools/playground/socket"
19    "golang.org/x/tools/present"
20
21    // This will register a handler at /compile that will proxy to the
22    // respective endpoints at play.golang.org. This allows the frontend to call
23    // these endpoints without needing cross-origin request sharing (CORS).
24    // Note that this is imported regardless of whether the endpoints are used or
25    // not (in the case of a local socket connection, they are not called).
26    _ "golang.org/x/tools/playground"
27)
28
29var scripts = []string{"jquery.js""jquery-ui.js""playground.js""play.js"}
30
31// playScript registers an HTTP handler at /play.js that serves all the
32// scripts specified by the variable above, and appends a line that
33// initializes the playground with the specified transport.
34func playScript(roottransport string) {
35    modTime := time.Now()
36    var buf bytes.Buffer
37    for _p := range scripts {
38        if sok := static.Files[p]; ok {
39            buf.WriteString(s)
40            continue
41        }
42        berr := ioutil.ReadFile(filepath.Join(root"static"p))
43        if err != nil {
44            panic(err)
45        }
46        buf.Write(b)
47    }
48    fmt.Fprintf(&buf"\ninitPlayground(new %v());\n"transport)
49    b := buf.Bytes()
50    http.HandleFunc("/play.js", func(w http.ResponseWriterr *http.Request) {
51        w.Header().Set("Content-type""application/javascript")
52        http.ServeContent(wr""modTimebytes.NewReader(b))
53    })
54}
55
56func initPlayground(basepath stringorigin *url.URL) {
57    if !present.PlayEnabled {
58        return
59    }
60    if *usePlayground {
61        playScript(basepath"HTTPTransport")
62        return
63    }
64
65    if *nativeClient {
66        // When specifying nativeClient, non-Go code cannot be executed
67        // because the NaCl setup doesn't support doing so.
68        socket.RunScripts = false
69        socket.Environ = func() []string {
70            if runtime.GOARCH == "amd64" {
71                return environ("GOOS=nacl""GOARCH=amd64p32")
72            }
73            return environ("GOOS=nacl")
74        }
75    }
76    playScript(basepath"SocketTransport")
77    http.Handle("/socket"socket.NewHandler(origin))
78}
79
80func playable(c present.Codebool {
81    play := present.PlayEnabled && c.Play
82
83    // Restrict playable files to only Go source files when using play.golang.org,
84    // since there is no method to execute shell scripts there.
85    if *usePlayground {
86        return play && c.Ext == ".go"
87    }
88    return play
89}
90
MembersX
static
socket
playScript.root
playScript.buf
playScript.RangeStmt_1157.BlockStmt.err
initPlayground.basepath
ioutil
playScript.modTime
playScript.RangeStmt_1157.BlockStmt.b
playable
playable.c
bytes
runtime
time
_
playScript.transport
playScript.RangeStmt_1157.p
playScript.b
playScript
initPlayground
initPlayground.origin
Members
X