mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-07-02 11:43:42 +02:00
Define eosapi.ReqParams struct that includes a optional Host parameter.
If left empty, Host parsed from eosapi.ReqParams.Url will be used (like before).
This commit is contained in:
parent
bb8cb86fc0
commit
3aba653038
2 changed files with 29 additions and 20 deletions
|
|
@ -10,6 +10,11 @@ import (
|
||||||
"github.com/liamylian/jsontime/v2"
|
"github.com/liamylian/jsontime/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ReqParams struct {
|
||||||
|
Url string
|
||||||
|
Host string
|
||||||
|
}
|
||||||
|
|
||||||
var json = v2.ConfigWithCustomTimeFormat
|
var json = v2.ConfigWithCustomTimeFormat
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
@ -18,31 +23,35 @@ func init() {
|
||||||
v2.SetDefaultTimeFormat("2006-01-02T15:04:05", time.UTC)
|
v2.SetDefaultTimeFormat("2006-01-02T15:04:05", time.UTC)
|
||||||
}
|
}
|
||||||
|
|
||||||
func send(method string, api_url string) (*req.Resp, error) {
|
func send(p ReqParams, method string, path string) (*req.Resp, error) {
|
||||||
|
|
||||||
u, err := url.Parse(api_url)
|
host := p.Host
|
||||||
if err != nil {
|
if len(host) < 1 {
|
||||||
return nil, err
|
u, err := url.Parse(p.Url)
|
||||||
}
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
host = strings.Split(u.Host, ":")[0]
|
||||||
|
}
|
||||||
|
|
||||||
// Go's net.http (that `req` uses) sends the port in the host header.
|
// Go's net.http (that `req` uses) sends the port in the host header.
|
||||||
// nodeos api does not like that, so we need to provide our
|
// nodeos api does not like that, so we need to provide our
|
||||||
// own Host header with just the host.
|
// own Host header with just the host.
|
||||||
headers := req.Header{
|
headers := req.Header{
|
||||||
"Host": strings.Split(u.Host, ":")[0],
|
"Host": host,
|
||||||
}
|
}
|
||||||
|
|
||||||
r := req.New()
|
r := req.New()
|
||||||
return r.Do(method, api_url, headers)
|
return r.Do(method, p.Url + path, headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetInfo - Fetches get_info from API
|
// GetInfo - Fetches get_info from API
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
func GetInfo(url string) (Info, error) {
|
func GetInfo(params ReqParams) (Info, error) {
|
||||||
|
|
||||||
var info Info
|
var info Info
|
||||||
|
|
||||||
r, err := send("GET", url + "/v1/chain/get_info")
|
r, err := send(params, "GET", "/v1/chain/get_info")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
resp := r.Response()
|
resp := r.Response()
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
|
@ -53,11 +62,11 @@ func GetInfo(url string) (Info, error) {
|
||||||
return info, err
|
return info, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetHealth(url string) (Health, error) {
|
func GetHealth(params ReqParams) (Health, error) {
|
||||||
|
|
||||||
var health Health;
|
var health Health;
|
||||||
|
|
||||||
r, err := send("GET", url + "/v2/health")
|
r, err := send(params, "GET", "/v2/health")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
resp := r.Response()
|
resp := r.Response()
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,9 @@ import (
|
||||||
|
|
||||||
// check_api - Validates head block time.
|
// check_api - Validates head block time.
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
func check_api(url string, block_time float64) (haproxy.HealthCheckStatus, string) {
|
func check_api(p eosapi.ReqParams, block_time float64) (haproxy.HealthCheckStatus, string) {
|
||||||
|
|
||||||
info, err := eosapi.GetInfo(url)
|
info, err := eosapi.GetInfo(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg := fmt.Sprintf("%s", err);
|
msg := fmt.Sprintf("%s", err);
|
||||||
return haproxy.HealthCheckFailed, msg
|
return haproxy.HealthCheckFailed, msg
|
||||||
|
|
@ -41,9 +41,9 @@ func check_api(url string, block_time float64) (haproxy.HealthCheckStatus, strin
|
||||||
// Validates block num diff between
|
// Validates block num diff between
|
||||||
// nodeos and elasticsearch
|
// nodeos and elasticsearch
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
func check_api_v2(url string, offset int64) (haproxy.HealthCheckStatus, string) {
|
func check_api_v2(p eosapi.ReqParams, offset int64) (haproxy.HealthCheckStatus, string) {
|
||||||
|
|
||||||
health, err := eosapi.GetHealth(url)
|
health, err := eosapi.GetHealth(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg := fmt.Sprintf("%s", err);
|
msg := fmt.Sprintf("%s", err);
|
||||||
return haproxy.HealthCheckFailed, msg
|
return haproxy.HealthCheckFailed, msg
|
||||||
|
|
@ -136,14 +136,14 @@ func main() {
|
||||||
|
|
||||||
// TCP Client sends message.
|
// TCP Client sends message.
|
||||||
server.OnNewMessage(func(c *tcp_server.Client, message string) {
|
server.OnNewMessage(func(c *tcp_server.Client, message string) {
|
||||||
var url string
|
params := eosapi.ReqParams{}
|
||||||
var block_time int = 10
|
var block_time int = 10
|
||||||
var version string = "v1"
|
var version string = "v1"
|
||||||
|
|
||||||
// Parse host + port.
|
// Parse host + port.
|
||||||
split := strings.Split(strings.TrimSpace(message), "|")
|
split := strings.Split(strings.TrimSpace(message), "|")
|
||||||
|
|
||||||
url = split[0]
|
params.Url = split[0]
|
||||||
if len(split) > 1 {
|
if len(split) > 1 {
|
||||||
p, err := strconv.ParseInt(split[1], 10, 32)
|
p, err := strconv.ParseInt(split[1], 10, 32)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
@ -159,14 +159,14 @@ func main() {
|
||||||
var msg string
|
var msg string
|
||||||
|
|
||||||
if version == "v2" {
|
if version == "v2" {
|
||||||
status, msg = check_api_v2(url, int64(block_time / 2))
|
status, msg = check_api_v2(params, int64(block_time / 2))
|
||||||
} else {
|
} else {
|
||||||
version = "v1"
|
version = "v1"
|
||||||
status, msg = check_api(url, float64(block_time))
|
status, msg = check_api(params, float64(block_time))
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("Status %s - %s (%d blocks): %s",
|
log.Info("Status %s - %s (%d blocks): %s",
|
||||||
version, url, block_time / 2, status)
|
version, params.Url, block_time / 2, status)
|
||||||
|
|
||||||
if status != haproxy.HealthCheckUp && len(msg) > 0 {
|
if status != haproxy.HealthCheckUp && len(msg) > 0 {
|
||||||
log.Warning(msg)
|
log.Warning(msg)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue