Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Split filesystem disk #169

Merged
merged 4 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions filesystem/application.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package filesystem

import (
"context"
"fmt"

"github.com/gookit/color"
Expand All @@ -14,10 +13,6 @@ type Driver string

const (
DriverLocal Driver = "local"
DriverS3 Driver = "s3"
DriverOss Driver = "oss"
DriverCos Driver = "cos"
DriverMinio Driver = "minio"
DriverCustom Driver = "custom"
)

Expand Down Expand Up @@ -52,26 +47,22 @@ func NewStorage(config config.Config) *Storage {
}

func NewDriver(config config.Config, disk string) (filesystem.Driver, error) {
ctx := context.Background()
driver := Driver(config.GetString(fmt.Sprintf("filesystems.disks.%s.driver", disk)))
switch driver {
case DriverLocal:
return NewLocal(config, disk)
case DriverOss:
return NewOss(ctx, config, disk)
case DriverCos:
return NewCos(ctx, config, disk)
case DriverS3:
return NewS3(ctx, config, disk)
case DriverMinio:
return NewMinio(ctx, config, disk)
case DriverCustom:
driver, ok := config.Get(fmt.Sprintf("filesystems.disks.%s.via", disk)).(filesystem.Driver)
if !ok {
return nil, fmt.Errorf("init %s disk fail: via must be implement filesystem.Driver", disk)
if ok {
return driver, nil
}

return driver, nil
driverCallback, ok := config.Get(fmt.Sprintf("filesystems.disks.%s.via", disk)).(func() (filesystem.Driver, error))
if ok {
return driverCallback()
}

return nil, fmt.Errorf("init %s disk fail: via must be implement filesystem.Driver or func() (filesystem.Driver, error)", disk)
}

return nil, fmt.Errorf("invalid driver: %s, only support local, s3, oss, cos, minio, custom", driver)
Expand Down
164 changes: 0 additions & 164 deletions filesystem/application_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package filesystem

import (
"context"
"fmt"
"io/ioutil"
"mime"
"net/http"
Expand All @@ -11,17 +9,12 @@ import (
"time"

"github.com/gookit/color"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/ory/dockertest/v3"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"

configmocks "github.com/goravel/framework/contracts/config/mocks"
"github.com/goravel/framework/contracts/filesystem"
"github.com/goravel/framework/support/file"
supporttime "github.com/goravel/framework/support/time"
testingdocker "github.com/goravel/framework/testing/docker"
)

type TestDisk struct {
Expand All @@ -37,8 +30,6 @@ func TestStorage(t *testing.T) {

assert.Nil(t, file.Create("test.txt", "Goravel"))
mockConfig := initConfig()
minioPool, minioResource, err := initMinioDocker(mockConfig)
assert.Nil(t, err)

var driver filesystem.Driver

Expand All @@ -47,22 +38,6 @@ func TestStorage(t *testing.T) {
disk: "local",
url: "http://localhost/storage",
},
{
disk: "oss",
url: mockConfig.GetString("filesystems.disks.oss.url"),
},
{
disk: "cos",
url: mockConfig.GetString("filesystems.disks.cos.url"),
},
{
disk: "s3",
url: mockConfig.GetString("filesystems.disks.s3.url"),
},
{
disk: "minio",
url: mockConfig.GetString("filesystems.disks.minio.url"),
},
{
disk: "custom",
url: "http://localhost/storage",
Expand Down Expand Up @@ -429,7 +404,6 @@ func TestStorage(t *testing.T) {
}

file.Remove("test.txt")
assert.Nil(t, minioPool.Purge(minioResource))
}

func initConfig() *configmocks.Config {
Expand All @@ -446,144 +420,6 @@ func initConfig() *configmocks.Config {
root: "storage/app/public",
url: "http://localhost/storage",
})
mockConfig.On("GetString", "filesystems.disks.s3.driver").Return("s3")
mockConfig.On("GetString", "filesystems.disks.oss.driver").Return("oss")
mockConfig.On("GetString", "filesystems.disks.cos.driver").Return("cos")
mockConfig.On("GetString", "filesystems.disks.minio.driver").Return("minio")
mockConfig.On("GetString", "filesystems.disks.minio.region").Return("")
mockConfig.On("GetBool", "filesystems.disks.minio.ssl", false).Return(false)

if file.Exists("../.env") {
vip := viper.New()
vip.SetConfigName("../.env")
vip.SetConfigType("env")
vip.AddConfigPath(".")
_ = vip.ReadInConfig()
vip.SetEnvPrefix("goravel")
vip.AutomaticEnv()

mockConfig.On("GetString", "filesystems.disks.s3.key").Return(vip.Get("AWS_ACCESS_KEY_ID"))
mockConfig.On("GetString", "filesystems.disks.s3.secret").Return(vip.Get("AWS_ACCESS_KEY_SECRET"))
mockConfig.On("GetString", "filesystems.disks.s3.region").Return(vip.Get("AWS_DEFAULT_REGION"))
mockConfig.On("GetString", "filesystems.disks.s3.bucket").Return(vip.Get("AWS_BUCKET"))
mockConfig.On("GetString", "filesystems.disks.s3.url").Return(vip.Get("AWS_URL"))
mockConfig.On("GetString", "filesystems.disks.oss.key").Return(vip.Get("ALIYUN_ACCESS_KEY_ID"))
mockConfig.On("GetString", "filesystems.disks.oss.secret").Return(vip.Get("ALIYUN_ACCESS_KEY_SECRET"))
mockConfig.On("GetString", "filesystems.disks.oss.bucket").Return(vip.Get("ALIYUN_BUCKET"))
mockConfig.On("GetString", "filesystems.disks.oss.url").Return(vip.Get("ALIYUN_URL"))
mockConfig.On("GetString", "filesystems.disks.oss.endpoint").Return(vip.Get("ALIYUN_ENDPOINT"))
mockConfig.On("GetString", "filesystems.disks.cos.key").Return(vip.Get("TENCENT_ACCESS_KEY_ID"))
mockConfig.On("GetString", "filesystems.disks.cos.secret").Return(vip.Get("TENCENT_ACCESS_KEY_SECRET"))
mockConfig.On("GetString", "filesystems.disks.cos.bucket").Return(vip.Get("TENCENT_BUCKET"))
mockConfig.On("GetString", "filesystems.disks.cos.url").Return(vip.Get("TENCENT_URL"))
mockConfig.On("GetString", "filesystems.disks.minio.key").Return(vip.Get("MINIO_ACCESS_KEY_ID"))
mockConfig.On("GetString", "filesystems.disks.minio.secret").Return(vip.Get("MINIO_ACCESS_KEY_SECRET"))
mockConfig.On("GetString", "filesystems.disks.minio.bucket").Return(vip.Get("MINIO_BUCKET"))
}
if os.Getenv("AWS_ACCESS_KEY_ID") != "" {
mockConfig.On("GetString", "filesystems.disks.s3.key").Return(os.Getenv("AWS_ACCESS_KEY_ID"))
mockConfig.On("GetString", "filesystems.disks.s3.secret").Return(os.Getenv("AWS_ACCESS_KEY_SECRET"))
mockConfig.On("GetString", "filesystems.disks.s3.region").Return(os.Getenv("AWS_DEFAULT_REGION"))
mockConfig.On("GetString", "filesystems.disks.s3.bucket").Return(os.Getenv("AWS_BUCKET"))
mockConfig.On("GetString", "filesystems.disks.s3.url").Return(os.Getenv("AWS_URL"))
mockConfig.On("GetString", "filesystems.disks.oss.key").Return(os.Getenv("ALIYUN_ACCESS_KEY_ID"))
mockConfig.On("GetString", "filesystems.disks.oss.secret").Return(os.Getenv("ALIYUN_ACCESS_KEY_SECRET"))
mockConfig.On("GetString", "filesystems.disks.oss.bucket").Return(os.Getenv("ALIYUN_BUCKET"))
mockConfig.On("GetString", "filesystems.disks.oss.url").Return(os.Getenv("ALIYUN_URL"))
mockConfig.On("GetString", "filesystems.disks.oss.endpoint").Return(os.Getenv("ALIYUN_ENDPOINT"))
mockConfig.On("GetString", "filesystems.disks.cos.key").Return(os.Getenv("TENCENT_ACCESS_KEY_ID"))
mockConfig.On("GetString", "filesystems.disks.cos.secret").Return(os.Getenv("TENCENT_ACCESS_KEY_SECRET"))
mockConfig.On("GetString", "filesystems.disks.cos.bucket").Return(os.Getenv("TENCENT_BUCKET"))
mockConfig.On("GetString", "filesystems.disks.cos.url").Return(os.Getenv("TENCENT_URL"))
mockConfig.On("GetString", "filesystems.disks.minio.key").Return(os.Getenv("MINIO_ACCESS_KEY_ID"))
mockConfig.On("GetString", "filesystems.disks.minio.secret").Return(os.Getenv("MINIO_ACCESS_KEY_SECRET"))
mockConfig.On("GetString", "filesystems.disks.minio.bucket").Return(os.Getenv("MINIO_BUCKET"))
}

return mockConfig
}

func initMinioDocker(mockConfig *configmocks.Config) (*dockertest.Pool, *dockertest.Resource, error) {
pool, err := testingdocker.Pool()
if err != nil {
return nil, nil, err
}

key := mockConfig.GetString("filesystems.disks.minio.key")
secret := mockConfig.GetString("filesystems.disks.minio.secret")
bucket := mockConfig.GetString("filesystems.disks.minio.bucket")
resource, err := testingdocker.Resource(pool, &dockertest.RunOptions{
Repository: "minio/minio",
Tag: "latest",
Env: []string{
"MINIO_ACCESS_KEY=" + key,
"MINIO_SECRET_KEY=" + secret,
},
Cmd: []string{
"server",
"/data",
},
ExposedPorts: []string{
"9000/tcp",
},
})
if err != nil {
return nil, nil, err
}

_ = resource.Expire(600)

endpoint := fmt.Sprintf("127.0.0.1:%s", resource.GetPort("9000/tcp"))
mockConfig.On("GetString", "filesystems.disks.minio.url").Return(fmt.Sprintf("http://%s/%s", endpoint, bucket))
mockConfig.On("GetString", "filesystems.disks.minio.endpoint").Return(endpoint)

if err := pool.Retry(func() error {
client, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(key, secret, ""),
})
if err != nil {
return err
}

if err := client.MakeBucket(context.Background(), mockConfig.GetString("filesystems.disks.minio.bucket"), minio.MakeBucketOptions{}); err != nil {
return err
}

policy := `{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Effect": "Allow",
"Principal": "*",
"Resource": [
"arn:aws:s3:::` + bucket + `/*"
]
},
{
"Action": [
"s3:ListBucket"
],
"Effect": "Allow",
"Principal": "*",
"Resource": [
"arn:aws:s3:::` + bucket + `"
]
}
]
}`

if err := client.SetBucketPolicy(context.Background(), bucket, policy); err != nil {
return err
}

return nil
}); err != nil {
return nil, nil, err
}

return pool, resource, nil
}
Loading