Skip to content

Commit 9aa8dc6

Browse files
committed
chore: update dependencies
- Update github.com/google/uuid from v1.3.0 to v1.6.0 - Update github.com/stretchr/testify from v1.8.0 to v1.8.3 - Remove github.com/yuin/goldmark v1.4.13/go.mod - Update golang.org/x/crypto from v0.0.0-20190308221718-c2843e01d9a2 to v0.0.0-20210921155107-089bfa567519 - Remove golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod - Update golang.org/x/net from v0.0.0-20190620200207-3b0461eec859 to v0.21.0 - Remove golang.org/x/sync v0.0.0-201
1 parent 38abdef commit 9aa8dc6

File tree

3 files changed

+148
-177
lines changed

3 files changed

+148
-177
lines changed

go.mod

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
module github.com/keloran/go-healthcheck
22

3-
go 1.12
3+
go 1.22
44

55
require (
66
github.com/go-ping/ping v1.1.0
7-
github.com/google/uuid v1.3.0 // indirect
87
github.com/stretchr/testify v1.8.3
9-
golang.org/x/net v0.10.0 // indirect
10-
golang.org/x/sync v0.2.0 // indirect
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/google/uuid v1.6.0 // indirect
13+
github.com/pmezard/go-difflib v1.0.0 // indirect
14+
golang.org/x/net v0.21.0 // indirect
15+
golang.org/x/sync v0.6.0 // indirect
16+
golang.org/x/sys v0.17.0 // indirect
17+
gopkg.in/yaml.v3 v3.0.1 // indirect
1118
)

go.sum

+8-43
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,28 @@
1-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
21
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
32
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
43
github.com/go-ping/ping v1.1.0 h1:3MCGhVX4fyEUuhsfwPrsEdQw6xspHkv5zHsiSoDFZYw=
54
github.com/go-ping/ping v1.1.0/go.mod h1:xIFjORFzTxqIV/tDVGO4eDy/bLuSyawEeojSm3GfRGk=
65
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
7-
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
8-
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
6+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
7+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
98
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
109
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
11-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
12-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
13-
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
14-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
15-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
1610
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
1711
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
18-
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
19-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
20-
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
21-
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
22-
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
23-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
24-
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
2512
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
26-
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
27-
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
28-
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
29-
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
30-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
13+
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
14+
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
3115
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
32-
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
33-
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
34-
golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
35-
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
36-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
16+
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
17+
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
3718
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3819
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
39-
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
40-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
41-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
42-
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
43-
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
44-
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
20+
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
21+
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
4522
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
46-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
47-
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
48-
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
49-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
5023
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
51-
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
52-
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
53-
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
5424
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
55-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
56-
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
57-
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
58-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
5925
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
6026
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
61-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6227
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
6328
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

healthcheck.go

+129-130
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,160 @@
11
package healthcheck
22

33
import (
4-
"encoding/json"
5-
"fmt"
6-
"io/ioutil"
7-
"net/http"
8-
"os"
9-
"strings"
10-
11-
"github.com/go-ping/ping"
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"net/http"
8+
"os"
9+
"strings"
10+
11+
"github.com/go-ping/ping"
1212
)
1313

1414
// HTTP the request as done by routing
1515
func HTTP(w http.ResponseWriter, r *http.Request) {
16-
hc := HealthCheck{
17-
Name: os.Getenv("SERVICE_NAME"),
18-
URL: r.Host,
19-
Dependencies: os.Getenv("SERVICE_DEPENDENCIES"),
20-
}
21-
22-
health, err := hc.Check()
23-
if err != nil {
24-
w.Header().Set("Content-Type", "application/health+json")
25-
j, _ := json.Marshal(Health{
26-
Status: HealthFail,
27-
})
28-
w.WriteHeader(http.StatusOK)
29-
_, fErr := w.Write(j)
30-
if fErr != nil {
31-
fmt.Printf("write response: %v\n", fErr)
32-
}
33-
fmt.Printf("http health failed: %+v\n", err)
34-
return
35-
}
36-
37-
j, _ := json.Marshal(health)
38-
w.Header().Set("Content-Type", "application/health+json")
39-
w.WriteHeader(http.StatusOK)
40-
_, fErr := w.Write(j)
41-
if fErr != nil {
42-
fmt.Printf("write response: %v\n", fErr)
43-
}
16+
hc := HealthCheck{
17+
Name: os.Getenv("SERVICE_NAME"),
18+
URL: r.Host,
19+
Dependencies: os.Getenv("SERVICE_DEPENDENCIES"),
20+
}
21+
22+
health, err := hc.Check()
23+
if err != nil {
24+
w.Header().Set("Content-Type", "application/health+json")
25+
j, _ := json.Marshal(Health{
26+
Status: HealthFail,
27+
})
28+
w.WriteHeader(http.StatusOK)
29+
_, fErr := w.Write(j)
30+
if fErr != nil {
31+
fmt.Printf("write response: %v\n", fErr)
32+
}
33+
fmt.Printf("http health failed: %+v\n", err)
34+
return
35+
}
36+
37+
j, _ := json.Marshal(health)
38+
w.Header().Set("Content-Type", "application/health+json")
39+
_, fErr := w.Write(j)
40+
if fErr != nil {
41+
fmt.Printf("write response: %v\n", fErr)
42+
}
4443
}
4544

