Fix client web démarre sans API server accessible
Ajouter apiClient.GetV4() comme health check du API server
This commit is contained in:
parent
3e42ac0454
commit
942b69407d
2 changed files with 50 additions and 6 deletions
|
@ -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
|
||||
|
|
|
@ -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"`
|
||||
|
|
Reference in a new issue