Skip to content

Commit

Permalink
website: allow serving godoc for any valid package
Browse files Browse the repository at this point in the history
Fixes #1010

Change-Id: I15575227eb881a0c83ab2e60af0d344fedaf1850
  • Loading branch information
willnorris committed Jan 6, 2018
1 parent 101ce48 commit 9787462
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
10 changes: 7 additions & 3 deletions website/camweb.go
Expand Up @@ -316,6 +316,13 @@ func mainHandler(rw http.ResponseWriter, req *http.Request) {
return
}

// try to serve godoc if requested path exists
if req.URL.Path != "/" {
if err := serveGodoc(rw, req); err == nil {
return
}
}

findAndServeFile(rw, req, filepath.Join(*root, "content"))
}

Expand Down Expand Up @@ -871,9 +878,6 @@ func main() {
mux.Handle("/robots.txt", http.FileServer(http.Dir(filepath.Join(*root, "static"))))
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(filepath.Join(*root, "static")))))
mux.Handle("/talks/", http.StripPrefix("/talks/", http.FileServer(http.Dir(filepath.Join(*root, "talks")))))
mux.Handle(pkgPattern, godocHandler{})
mux.Handle(cmdPattern, godocHandler{})
mux.Handle(appPattern, godocHandler{})
mux.HandleFunc(errPattern, errHandler)

// Google Webmaster Tools ownership proof:
Expand Down
43 changes: 16 additions & 27 deletions website/godoc.go
Expand Up @@ -42,9 +42,7 @@ import (
)

const (
pkgPattern = "/pkg/"
cmdPattern = "/cmd/"
appPattern = "/app/"
fileembedPattern = "fileembed.go"
)

Expand Down Expand Up @@ -244,13 +242,6 @@ func (pi *PageInfo) populateDirs(diskPath string, depth int) {
}

func getPageInfo(pkgName, diskPath string) (pi PageInfo, err error) {
if pkgName == pathpkg.Join(domainName, pkgPattern) ||
pkgName == pathpkg.Join(domainName, cmdPattern) ||
pkgName == pathpkg.Join(domainName, appPattern) {
pi.Dirname = diskPath
pi.populateDirs(diskPath, -1)
return
}
bpkg, err := build.ImportDir(diskPath, 0)
if err != nil {
if _, ok := err.(*build.NoGoError); ok {
Expand Down Expand Up @@ -289,7 +280,7 @@ func getPageInfo(pkgName, diskPath string) (pi PageInfo, err error) {

pi.Dirname = diskPath
pi.PDoc = doc.New(aPkg, pkgName, 0)
pi.IsPkg = strings.Contains(pkgName, domainName+pkgPattern)
pi.IsPkg = pi.PDoc.Name != "main"

// get directory information
pi.populateDirs(diskPath, -1)
Expand Down Expand Up @@ -407,33 +398,30 @@ func serveTextFile(w http.ResponseWriter, r *http.Request, abspath, relpath, tit
})
}

type godocHandler struct{}
func serveGodoc(w http.ResponseWriter, r *http.Request) error {
suffix := r.URL.Path
diskPath := filepath.Join(*root, "..", suffix)

func (godocHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m := docRx.FindStringSubmatch(r.URL.Path)
suffix := ""
if m == nil {
if r.URL.Path != pkgPattern && r.URL.Path != cmdPattern && r.URL.Path != appPattern {
http.NotFound(w, r)
return
}
suffix = r.URL.Path
} else {
suffix = m[1]
fi, err := os.Stat(diskPath)
if err != nil {
return err
}
diskPath := filepath.Join(*root, "..", suffix)

switch pathpkg.Ext(suffix) {
case ".go":
switch {
case isGoFile(fi):
serveTextFile(w, r, diskPath, suffix, "Source file")
return
return nil
case isPkgDir(fi):
break
default:
return os.ErrInvalid
}

pkgName := pathpkg.Join(domainName, suffix)
pi, err := getPageInfo(pkgName, diskPath)
if err != nil {
log.Print(err)
return
return err
}

subtitle := pathpkg.Base(diskPath)
Expand All @@ -443,4 +431,5 @@ func (godocHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
subtitle: subtitle,
content: applyTextTemplate(packageHTML, "packageHTML", pi),
})
return nil
}

0 comments on commit 9787462

Please sign in to comment.