From 942b69407d73c509a45167925a58897dbbcec652 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 26 May 2023 01:43:58 -0400 Subject: [PATCH] =?UTF-8?q?Fix=20client=20web=20d=C3=A9marre=20sans=20API?= =?UTF-8?q?=20server=20accessible?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ajouter apiClient.GetV4() comme health check du API server --- v4/cmd/web.go | 27 +++++++++++++++++++++------ v4/data/apiclient.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/v4/cmd/web.go b/v4/cmd/web.go index c108012..08f6375 100644 --- a/v4/cmd/web.go +++ b/v4/cmd/web.go @@ -6,7 +6,9 @@ import ( "fmt" "html/template" "io" + "log" + "git.agecem.com/agecem/bottin/v4/data" "git.agecem.com/agecem/bottin/v4/web" "git.agecem.com/agecem/bottin/v4/web/webhandlers" "github.com/labstack/echo/v4" @@ -16,12 +18,13 @@ import ( ) var ( - webUser string - webPassword string - webPort int - webApiHost string - webApiKey string - webApiPort int + webUser string + webPassword string + webPort int + webApiHost string + webApiKey string + webApiPort int + webApiProtocol string ) var templatesFS embed.FS @@ -43,10 +46,22 @@ var webCmd = &cobra.Command{ webApiHost = viper.GetString("web.api.host") webApiKey = viper.GetString("web.api.key") webApiPort = viper.GetInt("web.api.port") + webApiProtocol = viper.GetString("web.api.protocol") webPassword = viper.GetString("web.password") webPort = viper.GetInt("web.port") webUser = viper.GetString("web.user") + // Ping API server + + apiClient := data.NewApiClient(webApiKey, webApiHost, webApiProtocol, webApiPort) + + pingResult, err := apiClient.GetV4() + if err != nil { + log.Fatal(err) + } + + log.Println(pingResult) + e := echo.New() // Middlewares diff --git a/v4/data/apiclient.go b/v4/data/apiclient.go index c805463..f97684b 100644 --- a/v4/data/apiclient.go +++ b/v4/data/apiclient.go @@ -69,6 +69,35 @@ func (a *ApiClient) Call(method, route string, requestBody io.Reader, useKey boo return response, nil } +// GetV4 allows checking for API v4 server health +func (a *ApiClient) GetV4() (string, error) { + var getV4Response struct { + Message string `json:"message"` + } + + response, err := a.Call(http.MethodGet, "/v4", nil, true) + if err != nil { + return getV4Response.Message, err + } + + defer response.Body.Close() + + body, err := ioutil.ReadAll(response.Body) + if err != nil { + return getV4Response.Message, err + } + + if err := json.Unmarshal(body, &getV4Response); err != nil { + return getV4Response.Message, err + } + + if getV4Response.Message == "" { + return getV4Response.Message, errors.New("Could not confirm that API server is up, no response message") + } + + return getV4Response.Message, nil +} + func (a *ApiClient) GetMembre(membreID string) (models.Membre, error) { var getMembreResponse struct { Message string `json:"message"`