-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from iammuho/19-implement-service-agnostic-sto…
…rage-context Implement Storage Context with Driver-Agnostic Design (Local File Storage as Initial Implementation)
- Loading branch information
Showing
11 changed files
with
232 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package file | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/iammuho/natternet/pkg/storage/drivers" | ||
) | ||
|
||
type file struct{} | ||
|
||
// NewFileStorage returns a new file storage | ||
func NewFileStorage() drivers.DriverContext { | ||
return &file{} | ||
} | ||
|
||
// Get returns a file | ||
func (f *file) Get(fileName string) ([]byte, error) { | ||
// open the file | ||
file, err := os.Open(fileName) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// close the file | ||
defer file.Close() | ||
|
||
// get the file info | ||
fileInfo, err := file.Stat() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// prepare the buffer | ||
buffer := make([]byte, fileInfo.Size()) | ||
|
||
// read the file | ||
_, err = file.Read(buffer) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return buffer, nil | ||
} | ||
|
||
// Put puts a file | ||
func (f *file) Put(fileName string, content []byte) error { | ||
// create the file | ||
file, err := os.Create(fileName) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
// close the file | ||
defer file.Close() | ||
|
||
// write the content | ||
_, err = file.Write(content) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Delete deletes a file | ||
func (f *file) Delete(fileName string) error { | ||
// delete the file | ||
err := os.Remove(fileName) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// List lists files | ||
func (f *file) List(path string) ([]string, error) { | ||
// list all files in a directory | ||
dir, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// close the directory | ||
defer dir.Close() | ||
|
||
// get the list of files | ||
files, err := dir.Readdir(0) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// prepare the list of files | ||
var fileList []string | ||
|
||
// loop through the files | ||
for _, file := range files { | ||
// append the file name to the list | ||
fileList = append(fileList, file.Name()) | ||
} | ||
|
||
// return the list of files | ||
return fileList, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package drivers | ||
|
||
// DriverContext is the interface for the storage driver | ||
// TODO: add/refactor methods | ||
type DriverContext interface { | ||
Get(string) ([]byte, error) | ||
Put(string, []byte) error | ||
Delete(string) error | ||
List(string) ([]string, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package storage | ||
|
||
import "github.com/iammuho/natternet/pkg/storage/drivers" | ||
|
||
// StorageContext is the interface for the storage | ||
type StorageContext interface { | ||
Driver() drivers.DriverContext | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package storage | ||
|
||
// Option is the func interface to assign options | ||
type Option func(*StorageOptions) | ||
|
||
type Driver string | ||
|
||
const ( | ||
// DriverFile is the file driver | ||
DriverFile Driver = "file" | ||
// DriverAWS is the AWS driver | ||
DriverAWS Driver = "aws" | ||
) | ||
|
||
// StorageOptions defines the options for the storage | ||
type StorageOptions struct { | ||
Driver Driver | ||
} | ||
|
||
// WithStorageDriver sets the storage driver | ||
func WithStorageDriver(driver string) Option { | ||
return func(o *StorageOptions) { | ||
o.Driver = Driver(driver) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package storage | ||
|
||
import ( | ||
"github.com/iammuho/natternet/pkg/storage/drivers" | ||
"github.com/iammuho/natternet/pkg/storage/drivers/file" | ||
) | ||
|
||
type storage struct { | ||
driver drivers.DriverContext | ||
options StorageOptions | ||
} | ||
|
||
func NewStorage(opts ...Option) (StorageContext, error) { | ||
// Setup the driver | ||
options := StorageOptions{} | ||
for _, o := range opts { | ||
o(&options) | ||
} | ||
|
||
switch options.Driver { | ||
case DriverFile: | ||
return &storage{ | ||
driver: file.NewFileStorage(), | ||
options: options, | ||
}, nil | ||
} | ||
|
||
return &storage{}, nil | ||
} | ||
|
||
func (s *storage) Driver() drivers.DriverContext { | ||
return s.driver | ||
} |