feat: start paving bucket operations

This commit is contained in:
Victor Lacasse-Beaudoin 2023-11-20 16:14:22 -05:00
parent d70b0643e8
commit 02a65f2c47
3 changed files with 89 additions and 6 deletions

View file

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

58
apirequest/bucket.go Normal file
View file

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

View file

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