From 3891ea36ca0bd47e43808cd66f4771b4eb4bae96 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 4 Jul 2023 20:33:27 -0400 Subject: [PATCH 1/3] Ajouter MediaClient.Seed() Retirer MediaClient.foo() --- media/media.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/media/media.go b/media/media.go index 2d044f0..8f22e8f 100644 --- a/media/media.go +++ b/media/media.go @@ -1,6 +1,7 @@ package media import ( + "context" "errors" "git.agecem.com/agecem/agecem-org/config" @@ -49,6 +50,29 @@ type MediaClient struct { MinioClient minio.Client } -func (m *MediaClient) foo() string { - return "bar" +func (m *MediaClient) Seed() ([]string, error) { + var cfg config.Config + if err := viper.Unmarshal(&cfg); err != nil { + return nil, err + } + + var new_buckets []string + + for _, bucket := range cfg.Server.Documents.Buckets { + exists, err := m.MinioClient.BucketExists(context.Background(), bucket) + if err != nil { + return new_buckets, err + } + + if exists { + continue + } + + if err = m.MinioClient.MakeBucket(context.Background(), bucket, minio.MakeBucketOptions{}); err != nil { + return new_buckets, err + } + new_buckets = append(new_buckets, bucket) + } + + return new_buckets, nil } From 03cef363cd7e9fdc04ab4ae8b04173198908bc18 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 4 Jul 2023 20:35:13 -0400 Subject: [PATCH 2/3] Autoseed buckets pendant serverCmd --- cmd/server.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cmd/server.go b/cmd/server.go index e5d07f6..a574e02 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -46,6 +46,18 @@ var serverCmd = &cobra.Command{ log.Fatal(err) } + mediaClient, err := media.NewMediaClientFromViper() + if err != nil { + log.Fatal(err) + } + + new_buckets, err := mediaClient.Seed() + if err != nil { + log.Fatal(err) + } + + log.Printf("Seeded %d buckets.\n", len(new_buckets)) + RunServer() }, } From 046b14520b110d1c177dcfcb0859855855314198 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 4 Jul 2023 20:35:35 -0400 Subject: [PATCH 3/3] Utiliser MediaClient.Seed() dans handleV1Seed --- cmd/server.go | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index a574e02..4b21ec1 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -253,29 +253,14 @@ func handleV1Seed(c echo.Context) error { }) } - var new_buckets []string - - for _, bucket := range cfg.Server.Documents.Buckets { - exists, err := mediaClient.MinioClient.BucketExists(context.Background(), bucket) - if err != nil { - return c.JSON(http.StatusInternalServerError, map[string]string{ - "message": "Error during minio#BucketExists", - "error": err.Error(), - }) - } - - if exists { - continue - } - - 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(), - }) - } - new_buckets = append(new_buckets, bucket) + new_buckets, err := mediaClient.Seed() + if err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{ + "message": "Error during mediaClient.Seed()", + "error": err.Error(), + }) } + var message string if len(new_buckets) == 0 { message = "All buckets already exist"