From 02a65f2c470194e434b6cf1e1faf8994422c79ef Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 20 Nov 2023 16:14:22 -0500 Subject: [PATCH] feat: start paving bucket operations --- apihandler/bucket.go | 25 ++++++++++++++++--- apirequest/bucket.go | 58 ++++++++++++++++++++++++++++++++++++++++++++ cmd/server.go | 12 +++++++-- 3 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 apirequest/bucket.go diff --git a/apihandler/bucket.go b/apihandler/bucket.go index 26ea6d1..04818f6 100644 --- a/apihandler/bucket.go +++ b/apihandler/bucket.go @@ -4,6 +4,7 @@ import ( "context" "net/http" + "git.agecem.com/agecem/agecem-org/apirequest" "git.agecem.com/agecem/agecem-org/apiresponse" "github.com/labstack/echo/v4" "github.com/minio/minio-go/v7" @@ -11,8 +12,16 @@ import ( // V1BucketsGET affiche les buckets permis par server.documents.buckets, qui existent. func (h *V1Handler) V1BucketsGET(c echo.Context) error { + var request apirequest.V1BucketsGET var response apiresponse.V1BucketsGET + if !request.Complete() { + response.Message = "Incomplete V1BucketsGET request received" + response.StatusCode = http.StatusBadRequest + + return c.JSON(response.StatusCode, response) + } + var buckets = make(map[string]string) for bucket_name, bucket_display_name := range h.Config.Server.Documents.Buckets { @@ -38,13 +47,21 @@ func (h *V1Handler) V1BucketsGET(c echo.Context) error { } func (h *V1Handler) V1BucketGET(c echo.Context) error { + var request apirequest.V1BucketGET var response apiresponse.V1BucketGET - bucket := c.Param("bucket") + request.Params.Bucket = c.Param("bucket") + + if !request.Complete() { + response.Message = "Incomplete V1BucketGET request received" + response.StatusCode = http.StatusBadRequest + + return c.JSON(response.StatusCode, response) + } allowed := false for bucket_allowed := range h.Config.Server.Documents.Buckets { - if bucket == bucket_allowed { + if request.Params.Bucket == bucket_allowed { allowed = true } } @@ -57,7 +74,7 @@ func (h *V1Handler) V1BucketGET(c echo.Context) error { defer cancel() - exists, err := h.MediaClient.MinioClient.BucketExists(ctx, bucket) + exists, err := h.MediaClient.MinioClient.BucketExists(ctx, request.Params.Bucket) if err != nil { response.StatusCode = http.StatusInternalServerError response.Message = "Error during minio#BucketExists" @@ -70,7 +87,7 @@ func (h *V1Handler) V1BucketGET(c echo.Context) error { return c.JSON(apiresponse.NotFoundResponse()) } - objectCh := h.MediaClient.MinioClient.ListObjects(ctx, bucket, minio.ListObjectsOptions{}) + objectCh := h.MediaClient.MinioClient.ListObjects(ctx, request.Params.Bucket, minio.ListObjectsOptions{}) for object := range objectCh { if object.Err != nil { response.StatusCode = http.StatusInternalServerError diff --git a/apirequest/bucket.go b/apirequest/bucket.go new file mode 100644 index 0000000..e4ba60a --- /dev/null +++ b/apirequest/bucket.go @@ -0,0 +1,58 @@ +package apirequest + +import ( + "fmt" + "net/http" + + "codeberg.org/vlbeaudoin/voki" + "codeberg.org/vlbeaudoin/voki/request" + "git.agecem.com/agecem/agecem-org/apiresponse" +) + +var _ request.Requester[apiresponse.V1BucketsGET] = V1BucketsGET{} + +type V1BucketsGET struct{} + +func NewV1BucketsGET() (request V1BucketsGET, err error) { + return +} + +func (request V1BucketsGET) Complete() bool { return true } + +func (request V1BucketsGET) Request(v *voki.Voki) (response apiresponse.V1BucketsGET, err error) { + if !request.Complete() { + err = fmt.Errorf("Incomplete V1BucketsGET request") + return + } + + return response, v.UnmarshalIfComplete(http.MethodGet, "/v1/bucket", nil, true, &response) +} + +var _ request.Requester[apiresponse.V1BucketGET] = V1BucketGET{} + +type V1BucketGET struct { + Params struct { + Bucket string `json:"bucket"` + } +} + +func NewV1BucketGET(bucket string) (request V1BucketGET, err error) { + if bucket == "" { + err = fmt.Errorf("NewV1BucketGET requires non-nil bucket name") + } + + request.Params.Bucket = bucket + + return +} + +func (request V1BucketGET) Complete() bool { return request.Params.Bucket != "" } + +func (request V1BucketGET) Request(v *voki.Voki) (response apiresponse.V1BucketGET, err error) { + if !request.Complete() { + err = fmt.Errorf("Incomplete V1BucketGET request") + return + } + + return response, v.UnmarshalIfComplete(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", request.Params.Bucket), nil, true, &response) +} diff --git a/cmd/server.go b/cmd/server.go index af1d138..bd17f2b 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -230,9 +230,17 @@ func RunServer() { log.Fatal(err) } - groupV1.GET("/bucket", v1Handler.V1BucketsGET) + if err := pave.EchoRegister[ + apirequest.V1BucketsGET, + apiresponse.V1BucketsGET](groupV1, &p, "/v1", http.MethodGet, "/bucket", "List buckets", "V1BucketsGET", v1Handler.V1BucketsGET); err != nil { + log.Fatal(err) + } - groupV1.GET("/bucket/:bucket", v1Handler.V1BucketGET) + if err := pave.EchoRegister[ + apirequest.V1BucketGET, + apiresponse.V1BucketGET](groupV1, &p, "/v1", http.MethodGet, "/bucket/:bucket", "Read bucket content", "V1BucketGET", v1Handler.V1BucketGET); err != nil { + log.Fatal(err) + } groupV1.POST("/bucket/:bucket", v1Handler.V1DocumentPOST)