Ajouter media/ pour abstraire le client minio

Ajouter media/ à Dockerfile

Migrer serverCmd#HandleV1Seed à utiliser media#NewMediaClientFromViper()
This commit is contained in:
Victor Lacasse-Beaudoin 2023-07-04 16:47:03 -04:00
parent fdc2c23096
commit 4f5b451042
3 changed files with 51 additions and 8 deletions

View file

@ -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

View file

@ -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(),

44
media/media.go Normal file
View file

@ -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"
}