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 | |
5 | package main |
6 | |
7 | import ( |
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 | |
29 | var 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. |
34 | func playScript(root, transport string) { |
35 | modTime := time.Now() |
36 | var buf bytes.Buffer |
37 | for _, p := range scripts { |
38 | if s, ok := static.Files[p]; ok { |
39 | buf.WriteString(s) |
40 | continue |
41 | } |
42 | b, err := 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.ResponseWriter, r *http.Request) { |
51 | w.Header().Set("Content-type", "application/javascript") |
52 | http.ServeContent(w, r, "", modTime, bytes.NewReader(b)) |
53 | }) |
54 | } |
55 | |
56 | func initPlayground(basepath string, origin *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 | |
80 | func playable(c present.Code) bool { |
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 |
Members