From c8af2acae917c1be2e688374eddede16d239569c Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sun, 20 Aug 2023 17:56:49 -0400 Subject: [PATCH 1/4] Ajouter responses pour certaines routes --- models/responses.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/models/responses.go b/models/responses.go index 815df1d..651b527 100644 --- a/models/responses.go +++ b/models/responses.go @@ -14,6 +14,20 @@ func (r Response) Respond() Responder { return r } +type HandleAdminDocumentsUploadResponse struct { + Response + Data struct { + Buckets []Bucket + } +} + +type HandleDocumentationResponse struct { + Response + Data struct { + Buckets []Bucket + } +} + type UploadDocumentResponse struct { Response Data UploadDocumentResponseData @@ -31,3 +45,10 @@ type V1SeedResponse struct { Buckets []string } } + +type V1BucketListResponse struct { + Response + Data struct { + Buckets map[string]string + } +} -- 2.45.2 From d736d53a43650f7f7940a9ac722ac6945e13a629 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sun, 20 Aug 2023 17:57:14 -0400 Subject: [PATCH 2/4] =?UTF-8?q?Impl=C3=A9menter=20HandleV1BucketListRespon?= =?UTF-8?q?se?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api_handlers/api_handlers.go | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/api_handlers/api_handlers.go b/api_handlers/api_handlers.go index 3b8537b..c6ba7d6 100644 --- a/api_handlers/api_handlers.go +++ b/api_handlers/api_handlers.go @@ -61,19 +61,24 @@ func HandleV1Seed(c echo.Context) error { // HandleV1BucketList affiche les buckets permis par server.documents.buckets, qui existent. func HandleV1BucketList(c echo.Context) error { + var response models.V1BucketListResponse + var cfg config.Config if err := viper.Unmarshal(&cfg); err != nil { - return c.JSON(http.StatusInternalServerError, map[string]string{ - "error": err.Error(), - }) + response.StatusCode = http.StatusInternalServerError + response.Message = "Error during viper.Unmarshal" + // response.Error = err.Error() + + return c.JSON(response.StatusCode, response) } mediaClient, err := media.NewMediaClientFromViper() if err != nil { - return c.JSON(http.StatusInternalServerError, map[string]string{ - "message": "Error during media.NewMediaClientFromViper()", - "error": err.Error(), - }) + response.StatusCode = http.StatusInternalServerError + response.Message = "Error during media.NewMediaClientFromViper()" + // response.Error = err.Error() + + return c.JSON(response.StatusCode, response) } var buckets = make(map[string]string) @@ -81,7 +86,11 @@ func HandleV1BucketList(c echo.Context) error { for bucket_name, bucket_display_name := range cfg.Server.Documents.Buckets { exists, err := mediaClient.MinioClient.BucketExists(context.Background(), bucket_name) if err != nil { - return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists") + response.StatusCode = http.StatusInternalServerError + response.Message = "Error during minio#BucketExists" + // response.Error = err.Error() + + return c.JSON(response.StatusCode, response) } if exists { @@ -89,7 +98,12 @@ func HandleV1BucketList(c echo.Context) error { } } - return c.JSON(http.StatusOK, buckets) + response.StatusCode = http.StatusOK + response.Message = "Buckets list successful" + response.Data.Buckets = buckets + + return c.JSON(response.StatusCode, response) + //return c.JSON(response.StatusCode, response.Data.Buckets) } func HandleV1BucketRead(c echo.Context) error { -- 2.45.2 From 9ebf27dbaf6c81cbfa1f82ff72dd5502fdd97efe Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sun, 20 Aug 2023 17:57:36 -0400 Subject: [PATCH 3/4] Ajouter API.ListBuckets() --- api/api.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/api/api.go b/api/api.go index 310bbdc..f70d2bf 100644 --- a/api/api.go +++ b/api/api.go @@ -271,3 +271,17 @@ func (a *API) CallWithData(method, route string, data []byte) (string, error) { //return "", errors.New(fmt.Sprintf("method must be 'POST' or 'PUT', got '%s'", method)) return "", errors.New(fmt.Sprintf("method must be 'POST', got '%s'", method)) } + +func (a *API) ListBuckets() (models.V1BucketListResponse, error) { + var response models.V1BucketListResponse + result, err := a.Call(http.MethodGet, "/v1/bucket") + if err != nil { + return response, err + } + + if err = json.Unmarshal(result, &response); err != nil { + return response, err + } + + return response, nil +} -- 2.45.2 From 3ce7ac53ca3ca9c645663711ce8c6cbd0adb7b55 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sun, 20 Aug 2023 18:03:45 -0400 Subject: [PATCH 4/4] Fix web_handlers avec nouvelles responses --- templates/html/admin-upload.gohtml | 2 +- web_handlers/web_handlers.go | 140 +++++++++++++++++------------ 2 files changed, 84 insertions(+), 58 deletions(-) diff --git a/templates/html/admin-upload.gohtml b/templates/html/admin-upload.gohtml index c7afd53..f7f7440 100644 --- a/templates/html/admin-upload.gohtml +++ b/templates/html/admin-upload.gohtml @@ -12,7 +12,7 @@
diff --git a/web_handlers/web_handlers.go b/web_handlers/web_handlers.go index e9639cd..dc45dfb 100644 --- a/web_handlers/web_handlers.go +++ b/web_handlers/web_handlers.go @@ -44,26 +44,27 @@ func HandleVieEtudianteOrganisme(c echo.Context) error { } func HandleDocumentation(c echo.Context) error { + var response models.HandleDocumentationResponse + client, err := api.NewApiClientFromViper() if err != nil { return c.Render(http.StatusInternalServerError, "documentation-html", nil) } - result, err := client.Call(http.MethodGet, "/v1/bucket") + v1BucketListResponse, err := client.ListBuckets() if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-html", nil) + response.StatusCode = v1BucketListResponse.StatusCode + response.Message = v1BucketListResponse.Message + response.Error = err.Error() + + return c.Render(response.StatusCode, "documentation-html", response) } - var buckets map[string]string + //TODO check v1BucketListRespone StatusCode and Error - err = json.Unmarshal(result, &buckets) - if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-html", nil) - } - - var data []models.Bucket - - for bucket, displayName := range buckets { + for bucket, displayName := range v1BucketListResponse.Data.Buckets { + // TODO move call to dedicated API client method + // TODO add Response type content, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket)) if err != nil { return c.Render(http.StatusInternalServerError, "documentation-html", nil) @@ -94,16 +95,19 @@ func HandleDocumentation(c echo.Context) error { documents_processed := documents */ - data = append(data, models.Bucket{ + response.Data.Buckets = append(response.Data.Buckets, models.Bucket{ Name: bucket, DisplayName: displayName, Documents: documents, }) } - sort.SliceStable(data, func(i, j int) bool { return data[i].Name < data[j].Name }) + sort.SliceStable(response.Data.Buckets, func(i, j int) bool { return response.Data.Buckets[i].Name < response.Data.Buckets[j].Name }) - return c.Render(http.StatusOK, "documentation-html", data) + response.StatusCode = http.StatusOK + + // TODO render .Message + return c.Render(response.StatusCode, "documentation-html", response.Data.Buckets) } func HandleFormulaires(c echo.Context) error { @@ -140,65 +144,80 @@ func HandleAdmin(c echo.Context) error { } func HandleAdminDocumentsUpload(c echo.Context) error { + var response models.HandleAdminDocumentsUploadResponse client, err := api.NewApiClientFromViper() if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-html", nil) + response.StatusCode = http.StatusInternalServerError + response.Error = err.Error() + + return c.Render(response.StatusCode, "admin-upload-html", nil) } - result, err := client.Call(http.MethodGet, "/v1/bucket") + v1BucketListResponse, err := client.ListBuckets() if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-html", nil) + response.StatusCode = v1BucketListResponse.StatusCode + response.Error = err.Error() + response.Message = v1BucketListResponse.Message + + return c.Render(response.StatusCode, "admin-upload-html", nil) } - var buckets map[string]string - - err = json.Unmarshal(result, &buckets) - if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-html", nil) - } - - var data struct { - Buckets []models.Bucket - Message string - } - - for bucketName, displayName := range buckets { - data.Buckets = append(data.Buckets, models.Bucket{ + for bucketName, displayName := range v1BucketListResponse.Data.Buckets { + response.Data.Buckets = append(response.Data.Buckets, models.Bucket{ Name: bucketName, DisplayName: displayName, }) } + response.StatusCode = http.StatusOK - return c.Render(http.StatusOK, "admin-upload-html", data) + return c.Render(response.StatusCode, "admin-upload-html", response) } func HandleAdminDocumentsUploadPOST(c echo.Context) error { - var data struct { - Buckets []models.Bucket - Message string - } + /* + var data struct { + Buckets []models.Bucket + Message string + } + */ + + var response models.HandleAdminDocumentsUploadResponse client, err := api.NewApiClientFromViper() if err != nil { - data.Message = fmt.Sprintf("HandleAdminDocumentsUploadPOST#api.New: %s", err) - return c.Render(http.StatusInternalServerError, "admin-upload-html", data) + response.StatusCode = http.StatusInternalServerError + response.Message = "Error during api.newApiClientFromViper()" + response.Error = err.Error() + + return c.Render(response.StatusCode, "admin-upload-html", response) } - result, err := client.Call(http.MethodGet, "/v1/bucket") + v1BucketListResponse, err := client.ListBuckets() if err != nil { - data.Message = "Error during GET /v1/bucket" - return c.Render(http.StatusInternalServerError, "documentation-html", data) + response.StatusCode = v1BucketListResponse.StatusCode + response.Message = v1BucketListResponse.Message + response.Error = err.Error() + + return c.Render(response.StatusCode, "admin-upload-html", response) } - var buckets map[string]string + /* + result, err := client.Call(http.MethodGet, "/v1/bucket") + if err != nil { + data.Message = "Error during GET /v1/bucket" + return c.Render(http.StatusInternalServerError, "admin-upload-html", data) + } - err = json.Unmarshal(result, &buckets) - if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-html", nil) - } + var buckets map[string]string - for bucketName, displayName := range buckets { - data.Buckets = append(data.Buckets, models.Bucket{ + err = json.Unmarshal(result, &buckets) + if err != nil { + return c.Render(http.StatusInternalServerError, "admin-upload-html", nil) + } + */ + + for bucketName, displayName := range v1BucketListResponse.Data.Buckets { + response.Data.Buckets = append(response.Data.Buckets, models.Bucket{ Name: bucketName, DisplayName: displayName, }) @@ -208,24 +227,31 @@ func HandleAdminDocumentsUploadPOST(c echo.Context) error { document, err := c.FormFile("document") if err != nil { - data.Message = fmt.Sprintf("HandleAdminDocumentsUploadPOST#c.FormFile: %s", err) - return c.Render(http.StatusBadRequest, "admin-upload-html", data) + response.StatusCode = http.StatusBadRequest + response.Message = "Formulaire invalide" + response.Error = err.Error() + + return c.Render(response.StatusCode, "admin-upload-html", response) } - response, err := client.UploadDocument(bucket, document) + uploadDocumentResponse, err := client.UploadDocument(bucket, document) if err != nil { - data.Message = fmt.Sprintf("HandleAdminDocumentsUploadPOST#client.UploadDocument: %s", err) - return c.Render(http.StatusInternalServerError, "admin-upload-html", data) + response.StatusCode = uploadDocumentResponse.StatusCode + response.Message = uploadDocumentResponse.Message + response.Error = err.Error() + + return c.Render(response.StatusCode, "admin-upload-html", response) } // Format response var info, status string - info = fmt.Sprintf("[%.0f] /public/documentation/%s/%s", response.Data.Size, response.Data.Bucket, response.Data.Object) + info = fmt.Sprintf("[%.0f] /public/documentation/%s/%s", uploadDocumentResponse.Data.Size, uploadDocumentResponse.Data.Bucket, uploadDocumentResponse.Data.Object) - status = response.Message + status = uploadDocumentResponse.Message - data.Message = fmt.Sprintf("%s - %s", status, info) + response.StatusCode = http.StatusOK + response.Message = fmt.Sprintf("%s - %s", status, info) - return c.Render(http.StatusOK, "admin-upload-html", data) + return c.Render(response.StatusCode, "admin-upload-html", response) } -- 2.45.2