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"`