From 0f563487465ed9243edca9629024c4aefd3461af Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 5 Sep 2023 16:30:15 -0400 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9menter=20Response=20dans=20GetHealthR?= =?UTF-8?q?esponse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/apiclient.go | 2 +- handlers/health.go | 22 +++++++++++++--------- responses/health.go | 5 ++--- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/data/apiclient.go b/data/apiclient.go index 2f1f1ae..3319d72 100644 --- a/data/apiclient.go +++ b/data/apiclient.go @@ -77,7 +77,7 @@ func (a *ApiClient) Call(method, route string, requestBody io.Reader, useKey boo // GetHealth allows checking for API server health func (a *ApiClient) GetHealth() (string, error) { - var getHealthResponse responses.GetHealth + var getHealthResponse responses.GetHealthResponse response, err := a.Call(http.MethodGet, "/v5/health", nil, true) if err != nil { diff --git a/handlers/health.go b/handlers/health.go index f8baece..d4fe868 100644 --- a/handlers/health.go +++ b/handlers/health.go @@ -1,7 +1,6 @@ package handlers import ( - "fmt" "net/http" "git.agecem.com/agecem/bottin/v5/data" @@ -10,23 +9,28 @@ import ( ) func GetHealth(c echo.Context) error { - response := responses.GetHealth{ - Message: "Bottin API v5 is ready", - } + var response responses.GetHealthResponse dataClient, err := data.NewDataClientFromViper() if err != nil { - response.Message = fmt.Sprintf("Error during data.NewDataClientFromViper(): %s", err) + response.StatusCode = http.StatusInternalServerError + response.Message = "Error during data.NewDataClientFromViper()" + response.Error = err.Error() - return c.JSON(http.StatusInternalServerError, response) + return c.JSON(response.StatusCode, response) } defer dataClient.DB.Close() if err = dataClient.DB.Ping(); err != nil { - response.Message = fmt.Sprintf("Error during dataClient.DB.Ping(): %s", err) + response.StatusCode = http.StatusInternalServerError + response.Message = "Error during dataClient.DB.Ping()" + response.Error = err.Error() - return c.JSON(http.StatusInternalServerError, response) + return c.JSON(response.StatusCode, response) } - return c.JSON(http.StatusOK, response) + response.StatusCode = http.StatusOK + response.Message = "Bottin API v5 is ready" + + return c.JSON(response.StatusCode, response) } diff --git a/responses/health.go b/responses/health.go index 6d5e663..854279d 100644 --- a/responses/health.go +++ b/responses/health.go @@ -1,6 +1,5 @@ package responses -// GetHealth is the response type for handlers.GetHealth -type GetHealth struct { - Message string `json:"message"` +type GetHealthResponse struct { + Response }