From ffb6076c75ada9e4d551aa9cb28d72e2f3d8074a Mon Sep 17 00:00:00 2001 From: Andrew Gerrand Date: Thu, 17 Sep 2015 12:11:32 +1000 Subject: [PATCH] playground: recognize managed vm's as being "on app engine" Change-Id: I117b113e2782cab1180e6f56e1869525564ab18f Reviewed-on: https://go-review.googlesource.com/14663 Reviewed-by: Andrew Gerrand --- cmd/present/appengine.go | 10 ++-------- cmd/present/local.go | 40 ++++++++++++-------------------------- cmd/present/play_http.go | 21 ++++++++++++++++++++ cmd/present/play_socket.go | 36 ++++++++++++++++++++++++++++++++++ playground/appenginevm.go | 11 +++++++++++ playground/common.go | 2 +- 6 files changed, 83 insertions(+), 37 deletions(-) create mode 100644 cmd/present/play_http.go create mode 100644 cmd/present/play_socket.go create mode 100644 playground/appenginevm.go diff --git a/cmd/present/appengine.go b/cmd/present/appengine.go index 7df536c18f..36661198d3 100644 --- a/cmd/present/appengine.go +++ b/cmd/present/appengine.go @@ -14,17 +14,11 @@ import ( _ "golang.org/x/tools/playground" ) -var basePath = "./present/" - func init() { - initTemplates(basePath) - playScript(basePath, "HTTPTransport") + initTemplates("./present/") present.PlayEnabled = true + initPlayground("./present/", nil) // App Engine has no /etc/mime.types mime.AddExtensionType(".svg", "image/svg+xml") } - -func playable(c present.Code) bool { - return present.PlayEnabled && c.Play && c.Ext == ".go" -} diff --git a/cmd/present/local.go b/cmd/present/local.go index d91dcc2a66..d968553a97 100644 --- a/cmd/present/local.go +++ b/cmd/present/local.go @@ -15,35 +15,34 @@ import ( "net/http" "net/url" "os" - "runtime" "strings" - "golang.org/x/tools/playground/socket" "golang.org/x/tools/present" ) const basePkg = "golang.org/x/tools/cmd/present" -var basePath string +var ( + httpAddr = flag.String("http", "127.0.0.1:3999", "HTTP service address (e.g., '127.0.0.1:3999')") + originHost = flag.String("orighost", "", "host component of web origin URL (e.g., 'localhost')") + basePath = flag.String("base", "", "base path for slide template and static resources") + nativeClient = flag.Bool("nacl", false, "use Native Client environment playground (prevents non-Go code execution)") +) func main() { - httpAddr := flag.String("http", "127.0.0.1:3999", "HTTP service address (e.g., '127.0.0.1:3999')") - originHost := flag.String("orighost", "", "host component of web origin URL (e.g., 'localhost')") - flag.StringVar(&basePath, "base", "", "base path for slide template and static resources") flag.BoolVar(&present.PlayEnabled, "play", true, "enable playground (permit execution of arbitrary user code)") - nativeClient := flag.Bool("nacl", false, "use Native Client environment playground (prevents non-Go code execution)") flag.Parse() - if basePath == "" { + if *basePath == "" { p, err := build.Default.Import(basePkg, "", build.FindOnly) if err != nil { fmt.Fprintf(os.Stderr, "Couldn't find gopresent files: %v\n", err) fmt.Fprintf(os.Stderr, basePathMessage, basePkg) os.Exit(1) } - basePath = p.Dir + *basePath = p.Dir } - err := initTemplates(basePath) + err := initTemplates(*basePath) if err != nil { log.Fatalf("Failed to parse templates: %v", err) } @@ -58,6 +57,7 @@ func main() { if err != nil { log.Fatal(err) } + origin := &url.URL{Scheme: "http"} if *originHost != "" { origin.Host = net.JoinHostPort(*originHost, port) @@ -76,20 +76,8 @@ func main() { } } - if present.PlayEnabled { - if *nativeClient { - socket.RunScripts = false - socket.Environ = func() []string { - if runtime.GOARCH == "amd64" { - return environ("GOOS=nacl", "GOARCH=amd64p32") - } - return environ("GOOS=nacl") - } - } - playScript(basePath, "SocketTransport") - http.Handle("/socket", socket.NewHandler(origin)) - } - http.Handle("/static/", http.FileServer(http.Dir(basePath))) + initPlayground(*basePath, origin) + http.Handle("/static/", http.FileServer(http.Dir(*basePath))) if !ln.Addr().(*net.TCPAddr).IP.IsLoopback() && present.PlayEnabled && !*nativeClient { @@ -100,10 +88,6 @@ func main() { log.Fatal(http.Serve(ln, nil)) } -func playable(c present.Code) bool { - return present.PlayEnabled && c.Play -} - func environ(vars ...string) []string { env := os.Environ() for _, r := range vars { diff --git a/cmd/present/play_http.go b/cmd/present/play_http.go new file mode 100644 index 0000000000..b4fa4fcf7f --- /dev/null +++ b/cmd/present/play_http.go @@ -0,0 +1,21 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build appengine appenginevm + +package main + +import ( + "net/url" + + "golang.org/x/tools/present" +) + +func initPlayground(basepath string, origin *url.URL) { + playScript(basepath, "HTTPTransport") +} + +func playable(c present.Code) bool { + return present.PlayEnabled && c.Play && c.Ext == ".go" +} diff --git a/cmd/present/play_socket.go b/cmd/present/play_socket.go new file mode 100644 index 0000000000..df63edabd5 --- /dev/null +++ b/cmd/present/play_socket.go @@ -0,0 +1,36 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build !appengine,!appenginevm + +package main + +import ( + "net/http" + "net/url" + "runtime" + + "golang.org/x/tools/playground/socket" + "golang.org/x/tools/present" +) + +func initPlayground(basepath string, origin *url.URL) { + if present.PlayEnabled { + if *nativeClient { + socket.RunScripts = false + socket.Environ = func() []string { + if runtime.GOARCH == "amd64" { + return environ("GOOS=nacl", "GOARCH=amd64p32") + } + return environ("GOOS=nacl") + } + } + playScript(basepath, "SocketTransport") + http.Handle("/socket", socket.NewHandler(origin)) + } +} + +func playable(c present.Code) bool { + return present.PlayEnabled && c.Play +} diff --git a/playground/appenginevm.go b/playground/appenginevm.go new file mode 100644 index 0000000000..aa3a21268e --- /dev/null +++ b/playground/appenginevm.go @@ -0,0 +1,11 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build appenginevm + +package playground + +func init() { + onAppengine = true +} diff --git a/playground/common.go b/playground/common.go index 9f428f5136..16591a7a3a 100644 --- a/playground/common.go +++ b/playground/common.go @@ -49,7 +49,7 @@ func passThru(w io.Writer, req *http.Request) error { return nil } -var onAppengine = false // will be overriden by appengine.go +var onAppengine = false // will be overriden by appengine.go and appenginevm.go func allowShare(r *http.Request) bool { if !onAppengine {