From 8fb892c5a9b2cdf4614759775d652c7ccb55dab5 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Tue, 12 Mar 2019 16:08:25 -0400 Subject: [PATCH] minio: WIP --- dbutil/minio/minio.go | 98 +++++++++++++++++++++++++++++++++++++++++++ server.go | 5 ++- 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 dbutil/minio/minio.go diff --git a/dbutil/minio/minio.go b/dbutil/minio/minio.go new file mode 100644 index 0000000..e0dba93 --- /dev/null +++ b/dbutil/minio/minio.go @@ -0,0 +1,98 @@ +package minio + +import ( + "encoding/json" + "fmt" + + "github.com/hashicorp/errwrap" + "github.com/minio/minio-go" + "github.com/vbatts/imgsrv/dbutil" + "github.com/vbatts/imgsrv/types" +) + +type minioConfig struct { + Endpoint string + AccessKeyID string + SecretAccessKey string + Secure bool + Region string + + Bucket string +} + +func init() { + dbutil.Handles["minio"] = &minioHandle{} +} + +type minioHandle struct { + Config minioConfig + Client *minio.Client +} + +func (mh *minioHandle) Init(config []byte, err error) error { + if err != nil { + return errwrap.Wrapf("minio Init: {{er}}", err) + } + + mh.Config = minioConfig{} + if err := json.Unmarshal(config, &mh.Config); err != nil { + return err + } + + mh.Client, err = minio.NewWithRegion(mh.Config.Endpoint, mh.Config.AccessKeyID, mh.Config.SecretAccessKey, mh.Config.Secure, mh.Config.Region) + if err != nil { + return errwrap.Wrapf("minio Client: {{er}}", err) + } + + if buc, err := mh.Client.BucketExists(mh.Config.Bucket); err != nil || !buc { + return fmt.Errorf("can not access bucket %q", mh.Config.Bucket) + } + return nil +} + +// ErrNotImplemented for functions as the stubs are being written +var ErrNotImplemented = fmt.Errorf("function not implemented") + +func (mh *minioHandle) Close() error { + return nil +} + +func (mh *minioHandle) Open(filename string) (dbutil.File, error) { + return nil, ErrNotImplemented +} +func (mh *minioHandle) Create(filename string) (dbutil.File, error) { + return nil, ErrNotImplemented +} +func (mh *minioHandle) Remove(filename string) error { + return mh.Client.RemoveObject(mh.Config.Bucket, filename) +} + +func (mh *minioHandle) HasFileByFilename(filename string) (exists bool, err error) { + return false, ErrNotImplemented +} +func (mh *minioHandle) FindFilesByKeyword(keyword string) (files []types.File, err error) { + return nil, ErrNotImplemented +} +func (mh *minioHandle) FindFilesByMd5(md5 string) (files []types.File, err error) { + return nil, ErrNotImplemented +} +func (mh *minioHandle) FindFilesByPatt(filenamePat string) (files []types.File, err error) { + return nil, ErrNotImplemented +} + +func (mh *minioHandle) CountFiles(filename string) (int, error) { + return -1, ErrNotImplemented +} + +func (mh *minioHandle) GetFiles(limit int) (files []types.File, err error) { + return nil, ErrNotImplemented +} +func (mh *minioHandle) GetFileByFilename(filename string) (types.File, error) { + return types.File{}, ErrNotImplemented +} +func (mh *minioHandle) GetExtensions() (kp []types.IdCount, err error) { + return nil, ErrNotImplemented +} +func (mh *minioHandle) GetKeywords() (kp []types.IdCount, err error) { + return nil, ErrNotImplemented +} diff --git a/server.go b/server.go index b0656a1..abed701 100644 --- a/server.go +++ b/server.go @@ -16,6 +16,7 @@ import ( "github.com/vbatts/imgsrv/assets" "github.com/vbatts/imgsrv/config" "github.com/vbatts/imgsrv/dbutil" + _ "github.com/vbatts/imgsrv/dbutil/minio" _ "github.com/vbatts/imgsrv/dbutil/mongo" "github.com/vbatts/imgsrv/hash" "github.com/vbatts/imgsrv/types" @@ -39,7 +40,8 @@ func runServer(c *config.Config) { log.Fatalf("DbHandler %q not found", serverConfig.DbHandler) } - if serverConfig.DbHandler == "mongo" { + switch { + case serverConfig.DbHandler == "mongo": duConfig = struct { Seed string User string @@ -51,6 +53,7 @@ func runServer(c *config.Config) { serverConfig.MongoPassword, serverConfig.MongoDbName, } + case serverConfig.DbHandler == "minio": } if err := du.Init(json.Marshal(duConfig)); err != nil {