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:
parent
96b5a5404f
commit
951512dfed
|
@ -76,7 +76,7 @@ makeAppYaml() {
|
||||||
application: godoc
|
application: godoc
|
||||||
version: 1
|
version: 1
|
||||||
runtime: go
|
runtime: go
|
||||||
api_version: go1.4beta
|
api_version: go1
|
||||||
|
|
||||||
handlers:
|
handlers:
|
||||||
- url: /.*
|
- url: /.*
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
package godoc
|
package godoc
|
||||||
|
|
||||||
import "appengine"
|
import "google.golang.org/appengine"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
onAppengine = !appengine.IsDevAppServer()
|
onAppengine = !appengine.IsDevAppServer()
|
||||||
|
|
|
@ -4,8 +4,7 @@
|
||||||
|
|
||||||
// +build appengine
|
// +build appengine
|
||||||
|
|
||||||
// Package proxy proxies requests to the sandbox compiler service and the
|
// Package proxy proxies requests to the playground's compile and share handlers.
|
||||||
// playground share handler.
|
|
||||||
// It is designed to run only on the instance of godoc that serves golang.org.
|
// It is designed to run only on the instance of godoc that serves golang.org.
|
||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
|
@ -26,7 +25,6 @@ import (
|
||||||
|
|
||||||
"google.golang.org/appengine"
|
"google.golang.org/appengine"
|
||||||
"google.golang.org/appengine/log"
|
"google.golang.org/appengine/log"
|
||||||
"google.golang.org/appengine/memcache"
|
|
||||||
"google.golang.org/appengine/urlfetch"
|
"google.golang.org/appengine/urlfetch"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -45,13 +43,7 @@ type Event struct {
|
||||||
Delay time.Duration // time to wait before printing Message
|
Delay time.Duration // time to wait before printing Message
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const playgroundURL = "https://play.golang.org"
|
||||||
// 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 expires = 7 * 24 * time.Hour // 1 week
|
const expires = 7 * 24 * time.Hour // 1 week
|
||||||
var cacheControlHeader = fmt.Sprintf("public, max-age=%d", int(expires.Seconds()))
|
var cacheControlHeader = fmt.Sprintf("public, max-age=%d", int(expires.Seconds()))
|
||||||
|
@ -67,27 +59,15 @@ func compile(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c := appengine.NewContext(r)
|
ctx := appengine.NewContext(r)
|
||||||
|
|
||||||
body := r.FormValue("body")
|
body := r.FormValue("body")
|
||||||
res := &Response{}
|
res := &Response{}
|
||||||
key := cacheKey(body)
|
req := &Request{Body: body}
|
||||||
if _, err := memcache.Gob.Get(c, key, res); err != nil {
|
if err := makeCompileRequest(ctx, req, res); err != nil {
|
||||||
if err != memcache.ErrCacheMiss {
|
log.Errorf(ctx, "compile error: %v", err)
|
||||||
log.Errorf(c, "getting response cache: %v", err)
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
}
|
return
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
expiresTime := time.Now().Add(expires).UTC()
|
expiresTime := time.Now().Add(expires).UTC()
|
||||||
|
@ -105,18 +85,18 @@ func compile(w http.ResponseWriter, r *http.Request) {
|
||||||
}{res.Errors, flatten(res.Events)}
|
}{res.Errors, flatten(res.Events)}
|
||||||
}
|
}
|
||||||
if err := json.NewEncoder(w).Encode(out); err != nil {
|
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
|
// makePlaygroundRequest sends the given Request to the playground compile
|
||||||
// and stores the response in the given Response.
|
// endpoint and stores the response in the given Response.
|
||||||
func makeSandboxRequest(c context.Context, req *Request, res *Response) error {
|
func makeCompileRequest(ctx context.Context, req *Request, res *Response) error {
|
||||||
reqJ, err := json.Marshal(req)
|
reqJ, err := json.Marshal(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("marshalling request: %v", err)
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("making request: %v", err)
|
return fmt.Errorf("making request: %v", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue