From 4b5486d0e5c44f3e96aea9d5d8a00185872c416f Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Fri, 26 Feb 2021 01:28:40 -0500 Subject: internal/dl: move template to lib/godoc This completes an old TODO and uses the standard site frame for the download page instead of having a separate copy. Change-Id: Id647a6bb03d563b007b16cab51f40083d071c944 Reviewed-on: https://go-review.googlesource.com/c/website/+/317658 Trust: Russ Cox Run-TryBot: Russ Cox Reviewed-by: Dmitri Shuralyov --- _content/lib/godoc/dl.html | 159 +++++++++++++++++++++++ _content/lib/godoc/style.css | 60 ++++++++- cmd/golangorg/prod.go | 2 +- internal/dl/dl.go | 15 +-- internal/dl/dl_test.go | 3 + internal/dl/server.go | 21 +-- internal/dl/server_test.go | 3 + internal/dl/tmpl.go | 298 ------------------------------------------- 8 files changed, 245 insertions(+), 316 deletions(-) create mode 100644 _content/lib/godoc/dl.html delete mode 100644 internal/dl/tmpl.go diff --git a/_content/lib/godoc/dl.html b/_content/lib/godoc/dl.html new file mode 100644 index 00000000..11a893a8 --- /dev/null +++ b/_content/lib/godoc/dl.html @@ -0,0 +1,159 @@ +{{with .Data}} +

+After downloading a binary release suitable for your system, +please follow the installation instructions. +

+ +

+If you are building from source, +follow the source installation instructions. +

+ +

+See the release history for more +information about Go releases. +

+ +

+ As of Go 1.13, the go command by default downloads and authenticates + modules using the Go module mirror and Go checksum database run by Google. See + https://proxy.golang.org/privacy + for privacy information about these services and the + go command documentation + for configuration details including how to disable the use of these servers or use + different ones. +

+ +{{with .Featured}} + +{{range .}} +{{template "download" .}} +{{end}} +{{end}} + +
+ +{{with .Stable}} +

Stable versions

+{{template "download-releases" .}} +{{end}} + +{{with .Unstable}} +

Unstable version

+{{template "download-releases" .}} +{{end}} + +{{with .Archive}} +
+ +
+

Archived versions ▾

+ {{template "download-releases" .}} +
+
+{{end}} + + +{{end}} + +{{define "download-releases"}} +{{range .}} +
+ +
+

{{.Version}} ▾

+ {{if .Stable}}{{else}} +

This is an unstable version of Go. Use with caution.

+

If you already have Go installed, you can install this version by running:

+
+go get golang.org/dl/{{.Version}}
+
+

Then, use the {{.Version}} command instead of the go command to use {{.Version}}.

+ {{end}} + {{template "download-files" .}} +
+
+{{end}} +{{end}} + +{{define "download-files"}} + + + + + + + + + {{/* Use the checksum type of the first file for the column heading. */}} + + + +{{if .SplitPortTable}} + {{range .Files}}{{if .PrimaryPort}}{{template "download-file" .}}{{end}}{{end}} + + {{/* TODO(cbro): add a link to an explanatory doc page */}} + + {{range .Files}}{{if not .PrimaryPort}}{{template "download-file" .}}{{end}}{{end}} +{{else}} + {{range .Files}}{{template "download-file" .}}{{end}} +{{end}} +
File nameKindOSArchSize{{(index .Files 0).ChecksumType}} Checksum
Other Ports
+{{end}} + +{{define "download-file"}} + + {{.Filename}} + {{.PrettyKind}} + {{.PrettyOS}} + {{.PrettyArch}} + {{.PrettySize}} + {{.PrettyChecksum}} + +{{end}} + +{{define "download"}} + +
{{.Platform}}
+{{with .Requirements}}
{{.}}
{{end}} +
+ {{.Filename}} + {{if .Size}}({{.PrettySize}}){{end}} +
+
+{{end}} diff --git a/_content/lib/godoc/style.css b/_content/lib/godoc/style.css index 7e5008d7..b83700f3 100644 --- a/_content/lib/godoc/style.css +++ b/_content/lib/godoc/style.css @@ -1070,4 +1070,62 @@ a.error { white-space: pre-wrap; page-break-inside: avoid; } -} \ No newline at end of file +} +table.downloadtable { + margin-left: 20px; + margin-right: 20px; + border-collapse: collapse; +} +table.downloadtable tr { + background-color: #f0f0f0; +} +table.downloadtable tr:nth-child(2n), table.downloadtable tr.first { + background-color: white; +} +table.downloadtable td, table.downloadtable th { + white-space: nowrap; + padding: 6px 10px; +} +table.downloadtable tt { + font-size: xx-small; +} +table.downloadtable tr.highlight td { + font-weight: bold; +} +a.downloadBox { + display: block; + color: #222; + border: 1px solid #375EAB; + border-radius: 5px; + background: #E0EBF5; + width: 280px; + float: left; + margin-left: 10px; + margin-bottom: 10px; + padding: 10px; +} +a.downloadBox:hover { + text-decoration: none; +} +.downloadBox .platform { + font-size: large; +} +.downloadBox .filename { + color: #007d9c; + font-weight: bold; + line-height: 1.5em; +} +a.downloadBox:hover .filename { + text-decoration: underline; +} +.downloadBox .size { + font-size: small; + font-weight: normal; +} +.downloadBox .reqs { + font-size: small; + font-style: italic; +} +.downloadBox .checksum { + font-size: 5pt; +} diff --git a/cmd/golangorg/prod.go b/cmd/golangorg/prod.go index b156bfce..e6e3a548 100644 --- a/cmd/golangorg/prod.go +++ b/cmd/golangorg/prod.go @@ -40,7 +40,7 @@ func lateSetup(mux *http.ServeMux) { datastoreClient, memcacheClient := getClients() - dl.RegisterHandlers(mux, datastoreClient, memcacheClient) + dl.RegisterHandlers(mux, site, datastoreClient, memcacheClient) short.RegisterHandlers(mux, datastoreClient, memcacheClient) // Register /compile and /share handlers against the default serve mux diff --git a/internal/dl/dl.go b/internal/dl/dl.go index d9864218..2a3a7da0 100644 --- a/internal/dl/dl.go +++ b/internal/dl/dl.go @@ -2,6 +2,9 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +//go:build go1.16 +// +build go1.16 + // Package dl implements a simple downloads frontend server. // // It accepts HTTP POST requests to create a new download metadata entity, and @@ -24,7 +27,6 @@ package dl import ( "fmt" - "html/template" "regexp" "sort" "strconv" @@ -33,7 +35,7 @@ import ( ) const ( - cacheKey = "download_list_4" // increment if listTemplateData changes + cacheKey = "download_list_5" // increment if listTemplateData changes cacheDuration = time.Hour ) @@ -58,6 +60,9 @@ func (f File) ChecksumType() string { return "SHA1" } +func (f File) PrettyArch() string { return pretty(f.Arch) } +func (f File) PrettyKind() string { return pretty(f.Kind) } + func (f File) PrettyChecksum() string { if f.ChecksumSHA256 != "" { return f.ChecksumSHA256 @@ -177,14 +182,8 @@ var featuredFiles = []Feature{ type listTemplateData struct { Featured []Feature Stable, Unstable, Archive []Release - GoogleCN bool } -var ( - listTemplate = template.Must(template.New("").Funcs(templateFuncs).Parse(templateHTML)) - templateFuncs = template.FuncMap{"pretty": pretty} -) - func filesToFeatured(fs []File) (featured []Feature) { for _, feature := range featuredFiles { for _, file := range fs { diff --git a/internal/dl/dl_test.go b/internal/dl/dl_test.go index 1e562b3f..1a1e6326 100644 --- a/internal/dl/dl_test.go +++ b/internal/dl/dl_test.go @@ -2,6 +2,9 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +//go:build go1.16 +// +build go1.16 + package dl import ( diff --git a/internal/dl/server.go b/internal/dl/server.go index 7ec45a60..3f8efd41 100644 --- a/internal/dl/server.go +++ b/internal/dl/server.go @@ -2,6 +2,9 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +//go:build go1.16 +// +build go1.16 + package dl import ( @@ -21,15 +24,17 @@ import ( "cloud.google.com/go/datastore" "golang.org/x/website/internal/env" "golang.org/x/website/internal/memcache" + "golang.org/x/website/internal/web" ) type server struct { + site *web.Site datastore *datastore.Client memcache *memcache.CodecClient } -func RegisterHandlers(mux *http.ServeMux, dc *datastore.Client, mc *memcache.Client) { - s := server{dc, mc.WithCodec(memcache.Gob)} +func RegisterHandlers(mux *http.ServeMux, site *web.Site, dc *datastore.Client, mc *memcache.Client) { + s := server{site, dc, mc.WithCodec(memcache.Gob)} mux.HandleFunc("/dl", s.getHandler) mux.HandleFunc("/dl/", s.getHandler) // also serves listHandler mux.HandleFunc("/dl/upload", s.uploadHandler) @@ -49,9 +54,7 @@ func (h server) listHandler(w http.ResponseWriter, r *http.Request) { return } ctx := r.Context() - d := listTemplateData{ - GoogleCN: googleCN(r), - } + d := listTemplateData{} if err := h.memcache.Get(ctx, cacheKey, &d); err != nil { if err != memcache.ErrCacheMiss { @@ -82,9 +85,11 @@ func (h server) listHandler(w http.ResponseWriter, r *http.Request) { return } - if err := listTemplate.ExecuteTemplate(w, "root", d); err != nil { - log.Printf("ERROR executing template: %v", err) - } + h.site.ServePage(w, r, web.Page{ + Title: "Downloads", + Template: "dl.html", + Data: d, + }) } // serveJSON serves a JSON representation of d. It assumes that requests are diff --git a/internal/dl/server_test.go b/internal/dl/server_test.go index 2cff8d79..8aa52054 100644 --- a/internal/dl/server_test.go +++ b/internal/dl/server_test.go @@ -2,6 +2,9 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +//go:build go1.16 +// +build go1.16 + package dl import ( diff --git a/internal/dl/tmpl.go b/internal/dl/tmpl.go deleted file mode 100644 index d0005f21..00000000 --- a/internal/dl/tmpl.go +++ /dev/null @@ -1,298 +0,0 @@ -// Copyright 2015 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package dl - -// TODO(adg): refactor this to use the tools/godoc/static template. - -const templateHTML = ` -{{define "root"}} - - - - - - -Downloads - The Go Programming Language - - - - - - -
-
- Black Lives Matter. - Support the Equal Justice Initiative. -
- -
- -
-
- -

Downloads

- -

-After downloading a binary release suitable for your system, -please follow the installation instructions. -

- -

-If you are building from source, -follow the source installation instructions. -

- -

-See the release history for more -information about Go releases. -

- -

- As of Go 1.13, the go command by default downloads and authenticates - modules using the Go module mirror and Go checksum database run by Google. See - https://proxy.golang.org/privacy - for privacy information about these services and the - go command documentation - for configuration details including how to disable the use of these servers or use - different ones. -

- -{{with .Featured}} - -{{range .}} -{{template "download" .}} -{{end}} -{{end}} - -
- -{{with .Stable}} -

Stable versions

-{{template "releases" .}} -{{end}} - -{{with .Unstable}} -

Unstable version

-{{template "releases" .}} -{{end}} - -{{with .Archive}} -
- -
-

Archived versions ▾

- {{template "releases" .}} -
-
-{{end}} - -
-
- - - - - - -{{end}} - -{{define "releases"}} -{{range .}} -
- -
-

{{.Version}} ▾

- {{if .Stable}}{{else}} -

This is an unstable version of Go. Use with caution.

-

If you already have Go installed, you can install this version by running:

-
-go get golang.org/dl/{{.Version}}
-
-

Then, use the {{.Version}} command instead of the go command to use {{.Version}}.

- {{end}} - {{template "files" .}} -
-
-{{end}} -{{end}} - -{{define "files"}} - - - - - - - - - {{/* Use the checksum type of the first file for the column heading. */}} - - - -{{if .SplitPortTable}} - {{range .Files}}{{if .PrimaryPort}}{{template "file" .}}{{end}}{{end}} - - {{/* TODO(cbro): add a link to an explanatory doc page */}} - - {{range .Files}}{{if not .PrimaryPort}}{{template "file" .}}{{end}}{{end}} -{{else}} - {{range .Files}}{{template "file" .}}{{end}} -{{end}} -
File nameKindOSArchSize{{(index .Files 0).ChecksumType}} Checksum
Other Ports
-{{end}} - -{{define "file"}} - - {{.Filename}} - {{pretty .Kind}} - {{.PrettyOS}} - {{pretty .Arch}} - {{.PrettySize}} - {{.PrettyChecksum}} - -{{end}} - -{{define "download"}} - -
{{.Platform}}
-{{with .Requirements}}
{{.}}
{{end}} -
- {{.Filename}} - {{if .Size}}({{.PrettySize}}){{end}} -
-
-{{end}} -` -- cgit v1.3