cmd,internal/frontend: remove legacy playground code
Now that interactive playgrounds have been rolled out
the legacy code can be removed.
Change-Id: I6bab20a24364ba996197ac4bc4553c779e4fab1d
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/311849
Trust: Jamal Carvalho <jamal@golang.org>
Reviewed-by: Julie Qiu <julie@golang.org>
diff --git a/cmd/frontend/main.go b/cmd/frontend/main.go
index a910af8..988822a 100644
--- a/cmd/frontend/main.go
+++ b/cmd/frontend/main.go
@@ -142,7 +142,6 @@
postgres.SearchResponseCount,
frontend.FetchLatencyDistribution,
frontend.FetchResponseCount,
- frontend.PlaygroundShareRequestCount,
frontend.VersionTypeCount,
middleware.CacheResultCount,
middleware.CacheErrorCount,
diff --git a/internal/frontend/playground.go b/internal/frontend/playground.go
index eb926b1..5c38ba9 100644
--- a/internal/frontend/playground.go
+++ b/internal/frontend/playground.go
@@ -7,101 +7,36 @@
import (
"encoding/json"
"go/format"
- "io"
"net/http"
"net/http/httputil"
- "strconv"
+ "net/url"
"strings"
- "go.opencensus.io/stats"
- "go.opencensus.io/stats/view"
- "go.opencensus.io/tag"
"golang.org/x/pkgsite/internal/log"
)
// playgroundURL is the playground endpoint used for share links.
-const playgroundURL = "https://play.golang.org"
-
-var (
- keyPlaygroundShareStatus = tag.MustNewKey("playground.share.status")
- playgroundShareStatus = stats.Int64(
- "go-discovery/playground_share_count",
- "The status of a request to play.golang.org/share",
- stats.UnitDimensionless,
- )
-
- PlaygroundShareRequestCount = &view.View{
- Name: "go-discovery/playground/share_count",
- Measure: playgroundShareStatus,
- Aggregation: view.Count(),
- Description: "Playground share request count",
- TagKeys: []tag.Key{keyPlaygroundShareStatus},
- }
-)
-
-// handlePlay handles requests that mirror play.golang.org/share.
-func (s *Server) handlePlay(w http.ResponseWriter, r *http.Request) {
- makeFetchPlayRequest(w, r, playgroundURL)
-}
+var playgroundURL = &url.URL{Scheme: "https", Host: "play.golang.org"}
func httpErrorStatus(w http.ResponseWriter, status int) {
http.Error(w, http.StatusText(status), status)
}
-func makeFetchPlayRequest(w http.ResponseWriter, r *http.Request, pgURL string) {
- ctx := r.Context()
- if r.Method != http.MethodPost {
- httpErrorStatus(w, http.StatusMethodNotAllowed)
- return
- }
- req, err := http.NewRequest("POST", pgURL+"/share", r.Body)
- if err != nil {
- log.Errorf(ctx, "ERROR share error: %v", err)
- httpErrorStatus(w, http.StatusInternalServerError)
- return
- }
- req.Header.Set("Content-Type", "text/plain; charset=utf-8")
- req = req.WithContext(r.Context())
- resp, err := http.DefaultClient.Do(req)
- if err != nil {
- log.Errorf(ctx, "ERROR share error: %v", err)
- httpErrorStatus(w, http.StatusInternalServerError)
- return
- }
- stats.RecordWithTags(r.Context(),
- []tag.Mutator{tag.Upsert(keyPlaygroundShareStatus, strconv.Itoa(resp.StatusCode))},
- playgroundShareStatus.M(int64(resp.StatusCode)),
- )
- copyHeader := func(k string) {
- if v := resp.Header.Get(k); v != "" {
- w.Header().Set(k, v)
- }
- }
- copyHeader("Content-Type")
- copyHeader("Content-Length")
- defer resp.Body.Close()
- w.WriteHeader(resp.StatusCode)
- if _, err := io.Copy(w, resp.Body); err != nil {
- log.Errorf(ctx, "ERROR writing shareId: %v", err)
- }
-}
-
// proxyPlayground is a handler that proxies playground requests to play.golang.org.
func (s *Server) proxyPlayground(w http.ResponseWriter, r *http.Request) {
- makePlaygroundProxy().ServeHTTP(w, r)
+ makePlaygroundProxy(playgroundURL).ServeHTTP(w, r)
}
// makePlaygroundProxy creates a proxy that sends requests to play.golang.org.
// The prefix /play is removed from the URL path.
-func makePlaygroundProxy() *httputil.ReverseProxy {
+func makePlaygroundProxy(pgURL *url.URL) *httputil.ReverseProxy {
return &httputil.ReverseProxy{
Director: func(req *http.Request) {
- originHost := "play.golang.org"
req.Header.Add("X-Forwarded-Host", req.Host)
- req.Header.Add("X-Origin-Host", originHost)
- req.Host = originHost
- req.URL.Scheme = "https"
- req.URL.Host = originHost
+ req.Header.Add("X-Origin-Host", pgURL.Host)
+ req.Host = pgURL.Host
+ req.URL.Scheme = pgURL.Scheme
+ req.URL.Host = pgURL.Host
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/play")
},
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
diff --git a/internal/frontend/playground_test.go b/internal/frontend/playground_test.go
index 06f280b..85ae8ad 100644
--- a/internal/frontend/playground_test.go
+++ b/internal/frontend/playground_test.go
@@ -10,6 +10,7 @@
"io/ioutil"
"net/http"
"net/http/httptest"
+ "net/url"
"strings"
"testing"
)
@@ -31,11 +32,15 @@
}
}))
defer ts.Close()
- pgURL = ts.URL
+ testURL, err := url.Parse(ts.URL)
+ if err != nil {
+ t.Fatal(err)
+ }
+ pgURL = testURL
}
testCases := []struct {
- pgURL string
+ pgURL *url.URL
method string
desc string
body string
@@ -70,7 +75,7 @@
shareID: "UCPdVNrl0-P",
},
{
- pgURL: "/*?",
+ pgURL: &url.URL{Path: "/*?"},
method: http.MethodPost,
desc: "Share endpoint: Malformed URL returns internal server error",
code: http.StatusInternalServerError,
@@ -80,13 +85,14 @@
t.Run(test.desc, func(t *testing.T) {
body := strings.NewReader(test.body)
- req, err := http.NewRequest(test.method, "/play", body)
+ req, err := http.NewRequest(test.method, "/play/share", body)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
w := httptest.NewRecorder()
- makeFetchPlayRequest(w, req, test.pgURL)
+ proxy := makePlaygroundProxy(test.pgURL)
+ proxy.ServeHTTP(w, req)
res := w.Result()
if got, want := res.StatusCode, test.code; got != want {
diff --git a/internal/frontend/server.go b/internal/frontend/server.go
index 96cf56a..b43647e 100644
--- a/internal/frontend/server.go
+++ b/internal/frontend/server.go
@@ -137,8 +137,6 @@
handle("/mod/", http.HandlerFunc(s.handleModuleDetailsRedirect))
handle("/pkg/", http.HandlerFunc(s.handlePackageDetailsRedirect))
handle("/fetch/", fetchHandler)
- // This is legacy handler to be replaced by /play/share.
- handle("/play", http.HandlerFunc(s.handlePlay))
handle("/play/compile", http.HandlerFunc(s.proxyPlayground))
handle("/play/fmt", http.HandlerFunc(s.handleFmt))
handle("/play/share", http.HandlerFunc(s.proxyPlayground))