-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
41 lines (32 loc) · 803 Bytes
/
api.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
package main
import (
"encoding/json"
"errors"
"io"
"net/http"
)
type HealthResponse struct {
Database bool `json:"database"`
Chromedriver bool `json:"chromedriver"`
}
func getEcoindexHealth(url string) error {
var healthResponse HealthResponse
res, err := http.Get(url + "/health")
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return errors.New("Status code is " + res.Status)
}
resBody, err := io.ReadAll(res.Body)
if err != nil {
return errors.New("response body can not be read")
}
if err := json.Unmarshal(resBody, &healthResponse); err != nil {
return errors.New("response body is not valid json")
}
if !healthResponse.Chromedriver || !healthResponse.Database {
return errors.New("Ecoindex is KO: " + string(resBody))
}
return nil
}