mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-06-18 05:00:03 +02:00
Move source files to "src" folder.
This commit is contained in:
parent
8e8393d560
commit
4ef714a750
6 changed files with 1 additions and 1 deletions
68
src/eosapi/functions.go
Normal file
68
src/eosapi/functions.go
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
|
||||
package eosapi;
|
||||
|
||||
import (
|
||||
"time"
|
||||
"net/url"
|
||||
"io/ioutil"
|
||||
"github.com/imroc/req"
|
||||
"github.com/liamylian/jsontime/v2"
|
||||
)
|
||||
|
||||
var json = v2.ConfigWithCustomTimeFormat
|
||||
|
||||
func init() {
|
||||
|
||||
// EOS Api does not specify timezone in timestamps (they are always UTC tho).
|
||||
v2.SetDefaultTimeFormat("2006-01-02T15:04:05", time.UTC)
|
||||
}
|
||||
|
||||
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.
|
||||
// nodeos api does not like that, so we need to provide our
|
||||
// own Host header with just the host.
|
||||
headers := req.Header{
|
||||
"Host": u.Host,
|
||||
}
|
||||
|
||||
r := req.New()
|
||||
return r.Do(method, api_url, headers)
|
||||
}
|
||||
|
||||
// GetInfo - Fetches get_info from API
|
||||
// ---------------------------------------------------------
|
||||
func GetInfo(url string) (Info, error) {
|
||||
|
||||
var info Info
|
||||
|
||||
r, err := send("GET", url + "/v1/chain/get_info")
|
||||
if err == nil {
|
||||
resp := r.Response()
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
|
||||
// Parse json
|
||||
err = json.Unmarshal(body, &info)
|
||||
}
|
||||
return info, err
|
||||
}
|
||||
|
||||
func GetHealth(url string) (Health, error) {
|
||||
|
||||
var health Health;
|
||||
|
||||
r, err := send("GET", url + "/v2/health")
|
||||
if err == nil {
|
||||
resp := r.Response()
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
|
||||
// Parse json
|
||||
err = json.Unmarshal(body, &health)
|
||||
}
|
||||
return health, err
|
||||
}
|
||||
25
src/eosapi/types.go
Normal file
25
src/eosapi/types.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
|
||||
package eosapi;
|
||||
|
||||
import "time"
|
||||
|
||||
// get_info format (not all fields).
|
||||
type Info struct {
|
||||
ServerVersion string `json:"server_version"`
|
||||
HeadBlockNum int64 `json:"head_block_num"`
|
||||
HeadBlockTime time.Time `json:"head_block_time"`
|
||||
}
|
||||
|
||||
// Service struct from /v2/health
|
||||
type Service struct {
|
||||
Name string `json:"service"`
|
||||
Status string `json:"status"`
|
||||
Data map[string]interface{} `json:"service_data"`
|
||||
Time int64 `json:"time"` // unix timestamp.
|
||||
}
|
||||
|
||||
// /v2/health format (not all fields).
|
||||
type Health struct {
|
||||
VersionHash string `json:"version_hash"`
|
||||
Health []Service `json:"health"`
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue