From a4b56214b2307f1428ad2d20c62cb9964ce4ad80 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 19 Aug 2023 14:45:07 -0400 Subject: [PATCH 01/16] Documenter NewApiClientFromViper --- api/api.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/api/api.go b/api/api.go index 42211b6..c6608a3 100644 --- a/api/api.go +++ b/api/api.go @@ -41,6 +41,9 @@ type UploadDocumentResponseInfo struct { Size float64 `json:"size"` } +// NewApiClientFromViper returns a pointer to a new API object, +// provided the configuration options are managed by +// https://git.agecem.com/agecem/agecem-org/config func NewApiClientFromViper() (*API, error) { var config config.Config From 0f60e58ec21abb15d20be9bbd4c3ccb8cc03baeb Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 19 Aug 2023 14:48:08 -0400 Subject: [PATCH 02/16] Ajouter models/ pour type Bucket MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit models.Bucket est utilisé dans cmd/server pour contenir les données relatives à la documentation dans certaines routes html --- Dockerfile | 2 ++ cmd/server.go | 29 ++++++----------------------- models/models.go | 7 +++++++ 3 files changed, 15 insertions(+), 23 deletions(-) create mode 100644 models/models.go diff --git a/Dockerfile b/Dockerfile index 7497889..a53152d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,6 +16,8 @@ ADD config/ config/ ADD media/ media/ +ADD models/ models/ + ADD templates/ templates/ ADD serverhandlers/ serverhandlers/ diff --git a/cmd/server.go b/cmd/server.go index e37b48f..8ba30e8 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -21,6 +21,7 @@ import ( "git.agecem.com/agecem/agecem-org/api" "git.agecem.com/agecem/agecem-org/config" "git.agecem.com/agecem/agecem-org/media" + "git.agecem.com/agecem/agecem-org/models" "git.agecem.com/agecem/agecem-org/public" "git.agecem.com/agecem/agecem-org/serverhandlers" "git.agecem.com/agecem/agecem-org/templates" @@ -306,13 +307,7 @@ func handleDocumentation(c echo.Context) error { return c.Render(http.StatusInternalServerError, "documentation-html", nil) } - type Bucket struct { - Name string - DisplayName string - Documents []string - } - - var data []Bucket + var data []models.Bucket for bucket, displayName := range buckets { content, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket)) @@ -345,7 +340,7 @@ func handleDocumentation(c echo.Context) error { documents_processed := documents */ - data = append(data, Bucket{ + data = append(data, models.Bucket{ Name: bucket, DisplayName: displayName, Documents: documents, @@ -408,19 +403,13 @@ func handleAdminDocumentsUpload(c echo.Context) error { return c.Render(http.StatusInternalServerError, "documentation-html", nil) } - type Bucket struct { - Name string - DisplayName string - Documents []string - } - var data struct { - Buckets []Bucket + Buckets []models.Bucket Message string } for bucketName, displayName := range buckets { - data.Buckets = append(data.Buckets, Bucket{ + data.Buckets = append(data.Buckets, models.Bucket{ Name: bucketName, DisplayName: displayName, }) @@ -430,14 +419,8 @@ func handleAdminDocumentsUpload(c echo.Context) error { } func handleAdminDocumentsUploadPOST(c echo.Context) error { - type Bucket struct { - Name string - DisplayName string - Documents []string - } - var data struct { - Buckets []Bucket + Buckets []models.Bucket Message string } diff --git a/models/models.go b/models/models.go new file mode 100644 index 0000000..9ef3c9e --- /dev/null +++ b/models/models.go @@ -0,0 +1,7 @@ +package models + +type Bucket struct { + Name string + DisplayName string + Documents []string +} From 25aaad42b6c411c06f4704600db0df814db52490 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 19 Aug 2023 15:23:10 -0400 Subject: [PATCH 03/16] Utiliser api.NewApiClientFromViper --- cmd/server.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index e37b48f..2b36000 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -441,13 +441,7 @@ func handleAdminDocumentsUploadPOST(c echo.Context) error { Message string } - client, err := api.New(cfg.Server.Api.Protocol, cfg.Server.Api.Host, cfg.Server.Port, api.APIOptions{ - KeyAuth: cfg.Server.Api.Auth, - Key: cfg.Server.Api.Key, - BasicAuth: cfg.Server.Admin.Auth, - Username: cfg.Server.Admin.Username, - Password: cfg.Server.Admin.Password, - }) + 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) From 73b5ce1bc2dc3f3ebf67a167e90dd05595a5e1a7 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 19 Aug 2023 15:35:56 -0400 Subject: [PATCH 04/16] =?UTF-8?q?Ajouter=20error=20handling=20=C3=A0=20api?= =?UTF-8?q?.API#UploadDocument?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Créer moins d'objets UploadDocumentResponse --- api/api.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/api/api.go b/api/api.go index c6608a3..419ad41 100644 --- a/api/api.go +++ b/api/api.go @@ -150,6 +150,7 @@ func (a *API) Call(method, route string) ([]byte, error) { } func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (UploadDocumentResponse, error) { + var response UploadDocumentResponse endpoint := fmt.Sprintf("%s://%s:%d", a.Protocol, a.Host, @@ -165,34 +166,34 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) ( // Add the file to the request file, err := file_header.Open() if err != nil { - return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#file_header.Open: %s", err) + return response, fmt.Errorf("UploadDocument#file_header.Open: %s", err) } defer file.Close() filename_processed, err := url.QueryUnescape(file_header.Filename) if err != nil { - return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#url.QueryUnescape: %s", err) + return response, fmt.Errorf("UploadDocument#url.QueryUnescape: %s", err) } part, err := writer.CreateFormFile("document", filename_processed) if err != nil { - return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err) + return response, fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err) } _, err = io.Copy(part, file) if err != nil { - return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#io.Copy: %s", err) + return response, fmt.Errorf("UploadDocument#io.Copy: %s", err) } err = writer.Close() if err != nil { - return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#writer.Close: %s", err) + return response, fmt.Errorf("UploadDocument#writer.Close: %s", err) } // Create a new HTTP request with the multipart body req, err := http.NewRequest(http.MethodPost, current_url, body) if err != nil { - return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#http.NewRequest: %s", err) + return response, fmt.Errorf("UploadDocument#http.NewRequest: %s", err) } req.Header.Set("Content-Type", writer.FormDataContentType()) @@ -205,15 +206,12 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) ( client := &http.Client{} resp, err := client.Do(req) if err != nil { - return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#client.Do: %s", err) + return response, fmt.Errorf("UploadDocument#client.Do: %s", err) } defer resp.Body.Close() - // Handle the response - var res UploadDocumentResponse - - json.NewDecoder(resp.Body).Decode(&res) - return res, nil + err = json.NewDecoder(resp.Body).Decode(&response) + return response, err } // CallWithData takes data and returns a string representing a response body. From d4f26435e8f4b79740710e5bb2d1363e8b726884 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 19 Aug 2023 15:38:52 -0400 Subject: [PATCH 05/16] Remplacer deprecated ioutil.ReadAll -> io.ReadAll --- api/api.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/api/api.go b/api/api.go index 419ad41..dd8c92a 100644 --- a/api/api.go +++ b/api/api.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "mime/multipart" "net/http" "net/url" @@ -110,7 +109,7 @@ func (a *API) Call(method, route string) ([]byte, error) { defer response.Body.Close() - body, err := ioutil.ReadAll(response.Body) + body, err := io.ReadAll(response.Body) if err != nil { return nil, err } @@ -138,7 +137,7 @@ func (a *API) Call(method, route string) ([]byte, error) { defer resp.Body.Close() // Read Response Body - respBody, err := ioutil.ReadAll(resp.Body) + respBody, err := io.ReadAll(resp.Body) if err != nil { return nil, err } From 8cce7414ef2ef9917a0427920a2a36ea4281ca18 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 19 Aug 2023 15:49:57 -0400 Subject: [PATCH 06/16] =?UTF-8?q?D=C3=A9placer=20UploadDocumentResponse=20?= =?UTF-8?q?dans=20models/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactor UploadDocumentResponse selon type models.Response Implémenter models.Response struct et models.Responder interface --- api/api.go | 16 +++------------- models/responses.go | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 models/responses.go diff --git a/api/api.go b/api/api.go index dd8c92a..310bbdc 100644 --- a/api/api.go +++ b/api/api.go @@ -11,6 +11,7 @@ import ( "net/url" "git.agecem.com/agecem/agecem-org/config" + "git.agecem.com/agecem/agecem-org/models" "github.com/spf13/viper" ) @@ -29,17 +30,6 @@ type APIOptions struct { Password string } -type UploadDocumentResponse struct { - Info UploadDocumentResponseInfo `json:"info"` - Message string `json:"message"` -} - -type UploadDocumentResponseInfo struct { - Bucket string `json:"bucket"` - Object string `json:"key"` - Size float64 `json:"size"` -} - // NewApiClientFromViper returns a pointer to a new API object, // provided the configuration options are managed by // https://git.agecem.com/agecem/agecem-org/config @@ -148,8 +138,8 @@ func (a *API) Call(method, route string) ([]byte, error) { return nil, errors.New(fmt.Sprintf("method must be 'GET' or 'DELETE', got '%s'", method)) } -func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (UploadDocumentResponse, error) { - var response UploadDocumentResponse +func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (models.UploadDocumentResponse, error) { + var response models.UploadDocumentResponse endpoint := fmt.Sprintf("%s://%s:%d", a.Protocol, a.Host, diff --git a/models/responses.go b/models/responses.go new file mode 100644 index 0000000..b57b779 --- /dev/null +++ b/models/responses.go @@ -0,0 +1,25 @@ +package models + +type Responder interface { + Respond() Responder +} + +type Response struct { + StatusCode int `json:"status_code"` + Message string +} + +func (r Response) Respond() Responder { + return r +} + +type UploadDocumentResponse struct { + Response + Data UploadDocumentResponseData +} + +type UploadDocumentResponseData struct { + Bucket string + Object string + Size float64 +} From 6b19aa8db681bf1351bfa95611d599e85ff38ca8 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 19 Aug 2023 19:25:13 -0400 Subject: [PATCH 07/16] Ajouter field Response.Error --- models/responses.go | 1 + 1 file changed, 1 insertion(+) diff --git a/models/responses.go b/models/responses.go index b57b779..c9ff5d1 100644 --- a/models/responses.go +++ b/models/responses.go @@ -7,6 +7,7 @@ type Responder interface { type Response struct { StatusCode int `json:"status_code"` Message string + Error string } func (r Response) Respond() Responder { From ab5ba6708c38808ce883d14420bb8ac9459d2b36 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 19 Aug 2023 19:57:47 -0400 Subject: [PATCH 08/16] Ajouter models.V1SeedResponse --- models/responses.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/models/responses.go b/models/responses.go index c9ff5d1..815df1d 100644 --- a/models/responses.go +++ b/models/responses.go @@ -24,3 +24,10 @@ type UploadDocumentResponseData struct { Object string Size float64 } + +type V1SeedResponse struct { + Response + Data struct { + Buckets []string + } +} From 8dd0049fbad31153895052ed12416853083a3958 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 19 Aug 2023 19:58:09 -0400 Subject: [PATCH 09/16] =?UTF-8?q?Mettre=20=C3=A0=20jour=20utilisation=20de?= =?UTF-8?q?=20models.Bucket=20et=20V1SeedResponse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/server.go | 4 ++-- serverhandlers/serverhandlers.go | 34 ++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index 5e4f8a4..556c477 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -444,7 +444,7 @@ func handleAdminDocumentsUploadPOST(c echo.Context) error { } for bucketName, displayName := range buckets { - data.Buckets = append(data.Buckets, Bucket{ + data.Buckets = append(data.Buckets, models.Bucket{ Name: bucketName, DisplayName: displayName, }) @@ -467,7 +467,7 @@ func handleAdminDocumentsUploadPOST(c echo.Context) error { // Format response var info, status string - info = fmt.Sprintf("[%.0f] /public/documentation/%s/%s", response.Info.Size, response.Info.Bucket, response.Info.Object) + info = fmt.Sprintf("[%.0f] /public/documentation/%s/%s", response.Data.Size, response.Data.Bucket, response.Data.Object) status = response.Message diff --git a/serverhandlers/serverhandlers.go b/serverhandlers/serverhandlers.go index 6aca6d1..e7c8e1f 100644 --- a/serverhandlers/serverhandlers.go +++ b/serverhandlers/serverhandlers.go @@ -7,6 +7,7 @@ import ( "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" "github.com/spf13/viper" @@ -25,34 +26,37 @@ func 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 HandleV1Seed(c echo.Context) error { + var response models.V1SeedResponse + 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) } new_buckets, err := mediaClient.Seed() + response.Data.Buckets = new_buckets if err != nil { - return c.JSON(http.StatusInternalServerError, map[string]string{ - "message": "Error during mediaClient.Seed()", - "error": err.Error(), - }) + response.StatusCode = http.StatusInternalServerError + response.Message = "Error during mediaClient.Seed()" + response.Error = err.Error() + + return c.JSON(response.StatusCode, response) } - var message string if len(new_buckets) == 0 { - message = "All buckets already exist" + response.Message = "All buckets already exist" } else { - message = "Buckets successfully created" + response.Message = "Buckets successfully created" } - return c.JSON(http.StatusOK, map[string]interface{}{ - "message": message, - "buckets": new_buckets, - }) + response.StatusCode = http.StatusOK + + return c.JSON(response.StatusCode, response) } // HandleV1BucketList affiche les buckets permis par server.documents.buckets, qui existent. From 3bab5b3b51d786d246785d0b02964f7aa1afa166 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sun, 20 Aug 2023 16:19:05 -0400 Subject: [PATCH 10/16] Refactor api_handlers et web_handlers serverhandlers -> api_handlers html handlers dans cmd/server -> web_handlers --- .../api_handlers.go | 2 +- cmd/server.go | 268 ++---------------- web_handlers/web_handlers.go | 231 +++++++++++++++ 3 files changed, 254 insertions(+), 247 deletions(-) rename serverhandlers/serverhandlers.go => api_handlers/api_handlers.go (99%) create mode 100644 web_handlers/web_handlers.go diff --git a/serverhandlers/serverhandlers.go b/api_handlers/api_handlers.go similarity index 99% rename from serverhandlers/serverhandlers.go rename to api_handlers/api_handlers.go index e7c8e1f..3b8537b 100644 --- a/serverhandlers/serverhandlers.go +++ b/api_handlers/api_handlers.go @@ -1,4 +1,4 @@ -package serverhandlers +package api_handlers import ( "context" diff --git a/cmd/server.go b/cmd/server.go index 556c477..831946e 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -5,10 +5,8 @@ package cmd import ( "crypto/subtle" - "encoding/json" "fmt" "log" - "sort" "embed" "html/template" @@ -18,13 +16,12 @@ 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" - "git.agecem.com/agecem/agecem-org/models" "git.agecem.com/agecem/agecem-org/public" - "git.agecem.com/agecem/agecem-org/serverhandlers" "git.agecem.com/agecem/agecem-org/templates" + "git.agecem.com/agecem/agecem-org/web_handlers" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" ) @@ -201,51 +198,51 @@ func RunServer() { // API Routes - groupV1.GET("", serverhandlers.HandleV1) + groupV1.GET("", api_handlers.HandleV1) - groupV1.POST("/seed", serverhandlers.HandleV1Seed) + groupV1.POST("/seed", api_handlers.HandleV1Seed) - groupV1.GET("/bucket", serverhandlers.HandleV1BucketList) + groupV1.GET("/bucket", api_handlers.HandleV1BucketList) - groupV1.GET("/bucket/:bucket", serverhandlers.HandleV1BucketRead) + groupV1.GET("/bucket/:bucket", api_handlers.HandleV1BucketRead) - groupV1.POST("/bucket/:bucket", serverhandlers.HandleV1DocumentCreate) + groupV1.POST("/bucket/:bucket", api_handlers.HandleV1DocumentCreate) - groupV1.GET("/bucket/:bucket/:document", serverhandlers.HandleV1DocumentRead) + groupV1.GET("/bucket/:bucket/:document", api_handlers.HandleV1DocumentRead) - groupV1.PUT("/bucket/:bucket/:document", serverhandlers.HandleV1DocumentUpdate) + groupV1.PUT("/bucket/:bucket/:document", api_handlers.HandleV1DocumentUpdate) - groupV1.DELETE("/bucket/:bucket/:document", serverhandlers.HandleV1DocumentDelete) + groupV1.DELETE("/bucket/:bucket/:document", api_handlers.HandleV1DocumentDelete) // HTML Routes - e.GET("/", handleIndex) + e.GET("/", web_handlers.HandleIndex) - //e.GET("/a-propos", handleAPropos) + //e.GET("/a-propos", web_handlers.HandleAPropos) - //e.GET("/actualite", handleActualite) + //e.GET("/actualite", web_handlers.HandleActualite) - //e.GET("/actualite/:article", handleActualiteArticle) + //e.GET("/actualite/:article", web_handlers.HandleActualiteArticle) - e.GET("/vie-etudiante", handleVieEtudiante) + e.GET("/vie-etudiante", web_handlers.HandleVieEtudiante) - e.GET("/vie-etudiante/:organisme", handleVieEtudianteOrganisme) + e.GET("/vie-etudiante/:organisme", web_handlers.HandleVieEtudianteOrganisme) - e.GET("/documentation", handleDocumentation) + e.GET("/documentation", web_handlers.HandleDocumentation) - e.GET("/formulaires", handleFormulaires) + e.GET("/formulaires", web_handlers.HandleFormulaires) // Public Routes - e.GET("/public/documentation/:bucket/:document", handlePublicDocumentation) + e.GET("/public/documentation/:bucket/:document", web_handlers.HandlePublicDocumentation) // Admin Routes - groupAdmin.GET("", handleAdmin) + groupAdmin.GET("", web_handlers.HandleAdmin) - groupAdmin.GET("/documents/upload", handleAdminDocumentsUpload) + groupAdmin.GET("/documents/upload", web_handlers.HandleAdminDocumentsUpload) - groupAdmin.POST("/documents/upload", handleAdminDocumentsUploadPOST) + groupAdmin.POST("/documents/upload", web_handlers.HandleAdminDocumentsUploadPOST) e.Logger.Fatal(e.Start( fmt.Sprintf(":%d", cfg.Server.Port))) @@ -254,224 +251,3 @@ func RunServer() { func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error { return t.templates.ExecuteTemplate(w, name, data) } - -// HTML Handlers - -func handleIndex(c echo.Context) error { - return c.Render(http.StatusOK, "index-html", nil) -} - -/* -func handleAPropos(c echo.Context) error { - return c.Render(http.StatusOK, "a-propos-html", nil) -} -*/ - -/* -func handleActualite(c echo.Context) error { - return c.Render(http.StatusOK, "actualite-html", nil) -} -*/ - -/* -func handleActualiteArticle(c echo.Context) error { - article := c.Param("article") - return c.String(http.StatusOK, fmt.Sprintf("Article: %s", article)) -} -*/ - -func handleVieEtudiante(c echo.Context) error { - return c.Render(http.StatusOK, "vie-etudiante-html", nil) -} - -func handleVieEtudianteOrganisme(c echo.Context) error { - organisme := c.Param("organisme") - return c.String(http.StatusOK, fmt.Sprintf("Organisme: %s", organisme)) -} - -func handleDocumentation(c echo.Context) error { - client, err := api.NewApiClientFromViper() - if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-html", nil) - } - - result, err := client.Call(http.MethodGet, "/v1/bucket") - if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-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 []models.Bucket - - for bucket, displayName := range buckets { - content, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket)) - if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-html", nil) - } - - var documents []string - - err = json.Unmarshal(content, &documents) - if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-html", nil) - } - - // 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 - */ - - data = append(data, models.Bucket{ - Name: bucket, - DisplayName: displayName, - Documents: documents, - }) - } - - sort.SliceStable(data, func(i, j int) bool { return data[i].Name < data[j].Name }) - - return c.Render(http.StatusOK, "documentation-html", data) -} - -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(http.StatusNotFound, map[string]string{"message": "Not Found"}) - } - - bucket := c.Param("bucket") - document := c.Param("document") - - result, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s/%s", bucket, document)) - if err != nil { - return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) - } - - // Check if result can fit inside a map containing a message - var result_map map[string]string - - err = json.Unmarshal(result, &result_map) - if err == nil { - return c.JSON(http.StatusBadRequest, result_map) - } - - return c.Blob(http.StatusOK, "application/octet-stream", result) -} - -func handleAdmin(c echo.Context) error { - return c.Render(http.StatusOK, "admin-html", nil) -} - -func handleAdminDocumentsUpload(c echo.Context) error { - client, err := api.NewApiClientFromViper() - if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-html", nil) - } - - result, err := client.Call(http.MethodGet, "/v1/bucket") - if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-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{ - Name: bucketName, - DisplayName: displayName, - }) - } - - return c.Render(http.StatusOK, "admin-upload-html", data) -} - -func handleAdminDocumentsUploadPOST(c echo.Context) error { - var data struct { - Buckets []models.Bucket - Message string - } - - 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) - } - - result, err := client.Call(http.MethodGet, "/v1/bucket") - if err != nil { - data.Message = "Error during GET /v1/bucket" - return c.Render(http.StatusInternalServerError, "documentation-html", data) - } - - var buckets map[string]string - - err = json.Unmarshal(result, &buckets) - if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-html", nil) - } - - for bucketName, displayName := range buckets { - data.Buckets = append(data.Buckets, models.Bucket{ - Name: bucketName, - DisplayName: displayName, - }) - } - - bucket := c.FormValue("bucket") - - 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, 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) - } - - // Format response - var info, status string - - info = fmt.Sprintf("[%.0f] /public/documentation/%s/%s", response.Data.Size, response.Data.Bucket, response.Data.Object) - - status = response.Message - - data.Message = fmt.Sprintf("%s - %s", status, info) - - return c.Render(http.StatusOK, "admin-upload-html", data) -} diff --git a/web_handlers/web_handlers.go b/web_handlers/web_handlers.go new file mode 100644 index 0000000..e9639cd --- /dev/null +++ b/web_handlers/web_handlers.go @@ -0,0 +1,231 @@ +package web_handlers + +import ( + "encoding/json" + "fmt" + "net/http" + "sort" + + "git.agecem.com/agecem/agecem-org/api" + "git.agecem.com/agecem/agecem-org/models" + "github.com/labstack/echo/v4" +) + +func HandleIndex(c echo.Context) error { + return c.Render(http.StatusOK, "index-html", nil) +} + +/* +func HandleAPropos(c echo.Context) error { + return c.Render(http.StatusOK, "a-propos-html", nil) +} +*/ + +/* +func HandleActualite(c echo.Context) error { + return c.Render(http.StatusOK, "actualite-html", nil) +} +*/ + +/* +func HandleActualiteArticle(c echo.Context) error { + article := c.Param("article") + return c.String(http.StatusOK, fmt.Sprintf("Article: %s", article)) +} +*/ + +func HandleVieEtudiante(c echo.Context) error { + return c.Render(http.StatusOK, "vie-etudiante-html", nil) +} + +func HandleVieEtudianteOrganisme(c echo.Context) error { + organisme := c.Param("organisme") + return c.String(http.StatusOK, fmt.Sprintf("Organisme: %s", organisme)) +} + +func HandleDocumentation(c echo.Context) error { + client, err := api.NewApiClientFromViper() + if err != nil { + return c.Render(http.StatusInternalServerError, "documentation-html", nil) + } + + result, err := client.Call(http.MethodGet, "/v1/bucket") + if err != nil { + return c.Render(http.StatusInternalServerError, "documentation-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 []models.Bucket + + for bucket, displayName := range buckets { + content, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket)) + if err != nil { + return c.Render(http.StatusInternalServerError, "documentation-html", nil) + } + + var documents []string + + err = json.Unmarshal(content, &documents) + if err != nil { + return c.Render(http.StatusInternalServerError, "documentation-html", nil) + } + + // 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 + */ + + data = append(data, models.Bucket{ + Name: bucket, + DisplayName: displayName, + Documents: documents, + }) + } + + sort.SliceStable(data, func(i, j int) bool { return data[i].Name < data[j].Name }) + + return c.Render(http.StatusOK, "documentation-html", data) +} + +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(http.StatusNotFound, map[string]string{"message": "Not Found"}) + } + + bucket := c.Param("bucket") + document := c.Param("document") + + result, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s/%s", bucket, document)) + if err != nil { + return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + } + + // Check if result can fit inside a map containing a message + var result_map map[string]string + + err = json.Unmarshal(result, &result_map) + if err == nil { + return c.JSON(http.StatusBadRequest, result_map) + } + + return c.Blob(http.StatusOK, "application/octet-stream", result) +} + +func HandleAdmin(c echo.Context) error { + return c.Render(http.StatusOK, "admin-html", nil) +} + +func HandleAdminDocumentsUpload(c echo.Context) error { + client, err := api.NewApiClientFromViper() + if err != nil { + return c.Render(http.StatusInternalServerError, "documentation-html", nil) + } + + result, err := client.Call(http.MethodGet, "/v1/bucket") + if err != nil { + return c.Render(http.StatusInternalServerError, "documentation-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{ + Name: bucketName, + DisplayName: displayName, + }) + } + + return c.Render(http.StatusOK, "admin-upload-html", data) +} + +func HandleAdminDocumentsUploadPOST(c echo.Context) error { + var data struct { + Buckets []models.Bucket + Message string + } + + 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) + } + + result, err := client.Call(http.MethodGet, "/v1/bucket") + if err != nil { + data.Message = "Error during GET /v1/bucket" + return c.Render(http.StatusInternalServerError, "documentation-html", data) + } + + var buckets map[string]string + + err = json.Unmarshal(result, &buckets) + if err != nil { + return c.Render(http.StatusInternalServerError, "documentation-html", nil) + } + + for bucketName, displayName := range buckets { + data.Buckets = append(data.Buckets, models.Bucket{ + Name: bucketName, + DisplayName: displayName, + }) + } + + bucket := c.FormValue("bucket") + + 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, 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) + } + + // Format response + var info, status string + + info = fmt.Sprintf("[%.0f] /public/documentation/%s/%s", response.Data.Size, response.Data.Bucket, response.Data.Object) + + status = response.Message + + data.Message = fmt.Sprintf("%s - %s", status, info) + + return c.Render(http.StatusOK, "admin-upload-html", data) +} From 3a602486df06076712d3792c87b2ce759ae03f1a Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sun, 20 Aug 2023 16:21:44 -0400 Subject: [PATCH 11/16] =?UTF-8?q?Mettre=20=C3=A0=20jour=20Dockerfile=20ave?= =?UTF-8?q?c=20refactor=20de=20packages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index a53152d..a0fac5f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,8 @@ ADD cmd/ cmd/ ADD api/ api/ +ADD api_handlers/ api_handlers/ + ADD config/ config/ ADD media/ media/ @@ -20,7 +22,7 @@ ADD models/ models/ ADD templates/ templates/ -ADD serverhandlers/ serverhandlers/ +ADD web_handlers/ web_handlers/ RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o agecem-org . From c8af2acae917c1be2e688374eddede16d239569c Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sun, 20 Aug 2023 17:56:49 -0400 Subject: [PATCH 12/16] 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 + } +} From d736d53a43650f7f7940a9ac722ac6945e13a629 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sun, 20 Aug 2023 17:57:14 -0400 Subject: [PATCH 13/16] =?UTF-8?q?Impl=C3=A9menter=20HandleV1BucketListResp?= =?UTF-8?q?onse?= 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 { From 9ebf27dbaf6c81cbfa1f82ff72dd5502fdd97efe Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sun, 20 Aug 2023 17:57:36 -0400 Subject: [PATCH 14/16] 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 +} From 3ce7ac53ca3ca9c645663711ce8c6cbd0adb7b55 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sun, 20 Aug 2023 18:03:45 -0400 Subject: [PATCH 15/16] 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) } From 502b57bcd57f587ef555959c766078dda66c1845 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sun, 20 Aug 2023 18:35:07 -0400 Subject: [PATCH 16/16] =?UTF-8?q?Finaliser=20impl=C3=A9mentation=20de=20Ha?= =?UTF-8?q?ndleDocumentationResponse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- templates/html/documentation.gohtml | 5 ++++- web_handlers/web_handlers.go | 21 +++++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/templates/html/documentation.gohtml b/templates/html/documentation.gohtml index ab74478..5fd55dc 100644 --- a/templates/html/documentation.gohtml +++ b/templates/html/documentation.gohtml @@ -12,7 +12,7 @@

