@@ -10,14 +10,15 @@ import (
1010 "encoding/json"
1111 "fmt"
1212 "net/http"
13+ "net/url"
1314 "time"
1415
1516 "github.com/sirupsen/logrus"
1617)
1718
1819// Healthcheck allows to check status of the node
1920type Healthcheck struct {
20- uri string
21+ url url. URL
2122 userAgent string
2223}
2324
@@ -27,9 +28,9 @@ type Status struct {
2728}
2829
2930// Create a new Healthcheck
30- func NewHealthcheck (uri string , userAgent string ) * Healthcheck {
31+ func NewHealthcheck (url url. URL , userAgent string ) * Healthcheck {
3132 return & Healthcheck {
32- uri : uri ,
33+ url : url ,
3334 userAgent : userAgent ,
3435 }
3536}
@@ -39,7 +40,7 @@ func (h *Healthcheck) Run(ctx context.Context) error {
3940 client := http.Client {
4041 Timeout : 100 * time .Millisecond ,
4142 }
42- req , err := http .NewRequestWithContext (ctx , http .MethodGet , h .uri + "/status" , nil )
43+ req , err := http .NewRequestWithContext (ctx , http .MethodGet , h .url . String () , nil )
4344 if err != nil {
4445 logrus .WithError (err ).Error ("Error while creating http get request" )
4546 return err
@@ -49,23 +50,23 @@ func (h *Healthcheck) Run(ctx context.Context) error {
4950 req .Header .Set ("Accept-Charset" , "utf-8" )
5051 resp , err := client .Do (req )
5152 if err != nil {
52- logrus .WithFields (logrus.Fields {"remote-server" : h .uri }).WithError (err ).Info ("No http response" )
53+ logrus .WithFields (logrus.Fields {"remote-server" : h .url }).WithError (err ).Info ("No http response" )
5354 return err
5455 }
5556 defer resp .Body .Close ()
5657 if resp .StatusCode != 200 {
57- logrus .WithFields (logrus.Fields {"remote-server" : h .uri }).WithError (err ).Info ("Http response is not 200 OK" )
58+ logrus .WithFields (logrus.Fields {"remote-server" : h .url }).WithError (err ).Info ("Http response is not 200 OK" )
5859 return err
5960 }
6061 decoder := json .NewDecoder (resp .Body )
6162 var status Status
6263 if err := decoder .Decode (& status ); err != nil {
63- logrus .WithFields (logrus.Fields {"remote-server" : h .uri }).WithError (err ).Info ("Could not decode json response" )
64+ logrus .WithFields (logrus.Fields {"remote-server" : h .url }).WithError (err ).Info ("Could not decode json response" )
6465 return err
6566 }
6667 if ! status .Ready {
6768 err := fmt .Errorf ("Server is not ready" )
68- logrus .WithFields (logrus.Fields {"remote-server" : h .uri }).WithError (err ).Info ("Server is not ready" )
69+ logrus .WithFields (logrus.Fields {"remote-server" : h .url }).WithError (err ).Info ("Server is not ready" )
6970 return err
7071 }
7172 return nil
0 commit comments