From 0d7581ede6c89f3c87f05b5a5edfa0aa4a18643a Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 16 Jun 2011 20:45:47 -0700 Subject: [PATCH] Start of publish handler. Change-Id: I229c9e00a19aa985050373beda696c5468a94105 --- config/dev-server-config.json | 10 ++++ lib/go/camli/search/handler.go | 16 +++---- server/go/camlistored/publish.go | 79 ++++++++++++++++++++++++++++++++ server/go/camlistored/ui.go | 2 +- 4 files changed, 98 insertions(+), 9 deletions(-) create mode 100644 server/go/camlistored/publish.go diff --git a/config/dev-server-config.json b/config/dev-server-config.json index 21990f137..bc4407871 100644 --- a/config/dev-server-config.json +++ b/config/dev-server-config.json @@ -8,6 +8,16 @@ "stealth": false } }, + + "/www/": { + "handler": "publish", + "handlerArgs": { + "blobRoot": "/bs/", + "searchRoot": "/my-search/", + "cache": "/cache/" + } + }, + "/ui/": { "handler": "ui", "handlerArgs": { diff --git a/lib/go/camli/search/handler.go b/lib/go/camli/search/handler.go index c79ebce10..fce238270 100644 --- a/lib/go/camli/search/handler.go +++ b/lib/go/camli/search/handler.go @@ -36,7 +36,7 @@ func init() { blobserver.RegisterHandlerConstructor("search", newHandlerFromConfig) } -type searchHandler struct { +type Handler struct { index Index owner *blobref.BlobRef } @@ -61,7 +61,7 @@ func newHandlerFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (http.Handl return nil, fmt.Errorf("search 'owner' has malformed blobref %q; expecting e.g. sha1-xxxxxxxxxxxx", ownerBlobStr) } - return &searchHandler{ + return &Handler{ index: indexer, owner: ownerBlobRef, }, nil @@ -76,7 +76,7 @@ func jsonMapList() []map[string]interface{} { return make([]map[string]interface{}, 0) } -func (sh *searchHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { +func (sh *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { _ = req.Header.Get("X-PrefixHandler-PathBase") suffix := req.Header.Get("X-PrefixHandler-PathSuffix") @@ -104,7 +104,7 @@ func (sh *searchHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { httputil.ReturnJson(rw, ret) } -func (sh *searchHandler) serveRecentPermanodes(rw http.ResponseWriter, req *http.Request) { +func (sh *Handler) serveRecentPermanodes(rw http.ResponseWriter, req *http.Request) { ret := jsonMap() defer httputil.ReturnJson(rw, ret) @@ -138,7 +138,7 @@ func (sh *searchHandler) serveRecentPermanodes(rw http.ResponseWriter, req *http ret["recent"] = recent } -func (sh *searchHandler) serveClaims(rw http.ResponseWriter, req *http.Request) { +func (sh *Handler) serveClaims(rw http.ResponseWriter, req *http.Request) { ret := jsonMap() pn := blobref.Parse(req.FormValue("permanode")) @@ -178,7 +178,7 @@ func (sh *searchHandler) serveClaims(rw http.ResponseWriter, req *http.Request) } type describeRequest struct { - sh *searchHandler + sh *Handler lk sync.Mutex // protects m m map[string]interface{} // top-level response JSON @@ -246,7 +246,7 @@ func (dr *describeRequest) describeReally(br *blobref.BlobRef, depth int) { } } -func (sh *searchHandler) serveDescribe(rw http.ResponseWriter, req *http.Request) { +func (sh *Handler) serveDescribe(rw http.ResponseWriter, req *http.Request) { ret := jsonMap() defer httputil.ReturnJson(rw, ret) @@ -262,7 +262,7 @@ func (sh *searchHandler) serveDescribe(rw http.ResponseWriter, req *http.Request dr.wg.Wait() } -func (sh *searchHandler) serveFiles(rw http.ResponseWriter, req *http.Request) { +func (sh *Handler) serveFiles(rw http.ResponseWriter, req *http.Request) { ret := jsonMap() defer httputil.ReturnJson(rw, ret) diff --git a/server/go/camlistored/publish.go b/server/go/camlistored/publish.go new file mode 100644 index 000000000..1002c0d7f --- /dev/null +++ b/server/go/camlistored/publish.go @@ -0,0 +1,79 @@ +/* +Copyright 2011 Google Inc. + +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" + "html" + "http" + "os" + + "camli/blobserver" + "camli/jsonconfig" + "camli/search" +) + +// PublishHandler publishes your info to the world, if permanodes have +// appropriate ACLs set. (everything is private by default) +type PublishHandler struct { + Search *search.Handler + Storage blobserver.Storage // of blobRoot + Cache blobserver.Storage // or nil +} + +func init() { + blobserver.RegisterHandlerConstructor("publish", newPublishFromConfig) +} + +func newPublishFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (h http.Handler, err os.Error) { + pub := &PublishHandler{} + blobRoot := conf.RequiredString("blobRoot") + searchRoot := conf.RequiredString("searchRoot") + cachePrefix := conf.OptionalString("cache", "") + if err = conf.Validate(); err != nil { + return + } + + bs, err := ld.GetStorage(blobRoot) + if err != nil { + return nil, fmt.Errorf("publish handler's blobRoot of %q error: %v", blobRoot, err) + } + pub.Storage = bs + + si, err := ld.GetHandler(searchRoot) + if err != nil { + return nil, fmt.Errorf("publish handler's searchRoot of %q error: %v", searchRoot, err) + } + pub.Search = si.(*search.Handler) + + if cachePrefix != "" { + bs, err := ld.GetStorage(cachePrefix) + if err != nil { + return nil, fmt.Errorf("publish handler's cache of %q error: %v", cachePrefix, err) + } + pub.Cache = bs + } + + return pub, nil +} + +func (pub *PublishHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { + base := req.Header.Get("X-PrefixHandler-PathBase") + suffix := req.Header.Get("X-PrefixHandler-PathSuffix") + + fmt.Fprintf(rw, "I am publish handler at base %q, suffix %q", base, html.EscapeString(suffix)) +} diff --git a/server/go/camlistored/ui.go b/server/go/camlistored/ui.go index cfafbf954..9ff7f9462 100644 --- a/server/go/camlistored/ui.go +++ b/server/go/camlistored/ui.go @@ -117,7 +117,7 @@ func newUiFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (h http.Handler, if cachePrefix != "" { bs, err := ld.GetStorage(cachePrefix) if err != nil { - return nil, fmt.Errorf("UI handler's cache of %q error: %v", ui.BlobRoot, err) + return nil, fmt.Errorf("UI handler's cache of %q error: %v", cachePrefix, err) } ui.Cache = bs }