Skip to content

Commit 927e542

Browse files
broadydmitshur
authored andcommitted
[release-branch.go1.11] cmd/godoc: simplify dev and prod environment for App Engine
Remove all of the code generation and the concept of "APPDIR" - just generate godoc.zip and index files in the app directory. Simplify generation of the zip - use a symlink so that every file in godoc.zip is under the "goroot" directory, regardless of the environment. Previously, the prefix would be dependent on the location of the user's GOROOT. Running the setup script is now optional - it's now possible to run dev_appserver.py on a regular checkout of cmd/godoc without godoc.zip and search index files. Use environment variables to switch whether the zip file is used vs reading GOROOT from the filesystem. Updates golang/go#28893 Change-Id: I1ce95c891717fe2da975f979778fd775b23f18c8 Reviewed-on: https://go-review.googlesource.com/46725 Reviewed-by: Andrew Bonventre <[email protected]> (cherry picked from commit e9ca907) Reviewed-on: https://go-review.googlesource.com/c/150597 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 9e9bf16 commit 927e542

File tree

7 files changed

+156
-180
lines changed

7 files changed

+156
-180
lines changed

cmd/godoc/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
index.split.*
2+
godoc.index
3+
godoc.zip

cmd/godoc/README.godoc-app

+22-37
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,41 @@
1-
godoc on appengine
2-
------------------
1+
godoc on Google App Engine
2+
==========================
33

44
Prerequisites
55
-------------
66

7-
* Go appengine SDK
8-
https://developers.google.com/appengine/downloads#Google_App_Engine_SDK_for_Go
7+
* Google Cloud SDK
8+
https://cloud.google.com/sdk/
99

10-
* Go sources at tip under $GOROOT
10+
* Go sources under $GOROOT
1111

12-
* Godoc sources at tip inside $GOPATH
12+
* Godoc sources inside $GOPATH
1313
(go get -d golang.org/x/tools/cmd/godoc)
1414

1515

16-
Directory structure
17-
-------------------
16+
Running in dev_appserver.py
17+
---------------------------
1818

19-
* Let $APPDIR be the directory containing the app engine files.
20-
(e.g., $APPDIR=$HOME/godoc-app)
19+
Use dev_appserver.py to run the server in development mode:
2120

22-
* $APPDIR contains the following entries (this may change depending on
23-
app-engine release and version of godoc):
21+
dev_appserver.py app.dev.yaml
2422

25-
app.yaml
26-
golang.org/x/tools/cmd/godoc
27-
godoc.zip
28-
index.split.*
23+
To run the server with generated zip file and search index:
2924

30-
* The app.yaml file is set up per app engine documentation.
31-
For instance:
25+
./generate-index.bash
26+
dev_appserver.py app.prod.yaml
3227

33-
application: godoc-app
34-
version: 1
35-
runtime: go
36-
api_version: go1
28+
godoc should come up at http://localhost:8080
29+
Use the --host and --port flags to listen on a different address.
3730

38-
handlers:
39-
- url: /.*
40-
script: _go_app
31+
To clean up the index files, use git:
4132

33+
git clean -xn # n is dry run, replace with f
4234

43-
Configuring and running godoc
44-
-----------------------------
4535

46-
To configure godoc, run
36+
Troubleshooting
37+
---------------
4738

48-
bash setup-godoc-app.bash
49-
50-
to prepare an $APPDIR as described above. See the script for details on usage.
51-
52-
To run godoc locally, using the App Engine development server, run
53-
54-
<path to go_appengine>/dev_appserver.py $APPDIR
55-
56-
godoc should come up at http://localhost:8080 .
39+
Ensure the Cloud SDK is on your PATH and you have the app-engine-go component
40+
installed (gcloud components install app-engine-go) and your components are
41+
up-to-date (gcloud components update)

cmd/godoc/app.dev.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
runtime: go
2+
api_version: go1
3+
instance_class: F4_1G
4+
5+
handlers:
6+
- url: /s
7+
script: _go_app
8+
login: admin
9+
- url: /dl/init
10+
script: _go_app
11+
login: admin
12+
- url: /.*
13+
script: _go_app

cmd/godoc/app.prod.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
runtime: go
2+
api_version: go1
3+
instance_class: F4_1G
4+
5+
handlers:
6+
- url: /s
7+
script: _go_app
8+
login: admin
9+
- url: /dl/init
10+
script: _go_app
11+
login: admin
12+
- url: /.*
13+
script: _go_app
14+
15+
env_variables:
16+
GODOC_ZIP: godoc.zip
17+
GODOC_ZIP_PREFIX: goroot
18+
GODOC_INDEX_GLOB: 'index.split.*'

cmd/godoc/appinit.go

