mirror of
https://github.com/minio/minio.git
synced 2025-12-09 17:09:51 -05:00
Moving gateway and storage driver to packages
This commit is contained in:
22
pkgs/storage/file_storage.go
Normal file
22
pkgs/storage/file_storage.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type FileStorage struct {
|
||||
RootDir string
|
||||
}
|
||||
|
||||
func (storage FileStorage) Get(objectPath string) ([]byte, error) {
|
||||
return ioutil.ReadFile(path.Join(storage.RootDir, objectPath))
|
||||
|
||||
}
|
||||
|
||||
func (storage FileStorage) Put(objectPath string, object []byte) error {
|
||||
os.MkdirAll(filepath.Dir(path.Join(storage.RootDir, objectPath)), 0700)
|
||||
return ioutil.WriteFile(path.Join(storage.RootDir, objectPath), object, 0600)
|
||||
}
|
||||
51
pkgs/storage/file_storage_test.go
Normal file
51
pkgs/storage/file_storage_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
. "gopkg.in/check.v1"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
type FileStorageSuite struct{}
|
||||
|
||||
var _ = Suite(&FileStorageSuite{})
|
||||
|
||||
func makeTempTestDir() (string, error) {
|
||||
return ioutil.TempDir("/tmp", "minio-test-")
|
||||
}
|
||||
|
||||
func (s *FileStorageSuite) TestFileStoragePutAtRootPath(c *C) {
|
||||
rootDir, err := makeTempTestDir()
|
||||
c.Assert(err, IsNil)
|
||||
defer os.RemoveAll(rootDir)
|
||||
|
||||
var storage ObjectStorage
|
||||
storage = FileStorage{
|
||||
RootDir: rootDir,
|
||||
}
|
||||
|
||||
storage.Put("path1", []byte("object1"))
|
||||
|
||||
// assert object1 was created in correct path
|
||||
object1, err := storage.Get("path1")
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(string(object1), Equals, "object1")
|
||||
}
|
||||
|
||||
func (s *FileStorageSuite) TestFileStoragePutDirPath(c *C) {
|
||||
rootDir, err := makeTempTestDir()
|
||||
c.Assert(err, IsNil)
|
||||
defer os.RemoveAll(rootDir)
|
||||
|
||||
var storage ObjectStorage
|
||||
storage = FileStorage{
|
||||
RootDir: rootDir,
|
||||
}
|
||||
|
||||
storage.Put("path1/path2/path3", []byte("object"))
|
||||
|
||||
// assert object1 was created in correct path
|
||||
object1, err := storage.Get("path1/path2/path3")
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(string(object1), Equals, "object")
|
||||
}
|
||||
20
pkgs/storage/storage.go
Normal file
20
pkgs/storage/storage.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ObjectStorage interface {
|
||||
Get(path string) ([]byte, error)
|
||||
Put(path string, object []byte) error
|
||||
}
|
||||
|
||||
func RegisterStorageHandlers(router *mux.Router) {
|
||||
router.HandleFunc("/storage/rpc", StorageHandler)
|
||||
}
|
||||
|
||||
func StorageHandler(w http.ResponseWriter, req *http.Request) {
|
||||
fmt.Fprintf(w, "Storage")
|
||||
}
|
||||
27
pkgs/storage/storage_test.go
Normal file
27
pkgs/storage/storage_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPrintsStorage(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(StorageHandler))
|
||||
defer server.Close()
|
||||
res, err := http.Get(server.URL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
bodyString := string(body)
|
||||
if bodyString != "Storage" {
|
||||
log.Fatal("Expected 'Storage', Received '" + bodyString + "'")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user