Documentation

- {{ range . }} + {{ range .Data.Buckets }} {{ $bucket_name := .Name }} {{ $bucket_display_name := .DisplayName }}

@@ -26,6 +26,9 @@
{{ end }}

+

+ {{ .Message }} +

diff --git a/web_handlers/web_handlers.go b/web_handlers/web_handlers.go index dc45dfb..872aed5 100644 --- a/web_handlers/web_handlers.go +++ b/web_handlers/web_handlers.go @@ -48,7 +48,11 @@ func HandleDocumentation(c echo.Context) error { client, err := api.NewApiClientFromViper() if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-html", 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() @@ -67,14 +71,22 @@ func HandleDocumentation(c echo.Context) error { // 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) + response.StatusCode = http.StatusInternalServerError + response.Message = "Error during /v1/bucket/:bucket" + response.Error = err.Error() + + return c.Render(response.StatusCode, "documentation-html", response) } var documents []string err = json.Unmarshal(content, &documents) if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-html", nil) + response.StatusCode = http.StatusInternalServerError + response.Message = "Error during json.Unmarshal /v1/bucket/:bucket" + response.Error = err.Error() + + return c.Render(response.StatusCode, "documentation-html", response) } // Ce bloc retire tous les caractères spéciaux d'une string @@ -107,7 +119,8 @@ func HandleDocumentation(c echo.Context) error { response.StatusCode = http.StatusOK // TODO render .Message - return c.Render(response.StatusCode, "documentation-html", response.Data.Buckets) + return c.Render(response.StatusCode, "documentation-html", response) + //return c.Render(response.StatusCode, "documentation-html", response.Data.Buckets) } func HandleFormulaires(c echo.Context) error {