Merge pull request #19 from fkautz/pr_out_store_objects_in_memory_map
This commit is contained in:
commit
7b91f2405c
2 changed files with 30 additions and 7 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -6,23 +6,38 @@ 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) {
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue