1
0
Fork 0
mirror of https://github.com/eosswedenorg/antelope-api-healthcheck synced 2026-06-18 05:00:03 +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:
Henrik Hautakoski 2020-06-23 12:50:34 +02:00
parent bb8cb86fc0
commit 3aba653038
2 changed files with 29 additions and 20 deletions

View file

@ -10,6 +10,11 @@ import (
"github.com/liamylian/jsontime/v2"
)
type ReqParams struct {
Url string
Host string
}
var json = v2.ConfigWithCustomTimeFormat
func init() {
@ -18,31 +23,35 @@ func init() {
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)
if err != nil {
return nil, err
}
host := p.Host
if len(host) < 1 {
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.
// nodeos api does not like that, so we need to provide our
// own Host header with just the host.
headers := req.Header{
"Host": strings.Split(u.Host, ":")[0],
"Host": host,
}
r := req.New()
return r.Do(method, api_url, headers)
return r.Do(method, p.Url + path, headers)
}
// GetInfo - Fetches get_info from API
// ---------------------------------------------------------
func GetInfo(url string) (Info, error) {
func GetInfo(params ReqParams) (Info, error) {
var info Info
r, err := send("GET", url + "/v1/chain/get_info")
r, err := send(params, "GET", "/v1/chain/get_info")
if err == nil {
resp := r.Response()
body, _ := ioutil.ReadAll(resp.Body)
@ -53,11 +62,11 @@ func GetInfo(url string) (Info, error) {
return info, err
}
func GetHealth(url string) (Health, error) {
func GetHealth(params ReqParams) (Health, error) {
var health Health;
r, err := send("GET", url + "/v2/health")
r, err := send(params, "GET", "/v2/health")
if err == nil {
resp := r.Response()
body, _ := ioutil.ReadAll(resp.Body)

View file

@ -15,9 +15,9 @@ import (
// 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 {
msg := fmt.Sprintf("%s", err);
return haproxy.HealthCheckFailed, msg
@ -41,9 +41,9 @@ func check_api(url string, block_time float64) (haproxy.HealthCheckStatus, strin
// Validates block num diff between
// 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 {
msg := fmt.Sprintf("%s", err);
return haproxy.HealthCheckFailed, msg
@ -136,14 +136,14 @@ func main() {
// TCP Client sends message.
server.OnNewMessage(func(c *tcp_server.Client, message string) {
var url string
params := eosapi.ReqParams{}
var block_time int = 10
var version string = "v1"
// Parse host + port.
split := strings.Split(strings.TrimSpace(message), "|")
url = split[0]
params.Url = split[0]
if len(split) > 1 {
p, err := strconv.ParseInt(split[1], 10, 32)
if err == nil {
@ -159,14 +159,14 @@ func main() {
var msg string
if version == "v2" {
status, msg = check_api_v2(url, int64(block_time / 2))
status, msg = check_api_v2(params, int64(block_time / 2))
} else {
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",
version, url, block_time / 2, status)
version, params.Url, block_time / 2, status)
if status != haproxy.HealthCheckUp && len(msg) > 0 {
log.Warning(msg)