cmd/coordinator: redirect from http to https in production

By now, there's no more need to be able to serve requests over http
instead of https, so start always redirecting to https instead.

Fixes golang/go#27870

Change-Id: I257ce732597839dc511c3e9ce440340494c62191
Reviewed-on: https://go-review.googlesource.com/c/build/+/168138
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/cmd/coordinator/coordinator.go b/cmd/coordinator/coordinator.go
index 1b73f71..11a12c7 100644
--- a/cmd/coordinator/coordinator.go
+++ b/cmd/coordinator/coordinator.go
@@ -315,7 +315,7 @@
 		if *mode == "dev" {
 			return
 		}
-		var handler http.Handler = httpRouter{}
+		var handler http.Handler = httpToHTTPSRedirector{}
 		if autocertManager != nil {
 			handler = autocertManager.HTTPHandler(handler)
 		}
@@ -369,6 +369,17 @@
 	}
 }
 
+// httpToHTTPSRedirector redirects all requests from http to https.
+type httpToHTTPSRedirector struct{}
+
+func (httpToHTTPSRedirector) ServeHTTP(w http.ResponseWriter, req *http.Request) {
+	w.Header().Set("Connection", "close")
+	u := *req.URL
+	u.Scheme = "https"
+	u.Host = req.Host
+	http.Redirect(w, req, u.String(), http.StatusMovedPermanently)
+}
+
 // watcherProxy is the proxy which forwards from
 // https://farmer.golang.org/ to the gitmirror kubernetes service (git
 // cache+sync).