From 0f60e58ec21abb15d20be9bbd4c3ccb8cc03baeb Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 19 Aug 2023 14:48:08 -0400 Subject: [PATCH 1/7] 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 73b5ce1bc2dc3f3ebf67a167e90dd05595a5e1a7 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 19 Aug 2023 15:35:56 -0400 Subject: [PATCH 2/7] =?UTF-8?q?Ajouter=20error=20handling=20=C3=A0=20api.A?= =?UTF-8?q?PI#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 3/7] 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 4/7] =?UTF-8?q?D=C3=A9placer=20UploadDocumentResponse=20da?= =?UTF-8?q?ns=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 5/7] 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 6/7] 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 7/7] =?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.