From cd32b0b5b99f4fe89352b66e6c2da15d0310fdde Mon Sep 17 00:00:00 2001 From: mpl Date: Wed, 12 Dec 2012 18:04:05 +0100 Subject: [PATCH] move findGoPathPackage to osutil, and use it to define closureDir Change-Id: I838a2e665591d13ecf3bb2689df1931d7e14465b --- pkg/index/indextest/tests.go | 45 +++++++++++++----------------------- pkg/osutil/paths.go | 37 +++++++++++++++++++++++++++++ pkg/server/ui.go | 33 +++++++++++++++++++++----- 3 files changed, 80 insertions(+), 35 deletions(-) diff --git a/pkg/index/indextest/tests.go b/pkg/index/indextest/tests.go index 6f3fd0158..97edda88e 100644 --- a/pkg/index/indextest/tests.go +++ b/pkg/index/indextest/tests.go @@ -23,14 +23,13 @@ import ( "os" "path/filepath" "reflect" - "runtime" - "strings" "testing" "time" "camlistore.org/pkg/blobref" "camlistore.org/pkg/index" "camlistore.org/pkg/jsonsign" + "camlistore.org/pkg/osutil" "camlistore.org/pkg/schema" "camlistore.org/pkg/search" "camlistore.org/pkg/test" @@ -173,34 +172,14 @@ func (id *IndexDeps) UploadFile(fileName string, contents string) (fileRef, whol return } -func osSplitChar() string { - switch runtime.GOOS { - case "windows": - return ";" - case "plan9": - panic("unsupported") - } - return ":" -} - -func findGoPathPackage(pkg string) string { - gp := os.Getenv("GOPATH") - if gp == "" { - panic("no GOPATH set") - } - for _, p := range strings.Split(gp, osSplitChar()) { - dir := filepath.Join(p, "src", pkg) - if fi, err := os.Stat(dir); err == nil && fi.IsDir() { - return dir - } - } - panic(fmt.Sprintf("package %q not found in GOPATH(s) of %q", pkg, gp)) -} - // NewIndexDeps returns an IndexDeps helper for populating and working // with the provided index for tests. func NewIndexDeps(index *index.Index) *IndexDeps { - secretRingFile := filepath.Join(findGoPathPackage("camlistore.org"), "pkg", "jsonsign", "testdata", "test-secring.gpg") + camliRootPath, err := osutil.GoPackagePath("camlistore.org") + if err != nil { + log.Fatal("Package camlistore.org no found in $GOPATH or $GOPATH not defined") + } + secretRingFile := filepath.Join(camliRootPath, "pkg", "jsonsign", "testdata", "test-secring.gpg") pubKey := &test.Blob{Contents: `-----BEGIN PGP PUBLIC KEY BLOCK----- xsBNBEzgoVsBCAC/56aEJ9BNIGV9FVP+WzenTAkg12k86YqlwJVAB/VwdMlyXxvi @@ -256,9 +235,13 @@ func Index(t *testing.T, initIdx func() *index.Index) { // TODO(bradfitz): add EXIF tests here, once that stuff is ready. if false { + camliRootPath, err := osutil.GoPackagePath("camlistore.org") + if err != nil { + t.Fatal("Package camlistore.org no found in $GOPATH or $GOPATH not defined") + } for i := 1; i <= 8; i++ { fileBase := fmt.Sprintf("f%d-exif.jpg", i) - fileName := filepath.Join(findGoPathPackage("camlistore.org"), "pkg", "images", "testdata", fileBase) + fileName := filepath.Join(camliRootPath, "pkg", "images", "testdata", fileBase) contents, err := ioutil.ReadFile(fileName) if err != nil { t.Fatal(err) @@ -270,7 +253,11 @@ func Index(t *testing.T, initIdx func() *index.Index) { // Upload a basic image. var jpegFileRef *blobref.BlobRef { - fileName := filepath.Join(findGoPathPackage("camlistore.org"), "pkg", "index", "indextest", "testdata", "dude.jpg") + camliRootPath, err := osutil.GoPackagePath("camlistore.org") + if err != nil { + t.Fatal("Package camlistore.org no found in $GOPATH or $GOPATH not defined") + } + fileName := filepath.Join(camliRootPath, "pkg", "index", "indextest", "testdata", "dude.jpg") contents, err := ioutil.ReadFile(fileName) if err != nil { t.Fatal(err) diff --git a/pkg/osutil/paths.go b/pkg/osutil/paths.go index bcc492c3a..f2c73ebaf 100644 --- a/pkg/osutil/paths.go +++ b/pkg/osutil/paths.go @@ -113,3 +113,40 @@ func FindCamliInclude(configFile string) (absPath string, err error) { return "", os.ErrNotExist } + +func envVarSplitChar() string { + switch runtime.GOOS { + case "windows": + return ";" + case "plan9": + panic("unsupported") + } + return ":" +} + +// GoPackagePath returns the path to the provided Go package's +// source directory. +// pkg may be a path prefix without any *.go files. +// The error is os.ErrNotExist if GOPATH is unset or the directory +// doesn't exist in any GOPATH component. +func GoPackagePath(pkg string) (path string, err error) { + gp := os.Getenv("GOPATH") + if gp == "" { + return path, os.ErrNotExist + } + for _, p := range strings.Split(gp, envVarSplitChar()) { + dir := filepath.Join(p, "src", pkg) + fi, err := os.Stat(dir) + if os.IsNotExist(err) { + continue + } + if err != nil { + return "", err + } + if !fi.IsDir() { + continue + } + return dir, nil + } + return path, os.ErrNotExist +} diff --git a/pkg/server/ui.go b/pkg/server/ui.go index 6062cd7b4..a70f14042 100644 --- a/pkg/server/ui.go +++ b/pkg/server/ui.go @@ -32,6 +32,7 @@ import ( "camlistore.org/pkg/blobserver" "camlistore.org/pkg/httputil" "camlistore.org/pkg/jsonconfig" + "camlistore.org/pkg/osutil" newuistatic "camlistore.org/server/camlistored/newui" uistatic "camlistore.org/server/camlistored/ui" ) @@ -74,8 +75,8 @@ type UIHandler struct { Cache blobserver.Storage // or nil sc ScaledImage // cache for scaled images, optional - newUIStaticHandler http.Handler // for the new ui - closureHandler http.Handler + newUIStaticHandler handlerOrNil // for the new ui + closureHandler handlerOrNil } func init() { @@ -140,10 +141,15 @@ func newUIFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (h http.Handler, } } - ui.newUIStaticHandler = http.FileServer(newuiFiles) - // TODO(mpl): figure out camliroot and use an abs path - closureDir := filepath.Join("tmp", "closure") - ui.closureHandler = http.FileServer(http.Dir(closureDir)) + camliRootPath, err := osutil.GoPackagePath("camlistore.org") + if err != nil { + log.Printf("Package camlistore.org not found in $GOPATH (or $GOPATH not defined)." + + " Closure will not be used.") + } else { + closureDir := filepath.Join(camliRootPath, "tmp", "closure") + ui.closureHandler = handlerOrNil{http.FileServer(http.Dir(closureDir))} + ui.newUIStaticHandler = handlerOrNil{http.FileServer(newuiFiles)} + } rootPrefix, _, err := ld.FindHandlerByType("root") if err != nil { @@ -193,6 +199,21 @@ func wantsFileTreePage(req *http.Request) bool { return req.Method == "GET" && blobref.Parse(req.FormValue("d")) != nil } +// TODO(mpl): I don't like the name. +type handlerOrNil struct { + h http.Handler +} + +func (hon handlerOrNil) ServeHTTP(rw http.ResponseWriter, req *http.Request) { + if hon.h == nil { + suffix := req.Header.Get("X-PrefixHandler-PathSuffix") + log.Printf("%v not served: handler is nil", suffix) + http.NotFound(rw, req) + return + } + hon.h.ServeHTTP(rw, req) +} + func (ui *UIHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { base := req.Header.Get("X-PrefixHandler-PathBase") suffix := req.Header.Get("X-PrefixHandler-PathSuffix")