+28-9
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,37 @@ import (
1313
"archive/zip"
1414
"log"
1515
"net/http"
16+
"os"
1617
"path"
1718
"regexp"
19+
"runtime"
1820

1921
"golang.org/x/tools/godoc"
2022
"golang.org/x/tools/godoc/dl"
2123
"golang.org/x/tools/godoc/proxy"
2224
"golang.org/x/tools/godoc/short"
2325
"golang.org/x/tools/godoc/static"
2426
"golang.org/x/tools/godoc/vfs"
27+
"golang.org/x/tools/godoc/vfs/gatefs"
2528
"golang.org/x/tools/godoc/vfs/mapfs"
2629
"golang.org/x/tools/godoc/vfs/zipfs"
2730

2831
"google.golang.org/appengine"
2932
)
3033

3134
func init() {
35+
var (
36+
// .zip filename
37+
zipFilename = os.Getenv("GODOC_ZIP")
38+
39+
// goroot directory in .zip file
40+
zipGoroot = os.Getenv("GODOC_ZIP_PREFIX")
41+
42+
// glob pattern describing search index files
43+
// (if empty, the index is built at run-time)
44+
indexFilenames = os.Getenv("GODOC_INDEX_GLOB")
45+
)
46+
3247
enforceHosts = !appengine.IsDevAppServer()
3348
playEnabled = true
3449

@@ -37,16 +52,20 @@ func init() {
3752
log.Printf(".zip GOROOT = %s", zipGoroot)
3853
log.Printf("index files = %s", indexFilenames)
3954

40-
goroot := path.Join("/", zipGoroot) // fsHttp paths are relative to '/'
41-
42-
// read .zip file and set up file systems
43-
const zipfile = zipFilename
44-
rc, err := zip.OpenReader(zipfile)
45-
if err != nil {
46-
log.Fatalf("%s: %s\n", zipfile, err)
55+
if zipFilename != "" {
56+
goroot := path.Join("/", zipGoroot) // fsHttp paths are relative to '/'
57+
// read .zip file and set up file systems
58+
rc, err := zip.OpenReader(zipFilename)
59+
if err != nil {
60+
log.Fatalf("%s: %s\n", zipFilename, err)
61+
}
62+
// rc is never closed (app running forever)
63+
fs.Bind("/", zipfs.New(rc, zipFilename), goroot, vfs.BindReplace)
64+
} else {
65+
rootfs := gatefs.New(vfs.OS(runtime.GOROOT()), make(chan bool, 20))
66+
fs.Bind("/", rootfs, "/", vfs.BindReplace)
4767
}
48-
// rc is never closed (app running forever)
49-
fs.Bind("/", zipfs.New(rc, zipFilename), goroot, vfs.BindReplace)
68+
5069
fs.Bind("/lib/godoc", mapfs.New(static.Files), "/", vfs.BindReplace)
5170

5271
corpus := godoc.NewCorpus(fs)

cmd/godoc/generate-index.bash

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2011 The Go Authors. All rights reserved.
4+
# Use of this source code is governed by a BSD-style
5+
# license that can be found in the LICENSE file.
6+
7+
# This script creates a .zip file representing the $GOROOT file system
8+
# and computes the corresponding search index files.
9+
#
10+
# These are used in production (see app.prod.yaml)
11+
12+
set -e -u -x
13+
14+
ZIPFILE=godoc.zip
15+
INDEXFILE=godoc.index
16+
SPLITFILES=index.split.
17+
18+
error() {
19+
echo "error: $1"
20+
exit 2
21+
}
22+
23+
install() {
24+
go install
25+
}
26+
27+
getArgs() {
28+
if [ ! -v GOROOT ]; then
29+
GOROOT="$(go env GOROOT)"
30+
echo "GOROOT not set explicitly, using go env value instead"
31+
fi
32+
33+
# safety checks
34+
if [ ! -d "$GOROOT" ]; then
35+
error "$GOROOT is not a directory"
36+
fi
37+
38+
# reporting
39+
echo "GOROOT = $GOROOT"
40+
}
41+
42+
makeZipfile() {
43+
echo "*** make $ZIPFILE"
44+
rm -f $ZIPFILE goroot
45+
ln -s "$GOROOT" goroot
46+
zip -q -r $ZIPFILE goroot/* # glob to ignore dotfiles (like .git)
47+
rm goroot
48+
}
49+
50+
makeIndexfile() {
51+
echo "*** make $INDEXFILE"
52+
godoc=$(go env GOPATH)/bin/godoc
53+
# NOTE: run godoc without GOPATH set. Otherwise third-party packages will end up in the index.
54+
GOPATH= $godoc -write_index -goroot goroot -index_files=$INDEXFILE -zip=$ZIPFILE
55+
}
56+
57+
splitIndexfile() {
58+
echo "*** split $INDEXFILE"
59+
rm -f $SPLITFILES*
60+
split -b8m $INDEXFILE $SPLITFILES
61+
}
62+
63+
cd $(dirname $0)
64+
65+
install
66+
getArgs "$@"
67+
makeZipfile
68+
makeIndexfile
69+
splitIndexfile
70+
rm $INDEXFILE
71+
72+
echo "*** setup complete"

cmd/godoc/setup-godoc-app.bash

-134
This file was deleted.

0 commit comments

Comments
 (0)