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

Refactor minio client as agent #19

Merged
merged 1 commit into from
Sep 13, 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
15 changes: 8 additions & 7 deletions packages/application/minio.go → packages/agent/minio/client.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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 {
Expand Down
50 changes: 50 additions & 0 deletions packages/agent/minio/problem.go
Original file line number Diff line number Diff line change
@@ -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
}
46 changes: 0 additions & 46 deletions packages/mapper/problem.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion service/problem/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/minio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down