Skip to content

Commit

Permalink
content/static: use embed and io/fs
Browse files Browse the repository at this point in the history
This lets us delete the generated static.go.

For golang/go#41102

Change-Id: Ie09f34a83f114592eec4ba2dd9263285169374ae
Reviewed-on: https://go-review.googlesource.com/c/website/+/291690
Trust: Russ Cox <[email protected]>
Run-TryBot: Russ Cox <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
  • Loading branch information
rsc committed Feb 16, 2021
1 parent 546f845 commit aefff79
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 559 deletions.
2 changes: 1 addition & 1 deletion cmd/golangorg/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

.PHONY: usage

GO_REF ?= release-branch.go1.15
GO_REF ?= release-branch.go1.16
WEBSITE_HEAD := $(shell git rev-parse HEAD)
WEBSITE_CLEAN := $(shell (git status --porcelain | grep -q .) && echo dirty || echo clean)
ifeq ($(WEBSITE_CLEAN),clean)
Expand Down
6 changes: 3 additions & 3 deletions cmd/golangorg/appinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build go1.16
// +build golangorg

package main
Expand All @@ -25,7 +26,6 @@ import (
"golang.org/x/tools/godoc"
"golang.org/x/tools/godoc/vfs"
"golang.org/x/tools/godoc/vfs/gatefs"
"golang.org/x/tools/godoc/vfs/mapfs"
"golang.org/x/tools/godoc/vfs/zipfs"
"golang.org/x/website/content/static"
"golang.org/x/website/internal/dl"
Expand Down Expand Up @@ -79,8 +79,8 @@ func main() {
// go repository. This lets us update some documentation outside the
// Go release cycle. This includes root.html, which redirects to "/".
// See golang.org/issue/29206.
fs.Bind("/doc", mapfs.New(static.Files), "/doc", vfs.BindBefore)
fs.Bind("/lib/godoc", mapfs.New(static.Files), "/", vfs.BindReplace)
fs.Bind("/doc", vfs.FromFS(static.FS), "/doc", vfs.BindBefore)
fs.Bind("/lib/godoc", vfs.FromFS(static.FS), "/", vfs.BindReplace)

webroot := getFullPath("/src/golang.org/x/website")
fs.Bind("/favicon.ico", gatefs.New(vfs.OS(webroot), fsGate), "/favicon.ico", vfs.BindBefore)
Expand Down
13 changes: 13 additions & 0 deletions cmd/golangorg/go115.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2021 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.

// +build !go1.16

package main

import "log"

func main() {
log.Fatalf("golangorg requires Go 1.16 or later")
}
2 changes: 2 additions & 0 deletions cmd/golangorg/godoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build go1.16

package main_test

import (
Expand Down
6 changes: 3 additions & 3 deletions cmd/golangorg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// Some pages are being transitioned from $GOROOT to content/static/doc.
// See golang.org/issue/29206 and golang.org/issue/33637.

// +build go1.16
// +build !golangorg

package main
Expand All @@ -39,7 +40,6 @@ import (
"golang.org/x/tools/godoc"
"golang.org/x/tools/godoc/vfs"
"golang.org/x/tools/godoc/vfs/gatefs"
"golang.org/x/tools/godoc/vfs/mapfs"
"golang.org/x/tools/godoc/vfs/zipfs"
"golang.org/x/website/content/static"
)
Expand Down Expand Up @@ -172,8 +172,8 @@ func main() {
fs.Bind("/doc", vfs.OS(*templateDir), "/doc", vfs.BindBefore)
fs.Bind("/lib/godoc", vfs.OS(*templateDir), "/", vfs.BindBefore)
} else {
fs.Bind("/doc", mapfs.New(static.Files), "/doc", vfs.BindBefore)
fs.Bind("/lib/godoc", mapfs.New(static.Files), "/", vfs.BindReplace)
fs.Bind("/doc", vfs.FromFS(static.FS), "/doc", vfs.BindBefore)
fs.Bind("/lib/godoc", vfs.FromFS(static.FS), "/", vfs.BindReplace)
}

// Bind $GOPATH trees into Go root.
Expand Down
7 changes: 4 additions & 3 deletions cmd/golangorg/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build go1.16

package main

import (
Expand All @@ -12,7 +14,6 @@ import (

"golang.org/x/tools/godoc"
"golang.org/x/tools/godoc/vfs"
"golang.org/x/tools/godoc/vfs/mapfs"
"golang.org/x/website/content/static"
)

Expand All @@ -26,8 +27,8 @@ func TestReleaseHistory(t *testing.T) {
origFS, origPres := fs, pres
defer func() { fs, pres = origFS, origPres }()
fs = vfs.NameSpace{}
fs.Bind("/doc", mapfs.New(static.Files), "/doc", vfs.BindBefore)
fs.Bind("/lib/godoc", mapfs.New(static.Files), "/", vfs.BindReplace)
fs.Bind("/doc", vfs.FromFS(static.FS), "/doc", vfs.BindBefore)
fs.Bind("/lib/godoc", vfs.FromFS(static.FS), "/", vfs.BindReplace)
pres = godoc.NewPresentation(godoc.NewCorpus(fs))
readTemplates(pres)
mux := registerHandlers(pres)
Expand Down
43 changes: 0 additions & 43 deletions content/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,3 @@ binary when serving the [golang.org](https://golang.org) website.
The details of the directory to path mapping are documented at the top of
[`cmd/golangorg/main.go`](https://go.googlesource.com/website/+/refs/heads/master/cmd/golangorg/main.go).

TODO(dmitshur): The process below can be simplified.
See [golang.org/issue/29206#issuecomment-536099768](https://golang.org/issue/29206#issuecomment-536099768).

## Development mode

In production, CSS/JS/template assets need to be compiled into the `golangorg`
binary. It can be tedious to recompile assets every time, but you can pass a
flag to load CSS/JS/templates from disk every time a page loads:

```
golangorg -templates=$GOPATH/src/golang.org/x/website/content/static -http=:6060
```

## Recompiling static assets

Files such as `static/style.css`, `static/doc/copyright.html` and so on are not
present in the final binary. They are embedded into `static/static.go` by running
`go generate`. To compile a change and test it in your browser:

1) Make changes to an existing file such as `static/style.css`.

2) If a new file is being added to the `static` directory, add it to the `files`
slice in `static/internal/gen/gen.go`.

3) Run `go generate golang.org/x/website/content/static` so `static/static.go` is
up to date.

4) Run `go run golang.org/x/website/cmd/golangorg -http=:6060` and view your changes
in the browser at http://localhost:6060. You may need to disable your browser's cache
to avoid reloading a stale file.

A test exists to catch a possible mistake of forgetting to regenerate static assets:

```
website $ go test ./...
--- FAIL: TestStaticIsUpToDate (0.06s)
gen_test.go:27: static.go is stale. Run:
$ go generate golang.org/x/website/content/static
$ git diff
to see the differences.
FAIL
FAIL golang.org/x/website/content/static 0.650s
```
14 changes: 9 additions & 5 deletions content/static/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package static exports a map of static file content that supports the godoc
// user interface. The map should be used with the mapfs package, see
// golang.org/x/tools/godoc/vfs/mapfs.
package static // import "golang.org/x/website/content/static"
// +build go1.16

//go:generate go run makestatic.go
// Package static exports the static content as an embed.FS.
package static

import "embed"

// FS is the static content as a file system.
//go:embed *
var FS embed.FS
176 changes: 0 additions & 176 deletions content/static/internal/gen/gen.go

This file was deleted.

28 changes: 0 additions & 28 deletions content/static/internal/gen/gen_test.go

This file was deleted.

Loading

0 comments on commit aefff79

Please sign in to comment.