From 7a24e5e99852c9e6b29cf015469db7dbf9a62915 Mon Sep 17 00:00:00 2001 From: Barak Michener Date: Fri, 27 Jun 2014 22:34:29 -0400 Subject: [PATCH] Allow assets to come from more places than PWD, do some autodetection, add a build file, and update the README --- .goxc.json | 6 ++++++ README.md | 31 ++++--------------------------- http/cayley_http.go | 47 +++++++++++++++++++++++++++++++++++++++++++---- http/cayley_http_docs.go | 3 ++- 4 files changed, 55 insertions(+), 32 deletions(-) create mode 100644 .goxc.json diff --git a/.goxc.json b/.goxc.json new file mode 100644 index 0000000..bf26818 --- /dev/null +++ b/.goxc.json @@ -0,0 +1,6 @@ +{ + "Arch": "amd64 386", + "Os": "linux darwin windows", + "ResourcesInclude": "README.md,static,templates,LICENSE,AUTHORS,CONTRIBUTORS,docs,cayley.cfg.example,30kmoviedata.nt.gz,testdata.nt", + "ConfigVersion": "0.9" +} diff --git a/README.md b/README.md index b656bee..417206b 100644 --- a/README.md +++ b/README.md @@ -28,36 +28,13 @@ Rough performance testing shows that, on consumer hardware and an average disk, \* Note that while it's not exactly Gremlin, it certainly takes inspiration from that API. For this flavor, [see the documentation](docs/GremlinAPI.md). -## Building -Make sure you have the right packages installed. Mostly, this is just Go as a dependency, and different ways of pulling packages. +## Getting Started -### Linux -**Ubuntu / Debian** +Grab the latest [release binary](http://github.com/google/cayley/releases) and extract it wherever you like. -`sudo apt-get install golang git bzr mercurial make` +If you prefer to build from source, see the documentation on the wiki at [How to start hacking on Cayley](https://github.com/google/cayley/wiki/How-to-start-hacking-on-Cayley) -**RHEL / Fedora** - -`sudo yum install golang git bzr mercurial make gcc` - - -**OS X** - -[Homebrew](http://brew.sh) is the preferred method. - -`brew install bazaar mercurial git go` - -**Clone and build** - -Now you can clone the repository and build the project. - -```bash -go get github.com/google/cayley/cmd/cayley -``` - -And the `cayley` binary will be built and ready. - -Give it a quick test with: +`cd` to the directory and give it a quick test with: ``` ./cayley repl --dbpath=testdata.nt ``` diff --git a/http/cayley_http.go b/http/cayley_http.go index f4ad6fa..f850715 100644 --- a/http/cayley_http.go +++ b/http/cayley_http.go @@ -15,9 +15,11 @@ package http import ( + "flag" "fmt" "html/template" "net/http" + "os" "time" "github.com/barakmich/glog" @@ -29,6 +31,39 @@ import ( type ResponseHandler func(http.ResponseWriter, *http.Request, httprouter.Params) int +var assetsPath = flag.String("assets", "", "Explicit path to the HTTP assets.") +var assetsDirs = []string{"templates", "static", "docs"} + +func hasAssets(path string) bool { + for _, dir := range assetsDirs { + if _, err := os.Stat(fmt.Sprint(path, "/", dir)); os.IsNotExist(err) { + return false + } + } + return true +} + +func findAssetsPath() string { + if *assetsPath != "" { + if hasAssets(*assetsPath) { + return *assetsPath + } else { + glog.Fatalln("Cannot find assets at", *assetsPath, ".") + } + } + + if hasAssets(".") { + return "." + } + + gopathPath := os.ExpandEnv("$GOPATH/src/github.com/google/cayley") + if hasAssets(gopathPath) { + return gopathPath + } + glog.Fatalln("Cannot find assets in any of the default search paths. Please run in the same directory, in a Go workspace, or set --assets .") + return "" +} + func LogRequest(handler ResponseHandler) httprouter.Handle { return func(w http.ResponseWriter, req *http.Request, params httprouter.Params) { start := time.Now() @@ -86,10 +121,14 @@ func (api *Api) ApiV1(r *httprouter.Router) { func SetupRoutes(ts graph.TripleStore, cfg *config.CayleyConfig) { r := httprouter.New() - var templates = template.Must(template.ParseGlob("templates/*.tmpl")) - templates.ParseGlob("templates/*.html") + assets := findAssetsPath() + if glog.V(2) { + glog.V(2).Infoln("Found assets at", assets) + } + var templates = template.Must(template.ParseGlob(fmt.Sprint(assets, "/templates/*.tmpl"))) + templates.ParseGlob(fmt.Sprint(assets, "/templates/*.html")) root := &TemplateRequestHandler{templates: templates} - docs := &DocRequestHandler{} + docs := &DocRequestHandler{assets: assets} api := &Api{config: cfg, ts: ts} api.ApiV1(r) @@ -98,7 +137,7 @@ func SetupRoutes(ts graph.TripleStore, cfg *config.CayleyConfig) { r.GET("/docs/:docpage", docs.ServeHTTP) r.GET("/ui/:ui_type", root.ServeHTTP) r.GET("/", root.ServeHTTP) - http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("static/")))) + http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir(fmt.Sprint(assets, "/static/"))))) http.Handle("/", r) } diff --git a/http/cayley_http_docs.go b/http/cayley_http_docs.go index 45e747d..0d1d234 100644 --- a/http/cayley_http_docs.go +++ b/http/cayley_http_docs.go @@ -25,6 +25,7 @@ import ( ) type DocRequestHandler struct { + assets string } func MarkdownWithCSS(input []byte, title string) []byte { @@ -56,7 +57,7 @@ func (h *DocRequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request, pa if docpage == "" { docpage = "Index" } - file, err := os.Open(fmt.Sprintf("docs/%s.md", docpage)) + file, err := os.Open(fmt.Sprintf("%s/docs/%s.md", h.assets, docpage)) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return