forked from apache/servicecomb-mesher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl7check.go
61 lines (59 loc) · 1.45 KB
/
l7check.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
package health
import (
"errors"
"github.com/go-chassis/go-chassis/core/lager"
"github.com/go-chassis/go-chassis/pkg/httpclient"
"github.com/go-mesh/mesher/config"
"io/ioutil"
"net/http"
"regexp"
"strconv"
)
//HTTPCheck checks http service
func HTTPCheck(check *config.HealthCheck, address string) error {
c, err := httpclient.GetURLClient(httpclient.DefaultURLClientOption)
if err != nil {
lager.Logger.Error("can not get http client: " + err.Error())
//must not return error, because it is mesher error
return nil
}
var url = "http://" + address
if check.URI != "" {
url = url + check.URI
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
lager.Logger.Error("can not get http req: " + err.Error())
//must not return error, because it is mesher error
return nil
}
resp, err := c.Do(req)
if err != nil {
lager.Logger.Error("server can not be connected: " + err.Error())
return err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
defer resp.Body.Close()
if check.Match != nil {
if check.Match.Status != "" {
n, _ := strconv.Atoi(check.Match.Status)
if resp.StatusCode != n {
return errors.New("status is not " + check.Match.Status)
}
}
if check.Match.Body != "" {
re := regexp.MustCompile(check.Match.Body)
if !re.Match(body) {
return errors.New("body does not match " + check.Match.Body)
}
}
} else {
if resp.StatusCode == 200 {
return nil
}
}
return nil
}