From b54040735da46429e442f544d186d3b86708d8c6 Mon Sep 17 00:00:00 2001 From: slhmy <31381093+slhmy@users.noreply.github.com> Date: Wed, 13 Sep 2023 10:42:26 +0000 Subject: [PATCH] Refactor minio client as agent --- .../minio.go => agent/minio/client.go} | 15 +++--- packages/agent/minio/problem.go | 50 +++++++++++++++++++ packages/mapper/problem.go | 46 ----------------- service/problem/problem.go | 3 +- tests/minio_test.go | 6 +-- 5 files changed, 63 insertions(+), 57 deletions(-) rename packages/{application/minio.go => agent/minio/client.go} (76%) create mode 100644 packages/agent/minio/problem.go diff --git a/packages/application/minio.go b/packages/agent/minio/client.go similarity index 76% rename from packages/application/minio.go rename to packages/agent/minio/client.go index fd74130..d95ac96 100644 --- a/packages/application/minio.go +++ b/packages/agent/minio/client.go @@ -1,9 +1,10 @@ -package application +package minio import ( "context" "log" + "github.com/OJ-lab/oj-lab-services/packages/application" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" ) @@ -28,12 +29,12 @@ var ( ) func init() { - endpoint = AppConfig.GetString(minioEndpointProp) - accessKeyID = AppConfig.GetString(minioAccessKeyProp) - secretAccessKey = AppConfig.GetString(minioSecretAccessKeyProp) - useSSL = AppConfig.GetBool(minioUseSSLProp) - region = AppConfig.GetString(minioRegionProp) - bucketName = AppConfig.GetString(minioBucketNameProp) + endpoint = application.AppConfig.GetString(minioEndpointProp) + accessKeyID = application.AppConfig.GetString(minioAccessKeyProp) + secretAccessKey = application.AppConfig.GetString(minioSecretAccessKeyProp) + useSSL = application.AppConfig.GetBool(minioUseSSLProp) + region = application.AppConfig.GetString(minioRegionProp) + bucketName = application.AppConfig.GetString(minioBucketNameProp) } func GetBucketName() string { diff --git a/packages/agent/minio/problem.go b/packages/agent/minio/problem.go new file mode 100644 index 0000000..ce0178c --- /dev/null +++ b/packages/agent/minio/problem.go @@ -0,0 +1,50 @@ +package minio + +import ( + "context" + "io/fs" + "log" + "path/filepath" + "strings" + + "github.com/minio/minio-go/v7" +) + +func PutProblemPackage(slug string, pkgDir string) error { + ctx := context.Background() + minioClient := GetMinioClient() + + // remove old package + objectsCh := minioClient.ListObjects(ctx, GetBucketName(), minio.ListObjectsOptions{ + Prefix: slug, + Recursive: true, + }) + for objInfo := range objectsCh { + if objInfo.Err != nil { + return objInfo.Err + } + + err := minioClient.RemoveObject(ctx, GetBucketName(), objInfo.Key, minio.RemoveObjectOptions{}) + if err != nil { + return err + } + } + + filepath.Walk(pkgDir, func(path string, info fs.FileInfo, err error) error { + if info.IsDir() { + return nil + } + relativePath := filepath.Join(slug, strings.Replace(path, pkgDir, "", 1)) + + _, minioErr := minioClient.FPutObject(ctx, GetBucketName(), + relativePath, + path, + minio.PutObjectOptions{}) + if minioErr != nil { + log.Fatalln(minioErr) + } + return minioErr + }) + + return nil +} diff --git a/packages/mapper/problem.go b/packages/mapper/problem.go index 4501a8b..284a59b 100644 --- a/packages/mapper/problem.go +++ b/packages/mapper/problem.go @@ -1,15 +1,8 @@ package mapper import ( - "context" - "io/fs" - "log" - "path/filepath" - "strings" - "github.com/OJ-lab/oj-lab-services/packages/application" "github.com/OJ-lab/oj-lab-services/packages/model" - "github.com/minio/minio-go/v7" ) func CreateProblem(problem model.Problem) error { @@ -108,42 +101,3 @@ func GetTagsList(problem model.Problem) []string { } return tagsList } - -func PutProblemPackage(slug string, pkgDir string) error { - ctx := context.Background() - minioClient := application.GetMinioClient() - - // remove old package - objectsCh := minioClient.ListObjects(ctx, application.GetBucketName(), minio.ListObjectsOptions{ - Prefix: slug, - Recursive: true, - }) - for objInfo := range objectsCh { - if objInfo.Err != nil { - return objInfo.Err - } - - err := minioClient.RemoveObject(ctx, application.GetBucketName(), objInfo.Key, minio.RemoveObjectOptions{}) - if err != nil { - return err - } - } - - filepath.Walk(pkgDir, func(path string, info fs.FileInfo, err error) error { - if info.IsDir() { - return nil - } - relativePath := filepath.Join(slug, strings.Replace(path, pkgDir, "", 1)) - - _, minioErr := minioClient.FPutObject(ctx, application.GetBucketName(), - relativePath, - path, - minio.PutObjectOptions{}) - if minioErr != nil { - log.Fatalln(minioErr) - } - return minioErr - }) - - return nil -} diff --git a/service/problem/problem.go b/service/problem/problem.go index f3e6141..c666fac 100644 --- a/service/problem/problem.go +++ b/service/problem/problem.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" + minioAgent "github.com/OJ-lab/oj-lab-services/packages/agent/minio" "github.com/OJ-lab/oj-lab-services/packages/mapper" "github.com/gin-gonic/gin" ) @@ -83,7 +84,7 @@ func PutProblemPackage(ctx *gin.Context) { } // put package to minio - err = mapper.PutProblemPackage(slug, targetDir) + err = minioAgent.PutProblemPackage(slug, targetDir) if err != nil { ctx.Error(err) return diff --git a/tests/minio_test.go b/tests/minio_test.go index e7904e2..d182f69 100644 --- a/tests/minio_test.go +++ b/tests/minio_test.go @@ -7,16 +7,16 @@ import ( "strings" "testing" - "github.com/OJ-lab/oj-lab-services/packages/application" + minioAgent "github.com/OJ-lab/oj-lab-services/packages/agent/minio" "github.com/minio/minio-go/v7" ) func TestMinio(T *testing.T) { // Initialize minio client object. - minioClient := application.GetMinioClient() + minioClient := minioAgent.GetMinioClient() log.Printf("%#v\n", minioClient) // minioClient is now set up - bucketName := application.GetBucketName() + bucketName := minioAgent.GetBucketName() err := minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{}) if err != nil {