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.
17 "golang.org/x/tools/godoc/static"
18 "golang.org/x/tools/playground/socket"
19 "golang.org/x/tools/present"
21 // This will register handlers at /compile and /share 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"
29 var scripts = []string{"jquery.js", "jquery-ui.js", "playground.js", "play.js"}
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.
34 func playScript(root, transport string) {
37 for _, p := range scripts {
38 if s, ok := static.Files[p]; ok {
42 b, err := ioutil.ReadFile(filepath.Join(root, "static", p))
48 fmt.Fprintf(&buf, "\ninitPlayground(new %v());\n", transport)
50 http.HandleFunc("/play.js", func(w http.ResponseWriter, r *http.Request) {
51 w.Header().Set("Content-type", "application/javascript")
52 http.ServeContent(w, r, "", modTime, bytes.NewReader(b))
56 func initPlayground(basepath string, origin *url.URL) {
57 if !present.PlayEnabled {
61 playScript(basepath, "HTTPTransport")
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")
73 return environ("GOOS=nacl")
76 playScript(basepath, "SocketTransport")
77 http.Handle("/socket", socket.NewHandler(origin))
80 func playable(c present.Code) bool {
81 play := present.PlayEnabled && c.Play
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.
86 return play && c.Ext == ".go"