godoc: proxy /compile requests to play.golang.org

Now that play.golang.org and sandbox[-flex].golang.org have been
merged, proxy requests to the former.

Change-Id: I7d18d0494fd54c2357dc53952fa458ceb1380aca
Reviewed-on: https://go-review.googlesource.com/86253
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Andrew Bonventre 2018-01-04 18:12:00 -05:00
parent 96b5a5404f
commit 951512dfed
3 changed files with 15 additions and 35 deletions

View File

@ -76,7 +76,7 @@ makeAppYaml() {
application: godoc
version: 1
runtime: go
api_version: go1.4beta
api_version: go1
handlers:
- url: /.*

View File

@ -6,7 +6,7 @@
package godoc
import "appengine"
import "google.golang.org/appengine"
func init() {
onAppengine = !appengine.IsDevAppServer()

View File

@ -4,8 +4,7 @@
// +build appengine
// Package proxy proxies requests to the sandbox compiler service and the
// playground share handler.
// Package proxy proxies requests to the playground's compile and share handlers.
// It is designed to run only on the instance of godoc that serves golang.org.
package proxy
@ -26,7 +25,6 @@ import (
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"google.golang.org/appengine/memcache"
"google.golang.org/appengine/urlfetch"
)
@ -45,13 +43,7 @@ type Event struct {
Delay time.Duration // time to wait before printing Message
}
const (
// We need to use HTTP here for "reasons", but the traffic isn't
// sensitive and it only travels across Google's internal network
// so we should be OK.
sandboxURL = "http://sandbox.golang.org/compile"
playgroundURL = "https://play.golang.org"
)
const playgroundURL = "https://play.golang.org"
const expires = 7 * 24 * time.Hour // 1 week
var cacheControlHeader = fmt.Sprintf("public, max-age=%d", int(expires.Seconds()))
@ -67,27 +59,15 @@ func compile(w http.ResponseWriter, r *http.Request) {
return
}
c := appengine.NewContext(r)
ctx := appengine.NewContext(r)
body := r.FormValue("body")
res := &Response{}
key := cacheKey(body)
if _, err := memcache.Gob.Get(c, key, res); err != nil {
if err != memcache.ErrCacheMiss {
log.Errorf(c, "getting response cache: %v", err)
}
req := &Request{Body: body}
if err := makeSandboxRequest(c, req, res); err != nil {
log.Errorf(c, "compile error: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
item := &memcache.Item{Key: key, Object: res}
if err := memcache.Gob.Set(c, item); err != nil {
log.Errorf(c, "setting response cache: %v", err)
}
req := &Request{Body: body}
if err := makeCompileRequest(ctx, req, res); err != nil {
log.Errorf(ctx, "compile error: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
expiresTime := time.Now().Add(expires).UTC()
@ -105,18 +85,18 @@ func compile(w http.ResponseWriter, r *http.Request) {
}{res.Errors, flatten(res.Events)}
}
if err := json.NewEncoder(w).Encode(out); err != nil {
log.Errorf(c, "encoding response: %v", err)
log.Errorf(ctx, "encoding response: %v", err)
}
}
// makeSandboxRequest sends the given Request to the sandbox
// and stores the response in the given Response.
func makeSandboxRequest(c context.Context, req *Request, res *Response) error {
// makePlaygroundRequest sends the given Request to the playground compile
// endpoint and stores the response in the given Response.
func makeCompileRequest(ctx context.Context, req *Request, res *Response) error {
reqJ, err := json.Marshal(req)
if err != nil {
return fmt.Errorf("marshalling request: %v", err)
}
r, err := urlfetch.Client(c).Post(sandboxURL, "application/json", bytes.NewReader(reqJ))
r, err := urlfetch.Client(ctx).Post(playgroundURL+"/compile", "application/json", bytes.NewReader(reqJ))
if err != nil {
return fmt.Errorf("making request: %v", err)
}