Skip to content
This repository was archived by the owner on Aug 24, 2022. It is now read-only.

Commit de117c8

Browse files
committed
import standard godoc
0 parents  commit de117c8

File tree

13 files changed

+1986
-0
lines changed

13 files changed

+1986
-0
lines changed

README.godoc-app

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
godoc on appengine
2+
------------------
3+
4+
Prerequisites
5+
-------------
6+
7+
* Go appengine SDK
8+
https://developers.google.com/appengine/downloads#Google_App_Engine_SDK_for_Go
9+
10+
* Go sources at tip under $GOROOT
11+
12+
* Godoc sources at tip inside $GOPATH
13+
(go get -d golang.org/x/tools/cmd/godoc)
14+
15+
16+
Directory structure
17+
-------------------
18+
19+
* Let $APPDIR be the directory containing the app engine files.
20+
(e.g., $APPDIR=$HOME/godoc-app)
21+
22+
* $APPDIR contains the following entries (this may change depending on
23+
app-engine release and version of godoc):
24+
25+
app.yaml
26+
golang.org/x/tools/cmd/godoc
27+
godoc.zip
28+
index.split.*
29+
30+
* The app.yaml file is set up per app engine documentation.
31+
For instance:
32+
33+
application: godoc-app
34+
version: 1
35+
runtime: go
36+
api_version: go1
37+
38+
handlers:
39+
- url: /.*
40+
script: _go_app
41+
42+
43+
Configuring and running godoc
44+
-----------------------------
45+
46+
To configure godoc, run
47+
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 .

appinit.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2011 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build appengine
6+
7+
package main
8+
9+
// This file replaces main.go when running godoc under app-engine.
10+
// See README.godoc-app for details.
11+
12+
import (
13+
"archive/zip"
14+
"log"
15+
"path"
16+
"regexp"
17+
18+
"golang.org/x/tools/godoc"
19+
"golang.org/x/tools/godoc/static"
20+
"golang.org/x/tools/godoc/vfs"
21+
"golang.org/x/tools/godoc/vfs/mapfs"
22+
"golang.org/x/tools/godoc/vfs/zipfs"
23+
)
24+
25+
func init() {
26+
playEnabled = true
27+
28+
log.Println("initializing godoc ...")
29+
log.Printf(".zip file = %s", zipFilename)
30+
log.Printf(".zip GOROOT = %s", zipGoroot)
31+
log.Printf("index files = %s", indexFilenames)
32+
33+
goroot := path.Join("/", zipGoroot) // fsHttp paths are relative to '/'
34+
35+
// read .zip file and set up file systems
36+
const zipfile = zipFilename
37+
rc, err := zip.OpenReader(zipfile)
38+
if err != nil {
39+
log.Fatalf("%s: %s\n", zipfile, err)
40+
}
41+
// rc is never closed (app running forever)
42+
fs.Bind("/", zipfs.New(rc, zipFilename), goroot, vfs.BindReplace)
43+
fs.Bind("/lib/godoc", mapfs.New(static.Files), "/", vfs.BindReplace)
44+
45+
corpus := godoc.NewCorpus(fs)
46+
corpus.Verbose = false
47+
corpus.MaxResults = 10000 // matches flag default in main.go
48+
corpus.IndexEnabled = true
49+
corpus.IndexFiles = indexFilenames
50+
if err := corpus.Init(); err != nil {
51+
log.Fatal(err)
52+
}
53+
go corpus.RunIndexer()
54+
55+
pres = godoc.NewPresentation(corpus)
56+
pres.TabWidth = 8
57+
pres.ShowPlayground = true
58+
pres.ShowExamples = true
59+
pres.DeclLinks = true
60+
pres.NotesRx = regexp.MustCompile("BUG")
61+
62+
readTemplates(pres, true)
63+
registerHandlers(pres)
64+
65+
log.Println("godoc initialization complete")
66+
}

blog.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2013 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"fmt"
9+
"go/build"
10+
"log"
11+
"net/http"
12+
"os"
13+
"path/filepath"
14+
"runtime"
15+
"strings"
16+
"sync"
17+
18+
"golang.org/x/tools/blog"
19+
"golang.org/x/tools/godoc/redirect"
20+
)
21+
22+
const (
23+
blogRepo = "golang.org/x/blog"
24+
blogURL = "http://blog.golang.org/"
25+
blogPath = "/blog/"
26+
)
27+
28+
var (
29+
blogServer http.Handler // set by blogInit
30+
blogInitOnce sync.Once
31+
playEnabled bool
32+
)
33+
34+
func init() {
35+
// Initialize blog only when first accessed.
36+
http.HandleFunc(blogPath, func(w http.ResponseWriter, r *http.Request) {
37+
blogInitOnce.Do(blogInit)
38+
blogServer.ServeHTTP(w, r)
39+
})
40+
}
41+
42+
func blogInit() {
43+
// Binary distributions will include the blog content in "/blog".
44+
root := filepath.Join(runtime.GOROOT(), "blog")
45+
46+
// Prefer content from go.blog repository if present.
47+
if pkg, err := build.Import(blogRepo, "", build.FindOnly); err == nil {
48+
root = pkg.Dir
49+
}
50+
51+
// If content is not available fall back to redirect.
52+
if fi, err := os.Stat(root); err != nil || !fi.IsDir() {
53+
fmt.Fprintf(os.Stderr, "Blog content not available locally. "+
54+
"To install, run \n\tgo get %v\n", blogRepo)
55+
blogServer = http.HandlerFunc(blogRedirectHandler)
56+
return
57+
}
58+
59+
s, err := blog.NewServer(blog.Config{
60+
BaseURL: blogPath,
61+
BasePath: strings.TrimSuffix(blogPath, "/"),
62+
ContentPath: filepath.Join(root, "content"),
63+
TemplatePath: filepath.Join(root, "template"),
64+
HomeArticles: 5,
65+
PlayEnabled: playEnabled,
66+
})
67+
if err != nil {
68+
log.Fatal(err)
69+
}
70+
blogServer = s
71+
}
72+
73+
func blogRedirectHandler(w http.ResponseWriter, r *http.Request) {
74+
if r.URL.Path == blogPath {
75+
http.Redirect(w, r, blogURL, http.StatusFound)
76+
return
77+
}
78+
blogPrefixHandler.ServeHTTP(w, r)
79+
}
80+
81+
var blogPrefixHandler = redirect.PrefixHandler(blogPath, blogURL)

0 commit comments

Comments
 (0)