From 0962ea5a2015f43de16ec548dd38aa569050d21f Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 24 Oct 2023 17:39:15 -0400 Subject: [PATCH 1/3] =?UTF-8?q?D=C3=A9placer=20api=20responses=20vers=20ap?= =?UTF-8?q?iresponse/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 ++ api/api.go | 8 ++--- api_handlers/api_handlers.go | 30 +++++++++---------- .../apiresponse.go | 18 ++++++++--- web_handlers/web_handlers.go | 13 ++++---- 5 files changed, 42 insertions(+), 29 deletions(-) rename models/responses.go => apiresponse/apiresponse.go (81%) diff --git a/Dockerfile b/Dockerfile index c8f609b..d2396ae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,6 +14,8 @@ ADD api/ api/ ADD api_handlers/ api_handlers/ +ADD apiresponse/ apiresponse/ + ADD config/ config/ ADD media/ media/ diff --git a/api/api.go b/api/api.go index d84be3c..9e8fbf7 100644 --- a/api/api.go +++ b/api/api.go @@ -10,8 +10,8 @@ import ( "net/url" "codeberg.org/vlbeaudoin/voki" + "git.agecem.com/agecem/agecem-org/apiresponse" "git.agecem.com/agecem/agecem-org/config" - "git.agecem.com/agecem/agecem-org/models" "github.com/spf13/viper" ) @@ -35,8 +35,8 @@ func New(client *http.Client, host, key string, port int, protocol string) (*API return &API{Voki: voki.New(client, host, key, port, protocol)}, nil } -func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (models.V1DocumentCreateResponse, error) { - var response models.V1DocumentCreateResponse +func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (apiresponse.V1DocumentCreateResponse, error) { + var response apiresponse.V1DocumentCreateResponse endpoint := fmt.Sprintf("%s://%s:%d", a.Voki.Protocol, a.Voki.Host, @@ -100,6 +100,6 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) ( return response, err } -func (a *API) ListBuckets() (response models.V1BucketListResponse, err error) { +func (a *API) ListBuckets() (response apiresponse.V1BucketListResponse, err error) { return response, a.Voki.Unmarshal(http.MethodGet, "/v1/bucket", nil, true, &response) } diff --git a/api_handlers/api_handlers.go b/api_handlers/api_handlers.go index 8ce859f..21ddb63 100644 --- a/api_handlers/api_handlers.go +++ b/api_handlers/api_handlers.go @@ -5,9 +5,9 @@ import ( "net/http" "sort" + "git.agecem.com/agecem/agecem-org/apiresponse" "git.agecem.com/agecem/agecem-org/config" "git.agecem.com/agecem/agecem-org/media" - "git.agecem.com/agecem/agecem-org/models" "github.com/labstack/echo/v4" "github.com/minio/minio-go/v7" ) @@ -30,7 +30,7 @@ func (h *V1Handler) 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 (h *V1Handler) HandleV1Seed(c echo.Context) error { - var response models.V1SeedResponse + var response apiresponse.V1SeedResponse new_buckets, err := h.MediaClient.Seed() response.Data.Buckets = new_buckets @@ -56,7 +56,7 @@ func (h *V1Handler) HandleV1Seed(c echo.Context) error { // HandleV1BucketList affiche les buckets permis par server.documents.buckets, qui existent. func (h *V1Handler) HandleV1BucketList(c echo.Context) error { - var response models.V1BucketListResponse + var response apiresponse.V1BucketListResponse var buckets = make(map[string]string) @@ -83,7 +83,7 @@ func (h *V1Handler) HandleV1BucketList(c echo.Context) error { } func (h *V1Handler) HandleV1BucketRead(c echo.Context) error { - var response models.V1BucketReadResponse + var response apiresponse.V1BucketReadResponse bucket := c.Param("bucket") @@ -95,7 +95,7 @@ func (h *V1Handler) HandleV1BucketRead(c echo.Context) error { } if !allowed { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } ctx, cancel := context.WithCancel(context.Background()) @@ -112,7 +112,7 @@ func (h *V1Handler) HandleV1BucketRead(c echo.Context) error { } if !exists { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } objectCh := h.MediaClient.MinioClient.ListObjects(ctx, bucket, minio.ListObjectsOptions{}) @@ -137,7 +137,7 @@ func (h *V1Handler) HandleV1BucketRead(c echo.Context) error { // HandleV1DocumentCreate permet d'ajouter un object dans un bucket, par multipart/form-data func (h *V1Handler) HandleV1DocumentCreate(c echo.Context) error { - var response models.V1DocumentCreateResponse + var response apiresponse.V1DocumentCreateResponse bucket := c.Param("bucket") @@ -158,7 +158,7 @@ func (h *V1Handler) HandleV1DocumentCreate(c echo.Context) error { } if !allowed { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } ctx, cancel := context.WithCancel(context.Background()) @@ -208,7 +208,7 @@ func (h *V1Handler) HandleV1DocumentRead(c echo.Context) error { } if !allowed { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } ctx, cancel := context.WithCancel(context.Background()) @@ -221,7 +221,7 @@ func (h *V1Handler) HandleV1DocumentRead(c echo.Context) error { } if !bucket_exists { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } document_info, err := h.MediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{}) @@ -229,7 +229,7 @@ func (h *V1Handler) HandleV1DocumentRead(c echo.Context) error { if err != nil { if err.Error() == "The specified key does not exist." { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } return c.JSON(http.StatusInternalServerError, map[string]interface{}{ @@ -253,7 +253,7 @@ func (h *V1Handler) HandleV1DocumentRead(c echo.Context) error { // HandleV1DocumentUpdate permet de mettre à jour certains champs d'un object, comme le Content-Type ou le Filename func (h *V1Handler) HandleV1DocumentUpdate(c echo.Context) error { - return c.JSON(models.NotImplementedResponse()) + return c.JSON(apiresponse.NotImplementedResponse()) } // HandleV1DocumentDelete permet de supprimer un object @@ -269,7 +269,7 @@ func (h *V1Handler) HandleV1DocumentDelete(c echo.Context) error { } if !allowed { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } ctx, cancel := context.WithCancel(context.Background()) @@ -282,14 +282,14 @@ func (h *V1Handler) HandleV1DocumentDelete(c echo.Context) error { } if !bucket_exists { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } document_info, err := h.MediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{}) if err != nil { if err.Error() == "The specified key does not exist." { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } return c.JSON(http.StatusInternalServerError, map[string]interface{}{ diff --git a/models/responses.go b/apiresponse/apiresponse.go similarity index 81% rename from models/responses.go rename to apiresponse/apiresponse.go index dd72936..71ca286 100644 --- a/models/responses.go +++ b/apiresponse/apiresponse.go @@ -1,6 +1,10 @@ -package models +package apiresponse -import "net/http" +import ( + "net/http" + + "git.agecem.com/agecem/agecem-org/models" +) type Responder interface { Respond() Responder @@ -36,17 +40,23 @@ func NotImplementedResponse() (int, SimpleResponse) { } } +func InternalServerErrorResponse() (int, SimpleResponse) { + return http.StatusInternalServerError, SimpleResponse{ + Message: "Internal Server Error", + } +} + type HandleAdminDocumentsUploadResponse struct { Response Data struct { - Buckets []Bucket + Buckets []models.Bucket } } type HandleDocumentationResponse struct { Response Data struct { - Buckets []Bucket + Buckets []models.Bucket } } diff --git a/web_handlers/web_handlers.go b/web_handlers/web_handlers.go index da02d10..8582c69 100644 --- a/web_handlers/web_handlers.go +++ b/web_handlers/web_handlers.go @@ -8,6 +8,7 @@ import ( "sort" "git.agecem.com/agecem/agecem-org/api" + "git.agecem.com/agecem/agecem-org/apiresponse" "git.agecem.com/agecem/agecem-org/models" "github.com/labstack/echo/v4" ) @@ -49,7 +50,7 @@ func HandleVieEtudianteOrganisme(c echo.Context) error { } func (h *WebHandler) HandleDocumentation(c echo.Context) error { - var response models.HandleDocumentationResponse + var response apiresponse.HandleDocumentationResponse v1BucketListResponse, err := h.ApiClient.ListBuckets() if err != nil { @@ -64,7 +65,7 @@ func (h *WebHandler) HandleDocumentation(c echo.Context) error { for bucket, displayName := range v1BucketListResponse.Data.Buckets { // TODO move call to dedicated API client method - var v1BucketReadResponse models.V1BucketReadResponse + var v1BucketReadResponse apiresponse.V1BucketReadResponse if err = h.ApiClient.Voki.Unmarshal(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket), nil, true, &v1BucketReadResponse); err != nil { response.Error = err.Error() @@ -106,13 +107,13 @@ func (h *WebHandler) HandlePublicDocumentation(c echo.Context) error { response, err := h.ApiClient.Voki.Call(http.MethodGet, unescaped, nil, true) if err != nil { - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) } defer response.Body.Close() switch response.StatusCode { case http.StatusNotFound: - return c.JSON(models.NotFoundResponse()) + return c.JSON(apiresponse.NotFoundResponse()) case http.StatusInternalServerError: return c.JSON(http.StatusInternalServerError, map[string]string{"message": "Internal Server Error"}) } @@ -130,7 +131,7 @@ func HandleAdmin(c echo.Context) error { } func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error { - var response models.HandleAdminDocumentsUploadResponse + var response apiresponse.HandleAdminDocumentsUploadResponse v1BucketListResponse, err := h.ApiClient.ListBuckets() if err != nil { @@ -153,7 +154,7 @@ func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error { } func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error { - var response models.HandleAdminDocumentsUploadResponse + var response apiresponse.HandleAdminDocumentsUploadResponse v1BucketListResponse, err := h.ApiClient.ListBuckets() if err != nil { From 206b45d41e827c87bea40b35acb7f0690e0c1679 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 24 Oct 2023 17:42:39 -0400 Subject: [PATCH 2/3] =?UTF-8?q?Split=20apiresponse=20en=20fichiers=20mieux?= =?UTF-8?q?=20nomm=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apiresponse/apiresponse.go | 30 ------------------------------ apiresponse/bucket.go | 15 +++++++++++++++ apiresponse/document.go | 10 ++++++++++ apiresponse/seed.go | 8 ++++++++ 4 files changed, 33 insertions(+), 30 deletions(-) create mode 100644 apiresponse/bucket.go create mode 100644 apiresponse/document.go create mode 100644 apiresponse/seed.go diff --git a/apiresponse/apiresponse.go b/apiresponse/apiresponse.go index 71ca286..6df6774 100644 --- a/apiresponse/apiresponse.go +++ b/apiresponse/apiresponse.go @@ -70,33 +70,3 @@ type UploadDocumentResponseData struct { Object string Size float64 } - -type V1SeedResponse struct { - Response - Data struct { - Buckets []string - } -} - -type V1BucketListResponse struct { - Response - Data struct { - Buckets map[string]string - } -} - -type V1BucketReadResponse struct { - Response - Data struct { - Keys []string - } -} - -type V1DocumentCreateResponse struct { - Response - Data struct { - Bucket string - Key string - Size int64 - } -} diff --git a/apiresponse/bucket.go b/apiresponse/bucket.go new file mode 100644 index 0000000..02c26a3 --- /dev/null +++ b/apiresponse/bucket.go @@ -0,0 +1,15 @@ +package apiresponse + +type V1BucketListResponse struct { + Response + Data struct { + Buckets map[string]string + } +} + +type V1BucketReadResponse struct { + Response + Data struct { + Keys []string + } +} diff --git a/apiresponse/document.go b/apiresponse/document.go new file mode 100644 index 0000000..9e1677b --- /dev/null +++ b/apiresponse/document.go @@ -0,0 +1,10 @@ +package apiresponse + +type V1DocumentCreateResponse struct { + Response + Data struct { + Bucket string + Key string + Size int64 + } +} diff --git a/apiresponse/seed.go b/apiresponse/seed.go new file mode 100644 index 0000000..858beab --- /dev/null +++ b/apiresponse/seed.go @@ -0,0 +1,8 @@ +package apiresponse + +type V1SeedResponse struct { + Response + Data struct { + Buckets []string + } +} From ce0f387ebd383c638486e3ec0fa03c3b006fac44 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 24 Oct 2023 17:55:07 -0400 Subject: [PATCH 3/3] =?UTF-8?q?Split=20r=C3=A9ponses=20webs=20de=20apiresp?= =?UTF-8?q?onse=20->=20webresponse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 ++ apiresponse/apiresponse.go | 27 --------------------------- web_handlers/web_handlers.go | 7 ++++--- webresponse/webresponse.go | 31 +++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 30 deletions(-) create mode 100644 webresponse/webresponse.go diff --git a/Dockerfile b/Dockerfile index d2396ae..73284c7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,6 +26,8 @@ ADD templates/ templates/ ADD web_handlers/ web_handlers/ +Add webresponse/ webresponse/ + RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o agecem-org . # Alpine diff --git a/apiresponse/apiresponse.go b/apiresponse/apiresponse.go index 6df6774..d4e98da 100644 --- a/apiresponse/apiresponse.go +++ b/apiresponse/apiresponse.go @@ -2,8 +2,6 @@ package apiresponse import ( "net/http" - - "git.agecem.com/agecem/agecem-org/models" ) type Responder interface { @@ -45,28 +43,3 @@ func InternalServerErrorResponse() (int, SimpleResponse) { Message: "Internal Server Error", } } - -type HandleAdminDocumentsUploadResponse struct { - Response - Data struct { - Buckets []models.Bucket - } -} - -type HandleDocumentationResponse struct { - Response - Data struct { - Buckets []models.Bucket - } -} - -type UploadDocumentResponse struct { - Response - Data UploadDocumentResponseData -} - -type UploadDocumentResponseData struct { - Bucket string - Object string - Size float64 -} diff --git a/web_handlers/web_handlers.go b/web_handlers/web_handlers.go index 8582c69..293666e 100644 --- a/web_handlers/web_handlers.go +++ b/web_handlers/web_handlers.go @@ -10,6 +10,7 @@ import ( "git.agecem.com/agecem/agecem-org/api" "git.agecem.com/agecem/agecem-org/apiresponse" "git.agecem.com/agecem/agecem-org/models" + "git.agecem.com/agecem/agecem-org/webresponse" "github.com/labstack/echo/v4" ) @@ -50,7 +51,7 @@ func HandleVieEtudianteOrganisme(c echo.Context) error { } func (h *WebHandler) HandleDocumentation(c echo.Context) error { - var response apiresponse.HandleDocumentationResponse + var response webresponse.HandleDocumentationResponse v1BucketListResponse, err := h.ApiClient.ListBuckets() if err != nil { @@ -131,7 +132,7 @@ func HandleAdmin(c echo.Context) error { } func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error { - var response apiresponse.HandleAdminDocumentsUploadResponse + var response webresponse.HandleAdminDocumentsUploadResponse v1BucketListResponse, err := h.ApiClient.ListBuckets() if err != nil { @@ -154,7 +155,7 @@ func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error { } func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error { - var response apiresponse.HandleAdminDocumentsUploadResponse + var response webresponse.HandleAdminDocumentsUploadResponse v1BucketListResponse, err := h.ApiClient.ListBuckets() if err != nil { diff --git a/webresponse/webresponse.go b/webresponse/webresponse.go new file mode 100644 index 0000000..b17b463 --- /dev/null +++ b/webresponse/webresponse.go @@ -0,0 +1,31 @@ +package webresponse + +import ( + "git.agecem.com/agecem/agecem-org/apiresponse" + "git.agecem.com/agecem/agecem-org/models" +) + +type HandleAdminDocumentsUploadResponse struct { + apiresponse.Response + Data struct { + Buckets []models.Bucket + } +} + +type HandleDocumentationResponse struct { + apiresponse.Response + Data struct { + Buckets []models.Bucket + } +} + +type UploadDocumentResponse struct { + apiresponse.Response + Data UploadDocumentResponseData +} + +type UploadDocumentResponseData struct { + Bucket string + Object string + Size float64 +}