From 098666289cf69456ba4e417a57467adc4d01c1a1 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Wed, 30 Aug 2023 15:24:37 -0400 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9menter=20web=5Fhandlers.WebHandler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api_handlers/api_handlers.go | 2 +- cmd/server.go | 21 +++++--- web_handlers/web_handlers.go | 94 ++++++------------------------------ 3 files changed, 30 insertions(+), 87 deletions(-) diff --git a/api_handlers/api_handlers.go b/api_handlers/api_handlers.go index 11b0fab..8ce859f 100644 --- a/api_handlers/api_handlers.go +++ b/api_handlers/api_handlers.go @@ -252,7 +252,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 HandleV1DocumentUpdate(c echo.Context) error { +func (h *V1Handler) HandleV1DocumentUpdate(c echo.Context) error { return c.JSON(models.NotImplementedResponse()) } diff --git a/cmd/server.go b/cmd/server.go index f505ebf..1149cb3 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -16,6 +16,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "git.agecem.com/agecem/agecem-org/api" "git.agecem.com/agecem/agecem-org/api_handlers" "git.agecem.com/agecem/agecem-org/config" "git.agecem.com/agecem/agecem-org/media" @@ -207,7 +208,7 @@ func RunServer() { MediaClient: mediaClient, } - groupV1.GET("", api_handlers.HandleV1) + groupV1.GET("", v1Handler.HandleV1) groupV1.POST("/seed", v1Handler.HandleV1Seed) @@ -219,11 +220,19 @@ func RunServer() { groupV1.GET("/bucket/:bucket/:document", v1Handler.HandleV1DocumentRead) - groupV1.PUT("/bucket/:bucket/:document", api_handlers.HandleV1DocumentUpdate) + groupV1.PUT("/bucket/:bucket/:document", v1Handler.HandleV1DocumentUpdate) groupV1.DELETE("/bucket/:bucket/:document", v1Handler.HandleV1DocumentDelete) // HTML Routes + apiClient, err := api.NewApiClientFromViper() + if err != nil { + log.Fatal("Error during NewMediaClientFromViper for API handlers") + } + + webHandler := web_handlers.WebHandler{ + ApiClient: apiClient, + } e.GET("/", web_handlers.HandleIndex) @@ -237,21 +246,21 @@ func RunServer() { e.GET("/vie-etudiante/:organisme", web_handlers.HandleVieEtudianteOrganisme) - e.GET("/documentation", web_handlers.HandleDocumentation) + e.GET("/documentation", webHandler.HandleDocumentation) e.GET("/formulaires", web_handlers.HandleFormulaires) // Public Routes - e.GET("/public/documentation/:bucket/:document", web_handlers.HandlePublicDocumentation) + e.GET("/public/documentation/:bucket/:document", webHandler.HandlePublicDocumentation) // Admin Routes groupAdmin.GET("", web_handlers.HandleAdmin) - groupAdmin.GET("/documents/upload", web_handlers.HandleAdminDocumentsUpload) + groupAdmin.GET("/documents/upload", webHandler.HandleAdminDocumentsUpload) - groupAdmin.POST("/documents/upload", web_handlers.HandleAdminDocumentsUploadPOST) + groupAdmin.POST("/documents/upload", webHandler.HandleAdminDocumentsUploadPOST) e.Logger.Fatal(e.Start( fmt.Sprintf(":%d", cfg.Server.Port))) diff --git a/web_handlers/web_handlers.go b/web_handlers/web_handlers.go index de19abd..1dd96e9 100644 --- a/web_handlers/web_handlers.go +++ b/web_handlers/web_handlers.go @@ -11,6 +11,10 @@ import ( "github.com/labstack/echo/v4" ) +type WebHandler struct { + ApiClient *api.API +} + func HandleIndex(c echo.Context) error { return c.Render(http.StatusOK, "index-html", nil) } @@ -43,19 +47,10 @@ func HandleVieEtudianteOrganisme(c echo.Context) error { return c.String(http.StatusOK, fmt.Sprintf("Organisme: %s", organisme)) } -func HandleDocumentation(c echo.Context) error { +func (h *WebHandler) HandleDocumentation(c echo.Context) error { var response models.HandleDocumentationResponse - client, err := api.NewApiClientFromViper() - if err != nil { - response.StatusCode = http.StatusInternalServerError - response.Message = "Error during api.NewApiClientFromViper()" - response.Error = err.Error() - - return c.Render(response.StatusCode, "documentation-html", response) - } - - v1BucketListResponse, err := client.ListBuckets() + v1BucketListResponse, err := h.ApiClient.ListBuckets() if err != nil { response.StatusCode = v1BucketListResponse.StatusCode response.Message = v1BucketListResponse.Message @@ -68,7 +63,7 @@ func HandleDocumentation(c echo.Context) error { for bucket, displayName := range v1BucketListResponse.Data.Buckets { // TODO move call to dedicated API client method - content, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket)) + content, err := h.ApiClient.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket)) if err != nil { response.StatusCode = http.StatusInternalServerError response.Message = "Error during /v1/bucket/:bucket" @@ -88,24 +83,6 @@ func HandleDocumentation(c echo.Context) error { return c.Render(response.StatusCode, "documentation-html", response) } - // Ce bloc retire tous les caractères spéciaux d'une string - // N'est pas présentement activé, car les fichiers sont processed - // à la création de toute façon. - /* - reg, err := regexp.Compile("[^.a-zA-Z0-9_-]+") - if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-html", nil) - } - - var documents_processed []string - - for _, document := range documents { - document_processed := reg.ReplaceAllString(document, "") - documents_processed = append(documents_processed, document_processed) - } - documents_processed := documents - */ - response.Data.Buckets = append(response.Data.Buckets, models.Bucket{ Name: bucket, DisplayName: displayName, @@ -127,16 +104,11 @@ func HandleFormulaires(c echo.Context) error { return c.Render(http.StatusOK, "formulaires-html", nil) } -func HandlePublicDocumentation(c echo.Context) error { - client, err := api.NewApiClientFromViper() - if err != nil { - return c.JSON(models.NotFoundResponse()) - } - +func (h *WebHandler) HandlePublicDocumentation(c echo.Context) error { bucket := c.Param("bucket") document := c.Param("document") - result, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s/%s", bucket, document)) + result, err := h.ApiClient.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s/%s", bucket, document)) if err != nil { return c.JSON(models.NotFoundResponse()) } @@ -156,17 +128,10 @@ func HandleAdmin(c echo.Context) error { return c.Render(http.StatusOK, "admin-html", nil) } -func HandleAdminDocumentsUpload(c echo.Context) error { +func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error { var response models.HandleAdminDocumentsUploadResponse - client, err := api.NewApiClientFromViper() - if err != nil { - response.StatusCode = http.StatusInternalServerError - response.Error = err.Error() - return c.Render(response.StatusCode, "admin-upload-html", nil) - } - - v1BucketListResponse, err := client.ListBuckets() + v1BucketListResponse, err := h.ApiClient.ListBuckets() if err != nil { response.StatusCode = v1BucketListResponse.StatusCode response.Error = err.Error() @@ -186,26 +151,10 @@ func HandleAdminDocumentsUpload(c echo.Context) error { return c.Render(response.StatusCode, "admin-upload-html", response) } -func HandleAdminDocumentsUploadPOST(c echo.Context) error { - /* - var data struct { - Buckets []models.Bucket - Message string - } - */ - +func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error { var response models.HandleAdminDocumentsUploadResponse - client, err := api.NewApiClientFromViper() - if err != nil { - response.StatusCode = http.StatusInternalServerError - response.Message = "Error during api.newApiClientFromViper()" - response.Error = err.Error() - - return c.Render(response.StatusCode, "admin-upload-html", response) - } - - v1BucketListResponse, err := client.ListBuckets() + v1BucketListResponse, err := h.ApiClient.ListBuckets() if err != nil { response.StatusCode = v1BucketListResponse.StatusCode response.Message = v1BucketListResponse.Message @@ -214,21 +163,6 @@ func HandleAdminDocumentsUploadPOST(c echo.Context) error { return c.Render(response.StatusCode, "admin-upload-html", response) } - /* - 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) - } - - var buckets map[string]string - - 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, @@ -247,7 +181,7 @@ func HandleAdminDocumentsUploadPOST(c echo.Context) error { return c.Render(response.StatusCode, "admin-upload-html", response) } - uploadDocumentResponse, err := client.UploadDocument(bucket, document) + uploadDocumentResponse, err := h.ApiClient.UploadDocument(bucket, document) if err != nil { response.StatusCode = uploadDocumentResponse.StatusCode response.Message = uploadDocumentResponse.Message