You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
222 lines
5.5 KiB
222 lines
5.5 KiB
10 years ago
|
package gateway
|
||
10 years ago
|
|
||
|
import (
|
||
10 years ago
|
"errors"
|
||
10 years ago
|
"fmt"
|
||
10 years ago
|
"io/ioutil"
|
||
10 years ago
|
"net/http"
|
||
10 years ago
|
"path"
|
||
10 years ago
|
|
||
|
"github.com/gorilla/mux"
|
||
|
"github.com/minio-io/minio/pkgs/storage"
|
||
|
"github.com/tchap/go-patricia/patricia"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
// Stores system configuration, populated from CLI or test runner
|
||
|
type GatewayConfig struct {
|
||
10 years ago
|
StorageDriver StorageDriver
|
||
10 years ago
|
BucketDriver BucketDriver
|
||
10 years ago
|
requestBucketChan chan BucketRequest
|
||
10 years ago
|
dataDir string
|
||
10 years ago
|
}
|
||
|
|
||
|
// Message for requesting a bucket
|
||
10 years ago
|
type BucketRequest struct {
|
||
|
name string
|
||
|
context Context
|
||
|
callback chan Bucket
|
||
|
}
|
||
|
|
||
10 years ago
|
// Context interface for security and session information
|
||
10 years ago
|
type Context interface{}
|
||
|
|
||
10 years ago
|
// Bucket definition
|
||
10 years ago
|
type Bucket interface {
|
||
|
GetName(Context) string
|
||
|
Get(Context, string) ([]byte, error)
|
||
|
Put(Context, string, []byte) error
|
||
|
}
|
||
|
|
||
10 years ago
|
// Bucket driver function, should read from a channel and respond through callback channels
|
||
|
type BucketDriver func(config GatewayConfig)
|
||
|
|
||
10 years ago
|
// Storage driver function, should read from a channel and respond through callback channels
|
||
10 years ago
|
type StorageDriver func(bucket string, input chan ObjectRequest, config GatewayConfig)
|
||
10 years ago
|
|
||
10 years ago
|
// TODO remove when building real context
|
||
|
type fakeContext struct{}
|
||
10 years ago
|
|
||
10 years ago
|
type GatewayGetHandler struct {
|
||
10 years ago
|
config GatewayConfig
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// GET requests server
|
||
10 years ago
|
func (handler GatewayGetHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||
10 years ago
|
vars := mux.Vars(req)
|
||
10 years ago
|
bucketName := vars["bucket"]
|
||
|
path := vars["path"]
|
||
|
context := fakeContext{}
|
||
|
callback := make(chan Bucket)
|
||
10 years ago
|
handler.config.requestBucketChan <- BucketRequest{
|
||
10 years ago
|
name: bucketName,
|
||
|
context: context,
|
||
|
callback: callback,
|
||
|
}
|
||
|
bucket := <-callback
|
||
|
object, err := bucket.Get(context, string(path))
|
||
|
if err != nil {
|
||
|
http.Error(w, err.Error(), 404)
|
||
10 years ago
|
} else if object == nil {
|
||
|
http.Error(w, errors.New("Object not found").Error(), 404)
|
||
10 years ago
|
} else {
|
||
|
fmt.Fprintf(w, string(object))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type GatewayPutHandler struct {
|
||
10 years ago
|
config GatewayConfig
|
||
10 years ago
|
}
|
||
|
|
||
|
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)
|
||
10 years ago
|
handler.config.requestBucketChan <- BucketRequest{
|
||
10 years ago
|
name: bucketName,
|
||
|
context: context,
|
||
|
callback: callback,
|
||
|
}
|
||
|
bucket := <-callback
|
||
|
bucket.Put(context, path, object)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
func RegisterGatewayHandlers(router *mux.Router, config GatewayConfig) {
|
||
10 years ago
|
config.requestBucketChan = make(chan BucketRequest)
|
||
10 years ago
|
go config.BucketDriver(config)
|
||
10 years ago
|
getHandler := GatewayGetHandler{config}
|
||
|
putHandler := GatewayPutHandler{config}
|
||
10 years ago
|
router.Handle("/{bucket}/{path:.*}", getHandler).Methods("GET")
|
||
|
router.Handle("/{bucket}/{path:.*}", putHandler).Methods("PUT")
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
func SynchronizedBucketDriver(config GatewayConfig) {
|
||
10 years ago
|
buckets := make(map[string]*SynchronizedBucket)
|
||
10 years ago
|
for request := range config.requestBucketChan {
|
||
10 years ago
|
if buckets[request.name] == nil {
|
||
|
bucketChannel := make(chan ObjectRequest)
|
||
10 years ago
|
go config.StorageDriver(request.name, bucketChannel, config)
|
||
10 years ago
|
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,
|
||
|
}
|
||
10 years ago
|
response := <-callback
|
||
|
|
||
|
switch response.(type) {
|
||
10 years ago
|
case error:
|
||
|
return nil, response.(error)
|
||
10 years ago
|
case nil:
|
||
|
return nil, errors.New("Object not found")
|
||
|
case interface{}:
|
||
|
return response.([]byte), nil
|
||
10 years ago
|
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)
|
||
|
}
|
||
|
|
||
10 years ago
|
func InMemoryStorageDriver(bucket string, input chan ObjectRequest, config GatewayConfig) {
|
||
10 years ago
|
objects := patricia.NewTrie()
|
||
10 years ago
|
for request := range input {
|
||
10 years ago
|
prefix := patricia.Prefix(request.path)
|
||
10 years ago
|
switch request.requestType {
|
||
|
case "GET":
|
||
10 years ago
|
request.callback <- objects.Get(prefix)
|
||
10 years ago
|
case "PUT":
|
||
10 years ago
|
objects.Insert(prefix, request.object)
|
||
10 years ago
|
request.callback <- nil
|
||
|
default:
|
||
|
request.callback <- errors.New("Unexpected message")
|
||
|
}
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
|
func SimpleFileStorageDriver(bucket string, input chan ObjectRequest, config GatewayConfig) {
|
||
10 years ago
|
fileStorage := storage.FileStorage{
|
||
10 years ago
|
RootDir: config.dataDir,
|
||
|
}
|
||
|
for request := range input {
|
||
|
switch request.requestType {
|
||
|
case "GET":
|
||
|
objectPath := path.Join(bucket, request.path)
|
||
10 years ago
|
object, err := fileStorage.Get(objectPath)
|
||
10 years ago
|
if err != nil {
|
||
|
request.callback <- nil
|
||
|
} else {
|
||
|
request.callback <- object
|
||
|
}
|
||
|
case "PUT":
|
||
|
objectPath := path.Join(bucket, request.path)
|
||
10 years ago
|
fileStorage.Put(objectPath, request.object)
|
||
10 years ago
|
request.callback <- nil
|
||
|
default:
|
||
|
request.callback <- errors.New("Unexpected message")
|
||
|
}
|
||
|
}
|
||
|
}
|