website: start of showing pkg and cmd doc (camlistore.org/issue/108)

Change-Id: I8bb9140ce787844392e93681a1118160ee6e4579
This commit is contained in:
Brad Fitzpatrick 2013-02-22 15:58:56 -08:00
parent d3aa327ac6
commit 7af0b73f8a
3 changed files with 83 additions and 1 deletions

View File

@ -285,6 +285,8 @@ 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("/pkg/", godocHandler{})
mux.Handle("/cmd/", godocHandler{})
gerritUrl, _ := url.Parse(fmt.Sprintf("http://%s:8000/", *gerritHost))
var gerritHandler http.Handler = httputil.NewSingleHostReverseProxy(gerritUrl)

80
website/godoc.go Normal file
View File

@ -0,0 +1,80 @@
/*
Copyright 2013 The Camlistore Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"go/build"
"go/doc"
"go/parser"
"go/token"
"log"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
)
var docRx = regexp.MustCompile(`^/((?:pkg|cmd)/([\w/]+?))/?$`)
type godocHandler struct{}
func (godocHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m := docRx.FindStringSubmatch(r.URL.Path)
if m == nil {
http.NotFound(w, r)
return
}
suffix := m[1]
pkgName := "camlistore.org/" + suffix
diskPath := filepath.Join(*root, "..", suffix)
bpkg, err := build.ImportDir(diskPath, 0)
if err != nil {
log.Print(err)
return
}
inSet := make(map[string]bool)
for _, name := range bpkg.GoFiles {
inSet[filepath.Base(name)] = true
}
fset := token.NewFileSet()
filter := func(fi os.FileInfo) bool {
return inSet[fi.Name()]
}
aPkgMap, err := parser.ParseDir(fset, diskPath, filter, 0)
if err != nil {
log.Print(err)
return
}
aPkg := aPkgMap[path.Base(suffix)]
if aPkg == nil {
for _, v := range aPkgMap {
aPkg = v
break
}
if aPkg == nil {
log.Printf("no apkg found?")
http.NotFound(w, r)
return
}
}
docpkg := doc.New(aPkg, pkgName, 0)
fmt.Fprintf(w, "%#v", docpkg)
}

View File

@ -17,7 +17,7 @@ print STDERR "Running camweb in $Bin on port 8080\n";
my $in_prod = -e "$HOME/etc/ssl.key"; # heuristic. good enough.
my @args;
push @args, "go", "run", "camweb.go", "logging.go";
push @args, "go", "run", "camweb.go", "logging.go", "godoc.go";
push @args, "--http=:8080";
push @args, "--root=$Bin";
push @args, "--logdir=$logdir";