Moving gateway and storage driver to packages

This commit is contained in:
Frederick F. Kautz IV
2014-11-29 14:42:22 -08:00
parent d6b65f1f04
commit 03beef3afc
9 changed files with 27 additions and 23 deletions

View 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)
}

View 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
View 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")
}

View 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 + "'")
}
}