1
0
Fork 0
mirror of https://github.com/eosswedenorg/antelope-api-healthcheck synced 2026-07-04 12:03:43 +02:00

combine host, port parameters into one string and add scheme.

This commit is contained in:
Henrik Hautakoski 2020-03-06 11:23:28 +01:00
parent 07c73c2b4e
commit 9717354e22
2 changed files with 26 additions and 28 deletions

View file

@ -2,9 +2,9 @@
package eosapi; package eosapi;
import ( import (
"fmt"
"time" "time"
"io/ioutil" "io/ioutil"
"net/url"
"github.com/imroc/req" "github.com/imroc/req"
"github.com/liamylian/jsontime/v2" "github.com/liamylian/jsontime/v2"
) )
@ -17,26 +17,31 @@ func init() {
v2.SetDefaultTimeFormat("2006-01-02T15:04:05", time.UTC); v2.SetDefaultTimeFormat("2006-01-02T15:04:05", time.UTC);
} }
func send(method string, host string, port int, uri string) (*req.Resp, error) { func send(method string, api_url string) (*req.Resp, error) {
u, err := url.Parse(api_url)
if err != nil {
return nil, err;
}
// 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": host, "Host": u.Host,
} }
r := req.New() r := req.New()
return r.Do(method, fmt.Sprintf("http://%s:%d%s", host, port, uri), headers); return r.Do(method, api_url, headers)
} }
// GetInfo - Fetches get_info from API // GetInfo - Fetches get_info from API
// --------------------------------------------------------- // ---------------------------------------------------------
func GetInfo(host string, port int) (Info, error) { func GetInfo(url string) (Info, error) {
var info Info; var info Info;
r, err := send("GET", host, port, "/v1/chain/get_info"); r, err := send("GET", url + "/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);
@ -47,11 +52,11 @@ func GetInfo(host string, port int) (Info, error) {
return info, err; return info, err;
} }
func GetHealth(host string, port int) (Health, error) { func GetHealth(url string) (Health, error) {
var health Health; var health Health;
r, err := send("GET", host, port, "/v2/health"); r, err := send("GET", url + "/v2/health");
if err == nil { if err == nil {
resp := r.Response() resp := r.Response()
body, _ := ioutil.ReadAll(resp.Body); body, _ := ioutil.ReadAll(resp.Body);

View file

@ -14,9 +14,9 @@ import (
// check_api - Validates head block time. // check_api - Validates head block time.
// --------------------------------------------------------- // ---------------------------------------------------------
func check_api(host string, port int, block_time float64) (haproxy.HealthCheckStatus, string) { func check_api(url string, block_time float64) (haproxy.HealthCheckStatus, string) {
info, err := eosapi.GetInfo(host, port) info, err := eosapi.GetInfo(url)
if err != nil { if err != nil {
msg := fmt.Sprintf("%s", err); msg := fmt.Sprintf("%s", err);
return haproxy.HealthCheckFailed, msg return haproxy.HealthCheckFailed, msg
@ -40,9 +40,9 @@ func check_api(host string, port int, block_time float64) (haproxy.HealthCheckSt
// Validates block num diff between // Validates block num diff between
// nodeos and elasticsearch // nodeos and elasticsearch
// --------------------------------------------------------- // ---------------------------------------------------------
func check_api_v2(host string, port int, offset int64) (haproxy.HealthCheckStatus, string) { func check_api_v2(url string, offset int64) (haproxy.HealthCheckStatus, string) {
health, err := eosapi.GetHealth(host, port) health, err := eosapi.GetHealth(url)
if err != nil { if err != nil {
msg := fmt.Sprintf("%s", err); msg := fmt.Sprintf("%s", err);
return haproxy.HealthCheckFailed, msg return haproxy.HealthCheckFailed, msg
@ -116,29 +116,22 @@ 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 host string var url string
var port int = 80
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), "|")
host = split[0] 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 {
port = int(p)
}
}
if len(split) > 2 {
p, err := strconv.ParseInt(split[2], 10, 32)
if err == nil { if err == nil {
block_time = int(p) block_time = int(p)
} }
} }
if len(split) > 3 { if len(split) > 2 {
version = split[3] version = split[2]
} }
// Check api. // Check api.
@ -146,14 +139,14 @@ func main() {
var msg string var msg string
if version == "v2" { if version == "v2" {
status, msg = check_api_v2(host, port, int64(block_time / 2)) status, msg = check_api_v2(url, int64(block_time / 2))
} else { } else {
version = "v1" version = "v1"
status, msg = check_api(host, port, float64(block_time)) status, msg = check_api(url, float64(block_time))
} }
log.Info("Status %s - %s:%d (%d blocks): %s", log.Info("Status %s - %s (%d blocks): %s",
version, host, port, block_time / 2, status) version, 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)