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-08 02:32:01 -05:00
|
|
|
"github.com/tchap/go-patricia/patricia"
|
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 {
|
2014-11-07 22:07:16 -05:00
|
|
|
StorageDriver StorageDriver
|
2014-11-14 01:18:44 -05:00
|
|
|
BucketDriver BucketDriver
|
2014-11-07 22:07:16 -05:00
|
|
|
requestBucketChan chan BucketRequest
|
2014-11-07 01:45:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-14 01:18:44 -05:00
|
|
|
// Bucket driver function, should read from a channel and respond through callback channels
|
|
|
|
type BucketDriver func(config GatewayConfig)
|
|
|
|
|
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 {
|
2014-11-07 22:07:16 -05:00
|
|
|
config GatewayConfig
|
2014-11-06 22:32:35 -05:00
|
|
|
}
|
|
|
|
|
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)
|
2014-11-07 22:07:16 -05:00
|
|
|
handler.config.requestBucketChan <- BucketRequest{
|
2014-11-06 22:32:35 -05:00
|
|
|
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 {
|
2014-11-07 22:07:16 -05:00
|
|
|
config GatewayConfig
|
2014-11-06 22:32:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2014-11-07 22:07:16 -05:00
|
|
|
handler.config.requestBucketChan <- BucketRequest{
|
2014-11-06 22:32:35 -05:00
|
|
|
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-07 22:07:16 -05:00
|
|
|
config.requestBucketChan = make(chan BucketRequest)
|
2014-11-14 01:18:44 -05:00
|
|
|
go config.BucketDriver(config)
|
2014-11-07 22:07:16 -05:00
|
|
|
getHandler := GatewayGetHandler{config}
|
|
|
|
putHandler := GatewayPutHandler{config}
|
2014-11-06 22:32:35 -05:00
|
|
|
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-14 01:18:44 -05:00
|
|
|
func SynchronizedBucketDriver(config GatewayConfig) {
|
2014-11-06 21:34:46 -05:00
|
|
|
buckets := make(map[string]*SynchronizedBucket)
|
2014-11-07 22:07:16 -05:00
|
|
|
for request := range config.requestBucketChan {
|
2014-11-06 21:34:46 -05:00
|
|
|
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,
|
|
|
|
}
|
2014-11-08 02:32:01 -05:00
|
|
|
response := <-callback
|
|
|
|
|
|
|
|
switch response.(type) {
|
2014-11-06 21:34:46 -05:00
|
|
|
case error:
|
|
|
|
return nil, response.(error)
|
2014-11-08 02:32:01 -05:00
|
|
|
case nil:
|
|
|
|
return nil, errors.New("Object not found")
|
|
|
|
case interface{}:
|
|
|
|
return response.([]byte), nil
|
2014-11-06 21:34:46 -05:00
|
|
|
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-08 02:32:01 -05:00
|
|
|
objects := patricia.NewTrie()
|
2014-11-06 21:34:46 -05:00
|
|
|
for request := range input {
|
2014-11-08 02:32:01 -05:00
|
|
|
prefix := patricia.Prefix(request.path)
|
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-08 02:32:01 -05:00
|
|
|
request.callback <- objects.Get(prefix)
|
2014-11-06 21:34:46 -05:00
|
|
|
case "PUT":
|
2014-11-07 01:45:27 -05:00
|
|
|
fmt.Println("PUT: " + request.path)
|
2014-11-08 02:32:01 -05:00
|
|
|
objects.Insert(prefix, request.object)
|
2014-11-06 21:34:46 -05:00
|
|
|
request.callback <- nil
|
|
|
|
default:
|
|
|
|
request.callback <- errors.New("Unexpected message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|