From 7af0b73f8a9e6db20145d2b4224bc9f0e90b8be2 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 22 Feb 2013 15:58:56 -0800 Subject: [PATCH] website: start of showing pkg and cmd doc (camlistore.org/issue/108) Change-Id: I8bb9140ce787844392e93681a1118160ee6e4579 --- website/camweb.go | 2 ++ website/godoc.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++ website/run.pl | 2 +- 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 website/godoc.go diff --git a/website/camweb.go b/website/camweb.go index 1715ebea9..2727e92e6 100644 --- a/website/camweb.go +++ b/website/camweb.go @@ -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) diff --git a/website/godoc.go b/website/godoc.go new file mode 100644 index 000000000..8038353a0 --- /dev/null +++ b/website/godoc.go @@ -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) +} diff --git a/website/run.pl b/website/run.pl index f024213f7..22673f35c 100755 --- a/website/run.pl +++ b/website/run.pl @@ -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";