aboutsummaryrefslogtreecommitdiff
path: root/playground
diff options
context:
space:
mode:
authorAndrew Gerrand <adg@golang.org>2013-09-19 10:45:22 +1000
committerAndrew Gerrand <adg@golang.org>2013-09-19 10:45:22 +1000
commita76da35c4055dbcb79bee3370a2ddb1e1b04ee2f (patch)
tree5d6e1d19bfb0c695372bbd0c03235ab642cce7b6 /playground
parent2695d311b910e357ea1a347b221676a6906850bd (diff)
downloadgolang-x-tools-a76da35c4055dbcb79bee3370a2ddb1e1b04ee2f.tar.gz
go.tools: move playground to repo root
R=golang-dev, r CC=golang-dev https://golang.org/cl/13451046
Diffstat (limited to 'playground')
-rw-r--r--playground/appengine.go22
-rw-r--r--playground/common.go46
-rw-r--r--playground/local.go20
3 files changed, 88 insertions, 0 deletions
diff --git a/playground/appengine.go b/playground/appengine.go
new file mode 100644
index 000000000..073b419d3
--- /dev/null
+++ b/playground/appengine.go
@@ -0,0 +1,22 @@
+// Copyright 2012 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
+
+package playground
+
+import (
+ "net/http"
+
+ "appengine"
+ "appengine/urlfetch"
+)
+
+func client(r *http.Request) *http.Client {
+ return urlfetch.Client(appengine.NewContext(r))
+}
+
+func report(r *http.Request, err error) {
+ appengine.NewContext(r).Errorf("%v", err)
+}
diff --git a/playground/common.go b/playground/common.go
new file mode 100644
index 000000000..055136297
--- /dev/null
+++ b/playground/common.go
@@ -0,0 +1,46 @@
+// Copyright 2013 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.
+
+// Package playground registers HTTP handlers at "/compile" and "/share" that
+// proxy requests to the golang.org playground service.
+// This package may be used unaltered on App Engine.
+package playground
+
+import (
+ "bytes"
+ "fmt"
+ "io"
+ "net/http"
+)
+
+const baseURL = "http://play.golang.org"
+
+func init() {
+ http.HandleFunc("/compile", bounce)
+ http.HandleFunc("/share", bounce)
+}
+
+func bounce(w http.ResponseWriter, r *http.Request) {
+ b := new(bytes.Buffer)
+ if err := passThru(b, r); err != nil {
+ http.Error(w, "Server error.", http.StatusInternalServerError)
+ report(r, err)
+ return
+ }
+ io.Copy(w, b)
+}
+
+func passThru(w io.Writer, req *http.Request) error {
+ defer req.Body.Close()
+ url := baseURL + req.URL.Path
+ r, err := client(req).Post(url, req.Header.Get("Content-type"), req.Body)
+ if err != nil {
+ return fmt.Errorf("making POST request: %v", err)
+ }
+ defer r.Body.Close()
+ if _, err := io.Copy(w, r.Body); err != nil {
+ return fmt.Errorf("copying response Body: %v", err)
+ }
+ return nil
+}
diff --git a/playground/local.go b/playground/local.go
new file mode 100644
index 000000000..b114b8778
--- /dev/null
+++ b/playground/local.go
@@ -0,0 +1,20 @@
+// Copyright 2012 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
+
+package playground
+
+import (
+ "log"
+ "net/http"
+)
+
+func client(r *http.Request) *http.Client {
+ return http.DefaultClient
+}
+
+func report(r *http.Request, err error) {
+ log.Println(err)
+}