-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth.go
63 lines (54 loc) · 1.28 KB
/
health.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
)
// hits the Health api of the account service
func (a *accountClient) Health(ctx context.Context) error {
healthPath := a.baseUrl.String() + endpointHealth
requestMethod := http.MethodGet
resp, err := a.httpClient.Get(healthPath)
if err != nil {
return &ErrRemoteGatewayFailure{
requestMethod,
healthPath,
0,
"failed to connect to remote gateway",
}
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return &ErrRemoteGatewayFailure{
requestMethod,
healthPath,
resp.StatusCode,
"invalid response code from api",
}
}
var healthResponse HealthResponse
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&healthResponse)
if err != nil {
return &ErrRemoteGatewayFailure{
Method: requestMethod,
BaseUri: healthPath,
StatusCode: resp.StatusCode,
Message: "invalid response body from api",
}
}
if strings.ToUpper(healthResponse.Status) != "UP" {
return &ErrRemoteGatewayFailure{
Method: requestMethod,
BaseUri: healthPath,
StatusCode: resp.StatusCode,
Message: fmt.Sprintf("status is %s in response from api", healthResponse.Status),
}
}
return nil
}
type HealthResponse struct {
Status string `json:"status"`
}