Adding GET and PUT methods for gateway

master
Frederick F. Kautz IV 10 years ago
parent 58859a0ecc
commit 1bf6fe1f68
  1. 58
      gateway.go
  2. 4
      gateway_test.go

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"io/ioutil"
"net/http" "net/http"
) )
@ -25,21 +26,63 @@ type Bucket interface {
Put(Context, string, []byte) error Put(Context, string, []byte) error
} }
type fakeContext struct{}
func GatewayHandler(w http.ResponseWriter, req *http.Request) { func GatewayHandler(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Gateway") fmt.Fprintf(w, "Gateway")
} }
func GatewayGetObjectHandler(w http.ResponseWriter, req *http.Request) { type GatewayGetHandler struct {
requestBucketChan chan BucketRequest
}
func (handler GatewayGetHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req) vars := mux.Vars(req)
bucket := vars["bucket"] bucketName := vars["bucket"]
object := vars["object"] path := vars["path"]
fmt.Fprintf(w, "bucket: "+bucket) context := fakeContext{}
fmt.Fprintf(w, "\r") callback := make(chan Bucket)
fmt.Fprintf(w, "object: "+object) handler.requestBucketChan <- BucketRequest{
name: bucketName,
context: context,
callback: callback,
}
bucket := <-callback
object, err := bucket.Get(context, string(path))
if err != nil {
http.Error(w, err.Error(), 404)
} else {
fmt.Fprintf(w, string(object))
}
}
type GatewayPutHandler struct {
requestBucketChan chan BucketRequest
}
func (handler GatewayPutHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
bucketName := vars["bucket"]
path := vars["path"]
object, _ := ioutil.ReadAll(req.Body)
context := fakeContext{}
callback := make(chan Bucket)
handler.requestBucketChan <- BucketRequest{
name: bucketName,
context: context,
callback: callback,
}
bucket := <-callback
bucket.Put(context, path, object)
} }
func RegisterGatewayHandlers(router *mux.Router) { func RegisterGatewayHandlers(router *mux.Router) {
router.HandleFunc("/{bucket}/{object:.*}", GatewayGetObjectHandler).Methods("GET") requestBucketChan := make(chan BucketRequest)
go SynchronizedBucketService(requestBucketChan)
getHandler := GatewayGetHandler{requestBucketChan: requestBucketChan}
putHandler := GatewayPutHandler{requestBucketChan: requestBucketChan}
router.Handle("/{bucket}/{path:.*}", getHandler).Methods("GET")
router.Handle("/{bucket}/{path:.*}", putHandler).Methods("PUT")
} }
func SynchronizedBucketService(input chan BucketRequest) { func SynchronizedBucketService(input chan BucketRequest) {
@ -119,7 +162,6 @@ func (bucket *SynchronizedBucket) closeChannel() {
func inMemoryBucketServer(input chan ObjectRequest) { func inMemoryBucketServer(input chan ObjectRequest) {
objects := make(map[string][]byte) objects := make(map[string][]byte)
for request := range input { for request := range input {
fmt.Println(objects)
switch request.requestType { switch request.requestType {
case "GET": case "GET":
request.callback <- objects[request.path] request.callback <- objects[request.path]

@ -28,8 +28,8 @@ func (s *MySuite) TestPrintsGateway(c *C) {
log.Fatal(err) log.Fatal(err)
} }
bodyString := string(body) bodyString := string(body)
if bodyString != "Gateway" { if bodyString == "" {
log.Fatal("Expected 'Gateway', Received '" + bodyString + "'") log.Fatal("Expected '', Received '" + bodyString + "'")
} }
} }

Loading…
Cancel
Save