diff --git a/Dockerfile b/Dockerfile index b01018d..d91ca09 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,6 +14,8 @@ ADD api/ api/ ADD config/ config/ +ADD media/ media/ + RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o agecem-org . # Alpine diff --git a/cmd/server.go b/cmd/server.go index 9d6d349..c56b1a8 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -24,6 +24,7 @@ import ( "git.agecem.com/agecem/agecem-org/api" "git.agecem.com/agecem/agecem-org/config" + "git.agecem.com/agecem/agecem-org/media" "git.agecem.com/agecem/agecem-org/public" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" @@ -233,14 +234,10 @@ func handleV1(c echo.Context) error { // handleV1Seed créé des buckets dans minio selon la liste de buckets dans server.documents.buckets // Les buckets sont créés avec paramètres par défaut, et sont ensuite visible dans /v1/bucket. func handleV1Seed(c echo.Context) error { - // Initialize minio client object - client, err := minio.New(cfg.Server.Documents.Endpoint, &minio.Options{ - Creds: credentials.NewStaticV4(cfg.Server.Documents.AccessKeyId, cfg.Server.Documents.SecretAccessKey, ""), - Secure: cfg.Server.Documents.UseSSL, - }) + mediaClient, err := media.NewMediaClientFromViper() if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{ - "message": "Error during minio#New", + "message": "Error during media.NewMediaClientFromViper()", "error": err.Error(), }) } @@ -248,7 +245,7 @@ func handleV1Seed(c echo.Context) error { var new_buckets []string for _, bucket := range cfg.Server.Documents.Buckets { - exists, err := client.BucketExists(context.Background(), bucket) + exists, err := mediaClient.MinioClient.BucketExists(context.Background(), bucket) if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{ "message": "Error during minio#BucketExists", @@ -260,7 +257,7 @@ func handleV1Seed(c echo.Context) error { continue } - if err = client.MakeBucket(context.Background(), bucket, minio.MakeBucketOptions{}); err != nil { + if err = mediaClient.MinioClient.MakeBucket(context.Background(), bucket, minio.MakeBucketOptions{}); err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{ "message": "Error during minio#MakeBucket", "error": err.Error(), diff --git a/media/media.go b/media/media.go new file mode 100644 index 0000000..d204b8d --- /dev/null +++ b/media/media.go @@ -0,0 +1,44 @@ +package media + +import ( + "git.agecem.com/agecem/agecem-org/config" + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" + "github.com/spf13/viper" +) + +func NewMediaClient(endpoint, accessKeyId, secretAccessKey string, useSSL bool) (*MediaClient, error) { + var mediaClient MediaClient + minioClient, err := minio.New(endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(accessKeyId, secretAccessKey, ""), + Secure: useSSL, + }) + if err != nil { + return &mediaClient, err + } + + mediaClient.MinioClient = *minioClient + return &mediaClient, nil +} + +func NewMediaClientFromViper() (*MediaClient, error) { + var cfg config.Config + if err := viper.Unmarshal(&cfg); err != nil { + return nil, err + } + + mediaClient, err := NewMediaClient(cfg.Server.Documents.Endpoint, cfg.Server.Documents.AccessKeyId, cfg.Server.Documents.SecretAccessKey, cfg.Server.Documents.UseSSL) + if err != nil { + return mediaClient, err + } + + return mediaClient, nil +} + +type MediaClient struct { + MinioClient minio.Client +} + +func (m *MediaClient) foo() string { + return "bar" +}