Skip to content

Commit

Permalink
Remove minio
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Jun 8, 2023
1 parent 9ca7e6d commit 901a671
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 504 deletions.
7 changes: 0 additions & 7 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,9 +13,6 @@ type Driver string

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

Expand Down Expand Up @@ -51,13 +47,10 @@ 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 DriverMinio:
return NewMinio(ctx, config, disk)
case DriverCustom:
driver, ok := config.Get(fmt.Sprintf("filesystems.disks.%s.via", disk)).(filesystem.Driver)
if ok {
Expand Down
121 changes: 0 additions & 121 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,10 +38,6 @@ func TestStorage(t *testing.T) {
disk: "local",
url: "http://localhost/storage",
},
{
disk: "minio",
url: mockConfig.GetString("filesystems.disks.minio.url"),
},
{
disk: "custom",
url: "http://localhost/storage",
Expand Down Expand Up @@ -417,7 +404,6 @@ func TestStorage(t *testing.T) {
}

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

func initConfig() *configmocks.Config {
Expand All @@ -434,113 +420,6 @@ func initConfig() *configmocks.Config {
root: "storage/app/public",
url: "http://localhost/storage",
})
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.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.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

0 comments on commit 901a671

Please sign in to comment.