[x/go.dev] cmd/frontend: redirect /explore to discovery site

This adds an application for redirecting /explore requests to the
correct discovery site, depending on which domain is requested.

All static content should be served by appengine itself. The static
handler only is necessary for testing locally.

Fixes b/142726792

Change-Id: I5ae6a5630c6f01c55a705ff4fde037fd37de6fad
Reviewed-on: https://team-review.git.corp.google.com/c/golang/go.dev/+/575223
CI-Result: Cloud Build <devtools-proctor-result-processor@system.gserviceaccount.com>
Reviewed-by: Andrew Bonventre <andybons@google.com>
X-GoDev-Commit: a83e60d5df3afba145bc26f18ff6f20b7e340b21
diff --git a/go.dev/app.yaml b/go.dev/app.yaml
index 4099419..698cc1b 100644
--- a/go.dev/app.yaml
+++ b/go.dev/app.yaml
@@ -1,13 +1,17 @@
-runtime: python27
-api_version: 1
-threadsafe: true
+runtime: go113
 service: go-dev
+main: ./cmd/frontend
 
 handlers:
 - url: /
   static_files: public/index.html
   upload: public/index.html
 
+- url: /explore
+  secure: always
+  redirect_http_response_code: 301
+  script: auto
+
 # Special handler for static content in themes or page bundles.
 - url: /(.*)\.(png|svg|css|jpeg|jpg|xml|html)$
   static_files: public/\1.\2
@@ -23,3 +27,7 @@
   static_files: public/\1/index.html
   upload: public/(.*)/index.html
 
+- url: /.*
+  secure: always
+  redirect_http_response_code: 301
+  script: auto
diff --git a/go.dev/cloudbuild.ci.yaml b/go.dev/cloudbuild.ci.yaml
index fe1843f..7543a83 100644
--- a/go.dev/cloudbuild.ci.yaml
+++ b/go.dev/cloudbuild.ci.yaml
@@ -3,6 +3,11 @@
     entrypoint: bash
     args:
       - -c
+      - go test ./...
+  - name: 'mirror.gcr.io/library/golang'
+    entrypoint: bash
+    args:
+      - -c
       - go run ./cmd/events/ > ./data/events.yaml
   - name: 'gcr.io/cloud-builders/docker'
     args: ['build', '-f', 'Dockerfile.hugo', '-t', 'gcr.io/$PROJECT_ID/hugo', '.']
diff --git a/go.dev/cmd/frontend/main.go b/go.dev/cmd/frontend/main.go
new file mode 100644
index 0000000..9c95504
--- /dev/null
+++ b/go.dev/cmd/frontend/main.go
@@ -0,0 +1,41 @@
+package main
+
+import (
+	"log"
+	"net"
+	"net/http"
+	"os"
+)
+
+func main() {
+	fs := http.FileServer(http.Dir("public/"))
+	http.Handle("/", fs)
+	http.HandleFunc("/explore", exploreHandler)
+
+	p := listenPort()
+	l, err := net.Listen("tcp", ":" + p)
+	if err != nil {
+		log.Fatalf("net.Listen(%q, %q) = _, %v", "tcp", p, err)
+	}
+	defer l.Close()
+	log.Printf("Listening on http://%v/\n", l.Addr().String())
+	log.Print(http.Serve(l, nil))
+}
+
+func listenPort() string {
+	if p := os.Getenv("PORT"); p != "" {
+		return p
+	}
+	return "0"
+}
+
+func exploreHandler(w http.ResponseWriter, r *http.Request) {
+	switch r.Host {
+	case "dev.go.dev":
+		http.Redirect(w, r, "https://dev-pkg.go.dev/", http.StatusFound)
+	case "staging.go.dev":
+		http.Redirect(w, r, "https://staging-pkg.go.dev/", http.StatusFound)
+	default:
+		http.Redirect(w, r, "https://pkg.go.dev/", http.StatusFound)
+	}
+}