cmd/golangorg: move googleCN code out of internal/godoc

Change-Id: I24140d73ca375cd624d852a2653c0a8bb0af9e2d
Reviewed-on: https://go-review.googlesource.com/c/website/+/293492
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
diff --git a/cmd/golangorg/main.go b/cmd/golangorg/main.go
index 14abaf6..3f102b7 100644
--- a/cmd/golangorg/main.go
+++ b/cmd/golangorg/main.go
@@ -99,6 +99,7 @@
 	if *notesRx != "" {
 		pres.NotesRx = regexp.MustCompile(*notesRx)
 	}
+	pres.GoogleCN = googleCN
 
 	readTemplates(pres)
 	mux := registerHandlers(pres)
diff --git a/internal/godoc/page.go b/internal/godoc/page.go
index d6cc640..c9f063b 100644
--- a/internal/godoc/page.go
+++ b/internal/godoc/page.go
@@ -12,9 +12,6 @@
 	"os"
 	"path/filepath"
 	"runtime"
-	"strings"
-
-	"golang.org/x/website/internal/godoc/golangorgenv"
 )
 
 // Page describes the contents of the top-level godoc webpage.
@@ -57,26 +54,7 @@
 		Title:           "File " + relpath,
 		Subtitle:        relpath,
 		Body:            applyTemplate(p.ErrorHTML, "errorHTML", err),
-		GoogleCN:        googleCN(r),
+		GoogleCN:        p.googleCN(r),
 		GoogleAnalytics: p.GoogleAnalytics,
 	})
 }
-
-// googleCN reports whether request r is considered
-// to be served from golang.google.cn.
-func googleCN(r *http.Request) bool {
-	if r.FormValue("googlecn") != "" {
-		return true
-	}
-	if strings.HasSuffix(r.Host, ".cn") {
-		return true
-	}
-	if !golangorgenv.CheckCountry() {
-		return false
-	}
-	switch r.Header.Get("X-Appengine-Country") {
-	case "", "ZZ", "CN":
-		return true
-	}
-	return false
-}
diff --git a/internal/godoc/pres.go b/internal/godoc/pres.go
index e4734bf..3403e21 100644
--- a/internal/godoc/pres.go
+++ b/internal/godoc/pres.go
@@ -68,6 +68,10 @@
 	// the query string highlighted.
 	URLForSrcQuery func(src, query string, line int) string
 
+	// GoogleCN reports whether this request should be marked GoogleCN.
+	// If the function is nil, no requests are marked GoogleCN.
+	GoogleCN func(*http.Request) bool
+
 	// GoogleAnalytics optionally adds Google Analytics via the provided
 	// tracking ID to each page.
 	GoogleAnalytics string
@@ -137,3 +141,7 @@
 func (p *Presentation) GetCmdPageInfo(abspath, relpath string, mode PageInfoMode) *PageInfo {
 	return p.cmdHandler.GetPageInfo(abspath, relpath, mode, "", "")
 }
+
+func (p *Presentation) googleCN(r *http.Request) bool {
+	return p.GoogleCN != nil && p.GoogleCN(r)
+}
diff --git a/internal/godoc/server.go b/internal/godoc/server.go
index c127769..b16449c 100644
--- a/internal/godoc/server.go
+++ b/internal/godoc/server.go
@@ -319,7 +319,7 @@
 		tabtitle = "Commands"
 	}
 
-	info.GoogleCN = googleCN(r)
+	info.GoogleCN = h.p.googleCN(r)
 	var body []byte
 	if info.Dirname == "/src" {
 		body = applyTemplate(h.p.PackageRootHTML, "packageRootHTML", info)
@@ -588,7 +588,7 @@
 		SrcPath:  relpath,
 		Tabtitle: relpath,
 		Body:     buf.Bytes(),
-		GoogleCN: googleCN(r),
+		GoogleCN: p.googleCN(r),
 	})
 }
 
@@ -659,7 +659,7 @@
 		SrcPath:  relpath,
 		Tabtitle: relpath,
 		Body:     applyTemplate(p.DirlistHTML, "dirlistHTML", info),
-		GoogleCN: googleCN(r),
+		GoogleCN: p.googleCN(r),
 	})
 }
 
@@ -696,7 +696,7 @@
 	page := Page{
 		Title:    meta.Title,
 		Subtitle: meta.Subtitle,
-		GoogleCN: googleCN(r),
+		GoogleCN: p.googleCN(r),
 	}
 
 	// evaluate as template if indicated