4645
// Check do the health check itself
4746
func (h HealthCheck) Check() (Health, error) {
48-
health := Health{
49-
Name: h.Name,
50-
URL: h.URL,
51-
Status: HealthFail,
52-
Dependencies: nil,
53-
}
54-
55-
health.Status = HealthPass
56-
if h.Dependencies != "" {
57-
deps, err := h.getDependencies()
58-
if err != nil {
59-
return health, err
60-
}
61-
62-
checkedDeps := []Health{}
63-
for _, dep := range deps.Dependencies {
64-
d, err := dep.check()
65-
if err != nil {
66-
return health, err
67-
}
68-
checkedDeps = append(checkedDeps, d)
69-
}
70-
71-
health.Dependencies = checkedDeps
72-
}
73-
74-
// now set to failed if a dependency failed
75-
for _, dep := range health.Dependencies {
76-
if dep.Status == HealthFail {
77-
health.Status = HealthFail
78-
}
79-
}
80-
81-
return health, nil
47+
health := Health{
48+
Name: h.Name,
49+
URL: h.URL,
50+
Status: HealthFail,
51+
Dependencies: nil,
52+
}
53+
54+
health.Status = HealthPass
55+
if h.Dependencies != "" {
56+
deps, err := h.getDependencies()
57+
if err != nil {
58+
return health, err
59+
}
60+
61+
checkedDeps := []Health{}
62+
for _, dep := range deps.Dependencies {
63+
d, err := dep.check()
64+
if err != nil {
65+
return health, err
66+
}
67+
checkedDeps = append(checkedDeps, d)
68+
}
69+
70+
health.Dependencies = checkedDeps
71+
}
72+
73+
// now set to failed if a dependency failed
74+
for _, dep := range health.Dependencies {
75+
if dep.Status == HealthFail {
76+
health.Status = HealthFail
77+
}
78+
}
79+
80+
return health, nil
8281
}
8382

8483
// getDependencies get the list of dependencies
8584
func (h HealthCheck) getDependencies() (Dependencies, error) {
86-
deps := Dependencies{}
87-
err := json.Unmarshal([]byte(h.Dependencies), &deps)
88-
if err != nil {
89-
return deps, err
90-
}
85+
deps := Dependencies{}
86+
err := json.Unmarshal([]byte(h.Dependencies), &deps)
87+
if err != nil {
88+
return deps, err
89+
}
9190

92-
return deps, nil
91+
return deps, nil
9392
}
9493

9594
// check the dependency status
9695
func (d Dependency) check() (Health, error) {
97-
if strings.Contains(d.URL, "$") {
98-
d.URL = os.Getenv(d.URL[1:])
99-
}
96+
if strings.Contains(d.URL, "$") {
97+
d.URL = os.Getenv(d.URL[1:])
98+
}
10099

101-
// Ping check
102-
if d.Ping {
103-
return d.ping()
104-
}
100+
// Ping check
101+
if d.Ping {
102+
return d.ping()
103+
}
105104

106-
// Standard check
107-
return d.curl()
105+
// Standard check
106+
return d.curl()
108107
}
109108

110109
// ping checks
111110
func (d Dependency) ping() (Health, error) {
112-
h := Health{
113-
Name: d.Name,
114-
URL: d.URL,
115-
Status: HealthFail,
116-
}
117-
118-
pinger, err := ping.NewPinger(h.URL)
119-
if err != nil {
120-
return h, err
121-
}
122-
123-
pinger.Count = 3
124-
if err := pinger.Run(); err != nil {
125-
return h, err
126-
}
127-
128-
h.Status = HealthPass
129-
return h, nil
111+
h := Health{
112+
Name: d.Name,
113+
URL: d.URL,
114+
Status: HealthFail,
115+
}
116+
117+
pinger, err := ping.NewPinger(h.URL)
118+
if err != nil {
119+
return h, err
120+
}
121+
122+
pinger.Count = 3
123+
if err := pinger.Run(); err != nil {
124+
return h, err
125+
}
126+
127+
h.Status = HealthPass
128+
return h, nil
130129
}
131130

132131
// curl checks
133132
func (d Dependency) curl() (Health, error) {
134-
h := Health{}
135-
p, err := http.Get(d.URL)
136-
if err != nil {
137-
h = Health{
138-
URL: d.URL,
139-
Status: HealthFail,
140-
}
141-
return h, err
142-
}
143-
144-
defer func() {
145-
_ = p.Body.Close()
146-
}()
147-
148-
b, err := ioutil.ReadAll(p.Body)
149-
if err != nil {
150-
h = Health{
151-
URL: d.URL,
152-
Status: HealthFail,
153-
}
154-
return h, err
155-
}
156-
jerr := json.Unmarshal(b, &h)
157-
if jerr != nil {
158-
return h, jerr
159-
}
160-
return h, nil
133+
h := Health{}
134+
p, err := http.Get(d.URL)
135+
if err != nil {
136+
h = Health{
137+
URL: d.URL,
138+
Status: HealthFail,
139+
}
140+
return h, err
141+
}
142+
143+
defer func() {
144+
_ = p.Body.Close()
145+
}()
146+
147+
b, err := ioutil.ReadAll(p.Body)
148+
if err != nil {
149+
h = Health{
150+
URL: d.URL,
151+
Status: HealthFail,
152+
}
153+
return h, err
154+
}
155+
jerr := json.Unmarshal(b, &h)
156+
if jerr != nil {
157+
return h, jerr
158+
}
159+
return h, nil
161160
}

0 commit comments

Comments
 (0)