Start ripping up blobserver into libraries, starting with blobserver.Storage

This commit is contained in:
Brad Fitzpatrick 2011-02-03 15:45:35 -08:00
parent 4df8d2ba04
commit 850623f932
8 changed files with 61 additions and 31 deletions

View File

@ -162,6 +162,7 @@ __DATA__
./server/go/blobserver/Makefile
- server/go/httputil
- lib/go/blobref
- lib/go/blobserver
- server/go/auth
- server/go/webserver
./server/go/sigserver/Makefile
@ -207,5 +208,5 @@ __DATA__
- lib/go/ext/openpgp/error
./lib/go/blobref/Makefile
# (no deps)
./lib/go/blobserver/Makefile
- lib/go/blobref

View File

@ -0,0 +1,9 @@
include $(GOROOT)/src/Make.inc
PREREQ=$(QUOTED_GOROOT)/pkg/$(GOOS)_$(GOARCH)/camli/blobref.a
TARG=camli/blobserver
GOFILES=\
interface.go\
include $(GOROOT)/src/Make.pkg

View File

@ -0,0 +1,31 @@
/*
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 blobserver
import (
"camli/blobref"
"os"
)
type Storage interface {
blobref.Fetcher
// Remove 0 or more blobs from provided partition, which should be empty
// for the default partition. Removal of non-existent items isn't an error.
// Returns failure if any items existed but failed to be deleted.
Remove(partition string, blobs []*blobref.BlobRef) os.Error
}

View File

@ -1,11 +1,12 @@
include $(GOROOT)/src/Make.inc
PREREQ=$(QUOTED_GOROOT)/pkg/$(GOOS)_$(GOARCH)/camli/blobserver.a
TARG=camlistored
GOFILES=\
camlistored.go\
enumerate.go\
get.go\
interface.go\
localdisk.go\
partitions.go\
preupload.go\

View File

@ -8,6 +8,7 @@ import (
"camli/auth"
"camli/httputil"
"camli/webserver"
"camli/blobserver"
"flag"
"fmt"
"http"
@ -19,7 +20,7 @@ import (
var flagStorageRoot *string = flag.String("root", "/tmp/camliroot", "Root directory to store files")
var flagRequestLog *bool = flag.Bool("reqlog", false, "Log incoming requests")
var blobServer BlobServer
var storage blobserver.Storage
const camliPrefix = "/camli/"
const partitionPrefix = "/partition-"
@ -77,9 +78,9 @@ func handleCamli(conn http.ResponseWriter, req *http.Request) {
case "GET":
switch action {
case "enumerate-blobs":
handler = auth.RequireAuth(createEnumerateHandler(blobServer, partition))
handler = auth.RequireAuth(createEnumerateHandler(storage, partition))
default:
handler = createGetHandler(blobServer)
handler = createGetHandler(storage)
}
case "POST":
switch action {
@ -89,7 +90,7 @@ func handleCamli(conn http.ResponseWriter, req *http.Request) {
handler = auth.RequireAuth(handleMultiPartUpload)
case "remove":
// Currently only allows removing from a non-main partition.
handler = auth.RequireAuth(createRemoveHandler(blobServer, partition))
handler = auth.RequireAuth(createRemoveHandler(storage, partition))
// Not part of the spec:
case "testform": // debug only
@ -127,7 +128,7 @@ func main() {
}
}
blobServer = newDiskStorage(*flagStorageRoot)
storage = newDiskStorage(*flagStorageRoot)
ws := webserver.New()
ws.RegisterPreMux(webserver.HandlerPicker(pickPartitionHandlerMaybe))

View File

@ -18,6 +18,7 @@ package main
import (
"camli/blobref"
"camli/blobserver"
"fmt"
"http"
"os"
@ -118,14 +119,14 @@ func readBlobs(opts readBlobRequest) {
}
}
func createEnumerateHandler(server BlobServer, partition string) func(http.ResponseWriter, *http.Request) {
func createEnumerateHandler(storage blobserver.Storage, partition string) func(http.ResponseWriter, *http.Request) {
return func(conn http.ResponseWriter, req *http.Request) {
handleEnumerateBlobs(conn, req, server, partition)
handleEnumerateBlobs(conn, req, storage, partition)
}
}
// TODO: use the provided server argument
func handleEnumerateBlobs(conn http.ResponseWriter, req *http.Request, server BlobServer, partition string) {
// TODO: use the provided storage argument
func handleEnumerateBlobs(conn http.ResponseWriter, req *http.Request, storage blobserver.Storage, partition string) {
limit, err := strconv.Atoui(req.FormValue("limit"))
if err != nil || limit > maxEnumerate {
limit = maxEnumerate

View File

@ -1,15 +0,0 @@
package main
import (
"camli/blobref"
"os"
)
type BlobServer interface {
blobref.Fetcher
// Remove 0 or more blobs from provided partition, which should be empty
// for the default partition. Removal of non-existent items isn't an error.
// Returns failure if any items existed but failed to be deleted.
Remove(partition string, blobs []*blobref.BlobRef) os.Error
}

View File

@ -18,6 +18,7 @@ package main
import (
"camli/blobref"
"camli/blobserver"
"camli/httputil"
"fmt"
"http"
@ -26,13 +27,13 @@ import (
const maxRemovesPerRequest = 1000
func createRemoveHandler(server BlobServer, partition string) func(http.ResponseWriter, *http.Request) {
func createRemoveHandler(storage blobserver.Storage, partition string) func(http.ResponseWriter, *http.Request) {
return func(conn http.ResponseWriter, req *http.Request) {
handleRemove(conn, req, server, partition)
handleRemove(conn, req, storage, partition)
}
}
func handleRemove(conn http.ResponseWriter, req *http.Request, server BlobServer, partition string) {
func handleRemove(conn http.ResponseWriter, req *http.Request, storage blobserver.Storage, partition string) {
if req.Method != "POST" {
log.Fatalf("Invalid method; handlers misconfigured")
}
@ -67,7 +68,7 @@ func handleRemove(conn http.ResponseWriter, req *http.Request, server BlobServer
toRemoveStr = append(toRemoveStr, ref.String())
}
err := server.Remove(partition, toRemove)
err := storage.Remove(partition, toRemove)
if err != nil {
conn.WriteHeader(http.StatusInternalServerError)
log.Printf("Server error during remove: %v", err)