2014-11-02 18:56:33 -05:00
|
|
|
package minio
|
|
|
|
|
|
|
|
import (
|
2014-11-06 21:34:46 -05:00
|
|
|
"errors"
|
2014-11-02 18:56:33 -05:00
|
|
|
"fmt"
|
|
|
|
"github.com/gorilla/mux"
|
2014-11-06 22:32:35 -05:00
|
|
|
"io/ioutil"
|
2014-11-02 18:56:33 -05:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2014-11-07 01:45:27 -05:00
|
|
|
// Stores system configuration, populated from CLI or test runner
|
|
|
|
type GatewayConfig struct {
|
|
|
|
StorageDriver StorageDriver
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message for requesting a bucket
|
2014-11-06 21:34:46 -05:00
|
|
|
type BucketRequest struct {
|
|
|
|
name string
|
|
|
|
context Context
|
|
|
|
callback chan Bucket
|
|
|
|
}
|
|
|
|
|
2014-11-07 01:45:27 -05:00
|
|
|
// Context interface for security and session information
|
2014-11-06 21:34:46 -05:00
|
|
|
type Context interface{}
|
|
|
|
|
2014-11-07 01:45:27 -05:00
|
|
|
// Bucket definition
|
2014-11-06 21:34:46 -05:00
|
|
|
type Bucket interface {
|
|
|
|
GetName(Context) string
|
|
|
|
Get(Context, string) ([]byte, error)
|
|
|
|
Put(Context, string, []byte) error
|
|
|
|
}
|
|
|
|
|
2014-11-07 01:45:27 -05:00
|
|
|
// Storage driver function, should read from a channel and respond through callback channels
|
|
|
|
type StorageDriver func(bucket string, input chan ObjectRequest)
|
2014-11-06 22:32:35 -05:00
|
|
|
|
2014-11-07 01:45:27 -05:00
|
|
|
// TODO remove when building real context
|
|
|
|
type fakeContext struct{}
|
2014-11-02 18:56:33 -05:00
|
|
|
|
2014-11-06 22:32:35 -05:00
|
|
|
type GatewayGetHandler struct {
|
|
|
|
requestBucketChan chan BucketRequest
|
|
|
|
}
|
|
|
|
|
2014-11-07 01:45:27 -05:00
|
|
|
// GET requests server
|
2014-11-06 22:32:35 -05:00
|
|
|
func (handler GatewayGetHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2014-11-02 22:55:01 -05:00
|
|
|
vars := mux.Vars(req)
|
2014-11-06 22:32:35 -05:00
|
|
|
bucketName := vars["bucket"]
|
|
|
|
path := vars["path"]
|
|
|
|
context := fakeContext{}
|
|
|
|
callback := make(chan Bucket)
|
|
|
|
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)
|
2014-11-07 01:45:27 -05:00
|
|
|
} else if object == nil {
|
|
|
|
http.Error(w, errors.New("Object not found").Error(), 404)
|
2014-11-06 22:32:35 -05:00
|
|
|
} 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)
|
2014-11-02 22:55:01 -05:00
|
|
|
}
|
|
|
|
|
2014-11-07 01:45:27 -05:00
|
|
|
func RegisterGatewayHandlers(router *mux.Router, config GatewayConfig) {
|
2014-11-06 22:32:35 -05:00
|
|
|
requestBucketChan := make(chan BucketRequest)
|
2014-11-07 01:45:27 -05:00
|
|
|
go SynchronizedBucketService(requestBucketChan, config)
|
2014-11-06 22:32:35 -05:00
|
|
|
getHandler := GatewayGetHandler{requestBucketChan: requestBucketChan}
|
|
|
|
putHandler := GatewayPutHandler{requestBucketChan: requestBucketChan}
|
|
|
|
router.Handle("/{bucket}/{path:.*}", getHandler).Methods("GET")
|
|
|
|
router.Handle("/{bucket}/{path:.*}", putHandler).Methods("PUT")
|
2014-11-02 18:56:33 -05:00
|
|
|
}
|
2014-11-06 21:34:46 -05:00
|
|
|
|
2014-11-07 01:45:27 -05:00
|
|
|
func SynchronizedBucketService(input chan BucketRequest, config GatewayConfig) {
|
2014-11-06 21:34:46 -05:00
|
|
|
buckets := make(map[string]*SynchronizedBucket)
|
|
|
|
for request := range input {
|
|
|
|
if buckets[request.name] == nil {
|
|
|
|
bucketChannel := make(chan ObjectRequest)
|
2014-11-07 01:45:27 -05:00
|
|
|
go config.StorageDriver(request.name, bucketChannel)
|
2014-11-06 21:34:46 -05:00
|
|
|
buckets[request.name] = &SynchronizedBucket{
|
|
|
|
name: request.name,
|
|
|
|
channel: bucketChannel,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
request.callback <- buckets[request.name]
|
|
|
|
}
|
|
|
|
for key := range buckets {
|
|
|
|
buckets[key].closeChannel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SynchronizedBucket struct {
|
|
|
|
name string
|
|
|
|
channel chan ObjectRequest
|
|
|
|
objects map[string][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type ObjectRequest struct {
|
|
|
|
requestType string
|
|
|
|
path string
|
|
|
|
object []byte
|
|
|
|
callback chan interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bucket SynchronizedBucket) GetName(context Context) string {
|
|
|
|
return bucket.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bucket SynchronizedBucket) Get(context Context, path string) ([]byte, error) {
|
|
|
|
callback := make(chan interface{})
|
|
|
|
bucket.channel <- ObjectRequest{
|
|
|
|
requestType: "GET",
|
|
|
|
path: path,
|
|
|
|
callback: callback,
|
|
|
|
}
|
|
|
|
switch response := <-callback; response.(type) {
|
|
|
|
case []byte:
|
|
|
|
return response.([]byte), nil
|
|
|
|
case error:
|
|
|
|
return nil, response.(error)
|
|
|
|
default:
|
|
|
|
return nil, errors.New("Unexpected error, service failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bucket SynchronizedBucket) Put(context Context, path string, object []byte) error {
|
|
|
|
callback := make(chan interface{})
|
|
|
|
bucket.channel <- ObjectRequest{
|
|
|
|
requestType: "PUT",
|
|
|
|
path: path,
|
|
|
|
object: object,
|
|
|
|
callback: callback,
|
|
|
|
}
|
|
|
|
switch response := <-callback; response.(type) {
|
|
|
|
case error:
|
|
|
|
return response.(error)
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return errors.New("Unexpected error, service failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bucket *SynchronizedBucket) closeChannel() {
|
|
|
|
close(bucket.channel)
|
|
|
|
}
|
|
|
|
|
2014-11-07 01:45:27 -05:00
|
|
|
func InMemoryStorageDriver(bucket string, input chan ObjectRequest) {
|
2014-11-06 21:34:46 -05:00
|
|
|
objects := make(map[string][]byte)
|
|
|
|
for request := range input {
|
2014-11-07 01:45:27 -05:00
|
|
|
fmt.Println("objects:", objects)
|
2014-11-06 21:34:46 -05:00
|
|
|
switch request.requestType {
|
|
|
|
case "GET":
|
2014-11-07 01:45:27 -05:00
|
|
|
fmt.Println("GET: " + request.path)
|
2014-11-06 21:34:46 -05:00
|
|
|
request.callback <- objects[request.path]
|
|
|
|
case "PUT":
|
2014-11-07 01:45:27 -05:00
|
|
|
fmt.Println("PUT: " + request.path)
|
2014-11-06 21:34:46 -05:00
|
|
|
objects[request.path] = request.object
|
|
|
|
request.callback <- nil
|
|
|
|
default:
|
|
|
|
request.callback <- errors.New("Unexpected message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|