From 718794205fc40ebeaec080481ac751b3de994c9f Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Sun, 18 Jan 2015 16:54:45 -0800 Subject: [PATCH 1/2] wip --- pkg/server/server.go | 8 ++++++++ pkg/storage/storage.go | 25 +++++++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/pkg/server/server.go b/pkg/server/server.go index 8ba95b206..59bcb60ab 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -61,6 +61,7 @@ func createSelectCases(channels []<-chan error) []reflect.SelectCase { func getHttpHandler() http.Handler { mux := mux.NewRouter() mux.HandleFunc("/{bucket}/{object:.*}", getObjectHandler).Methods("GET") + mux.HandleFunc("/{bucket}/{object:.*}", putObjectHandler).Methods("PUT") return mux } @@ -70,3 +71,10 @@ func getObjectHandler(w http.ResponseWriter, req *http.Request) { object := vars["object"] storage.CopyObjectToWriter(w, bucket, object) } + +func putObjectHandler(w http.ResponseWriter, req *http.Request) { + vars := mux.Vars(req) + bucket := vars["bucket"] + object := vars["object"] + storage.StoreObject(bucket, object, req.Body) +} diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index d59ca6edc..32ce7459c 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -6,16 +6,29 @@ import ( "io" ) -type Storage struct{} +type Storage struct { + data map[string][]byte +} func (storage *Storage) CopyObjectToWriter(w io.Writer, bucket string, object string) error { // TODO synchronize access // get object - objectData := "OBJECT: " + bucket + " - " + object - objectBuffer := bytes.NewBufferString(objectData) - // copy object to writer - _, err := io.Copy(w, objectBuffer) - return err + key := bucket + ":" + object + if val, ok := storage.data[key]; ok { + objectBuffer := bytes.NewBuffer(val) + _, err := io.Copy(w, objectBuffer) + return err + } else { + return errors.New("Not Found") + } +} + +func (storage *Storage) StoreObject(bucket string, object string, data io.Reader) { + key := bucket + ":" + object + var bytesBuffer bytes.Buffer + if _, ok := io.Copy(&bytesBuffer, data); ok == nil { + storage.data[key] = bytesBuffer.Bytes() + } } func Start() (chan<- string, <-chan error, *Storage) { From c791c7bca4549d9a96bddd4a363a1363c9542e87 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Mon, 19 Jan 2015 10:30:40 -0800 Subject: [PATCH 2/2] Store objects in memory map --- pkg/storage/storage.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index 32ce7459c..3052bda41 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -35,7 +35,9 @@ func Start() (chan<- string, <-chan error, *Storage) { ctrlChannel := make(chan string) errorChannel := make(chan error) go start(ctrlChannel, errorChannel) - return ctrlChannel, errorChannel, &Storage{} + return ctrlChannel, errorChannel, &Storage{ + data: make(map[string][]byte), + } } func start(ctrlChannel <-chan string, errorChannel chan<- error) {