From 44a01344c720adf72113e00adaa9a67b98aaae92 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 3 Jan 2022 18:00:01 +0100 Subject: [PATCH] Replace local eosapi with github.com/eosswedenorg-go/eosapi --- go.mod | 4 +- go.sum | 2 + src/eosapi/functions.go | 84 ----------------------------------------- src/eosapi/go.mod | 8 ---- src/eosapi/types.go | 27 ------------- src/server.go | 2 +- 6 files changed, 4 insertions(+), 123 deletions(-) delete mode 100644 src/eosapi/functions.go delete mode 100644 src/eosapi/go.mod delete mode 100644 src/eosapi/types.go diff --git a/go.mod b/go.mod index 87b2821..ce25421 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/eosswedenorg/eosio-api-healthcheck go 1.14 require ( + github.com/eosswedenorg-go/eosapi v0.1.0 github.com/eosswedenorg-go/haproxy v0.0.0-20220101140534-fccfdd93a8cd github.com/eosswedenorg-go/pid v1.0.0 github.com/eosswedenorg-go/tcp_server v0.1.0 @@ -10,10 +11,7 @@ require ( github.com/inconshreveable/log15 v0.0.0-20201112154412-8562bdadbbac github.com/mattn/go-colorable v0.1.12 // indirect github.com/pborman/getopt/v2 v2.1.0 - internal/eosapi v1.0.0 internal/utils v1.0.0 ) -replace internal/eosapi => ./src/eosapi - replace internal/utils => ./src/utils diff --git a/go.sum b/go.sum index 86ffd61..31fb589 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/eosswedenorg-go/eosapi v0.1.0 h1:SnVMx1QGPBZoQknjnAiGzjL6hVfrXPLrOdxoUstYUrk= +github.com/eosswedenorg-go/eosapi v0.1.0/go.mod h1:7VrkU30cSqRtGDE6bXygWqMcEhCyWOaC9yVA34QIQzM= github.com/eosswedenorg-go/haproxy v0.0.0-20220101140534-fccfdd93a8cd h1:e59v3HnnG60uE50OtziOxyiSNDADr1lDbzn33btn3yE= github.com/eosswedenorg-go/haproxy v0.0.0-20220101140534-fccfdd93a8cd/go.mod h1:l5DTEb0dcTZyATo2cuaYQPqwBRuzDw0UgupNo8SAgXo= github.com/eosswedenorg-go/pid v1.0.0 h1:k1ra19cgWBHnX5gWQq+eUhNHIjT7hdhsYGp+Ovfvd2U= diff --git a/src/eosapi/functions.go b/src/eosapi/functions.go deleted file mode 100644 index ab262dc..0000000 --- a/src/eosapi/functions.go +++ /dev/null @@ -1,84 +0,0 @@ - -package eosapi; - -import ( - "strings" - "time" - "net/url" - "io/ioutil" - "github.com/imroc/req" - jsontime "github.com/liamylian/jsontime/v2/v2" -) - -type ReqParams struct { - Url string - Host string -} - -var json = jsontime.ConfigWithCustomTimeFormat - -func init() { - - // EOS Api does not specify timezone in timestamps (they are always UTC tho). - jsontime.SetDefaultTimeFormat("2006-01-02T15:04:05", time.UTC) -} - -func send(p ReqParams, method string, path string) (*req.Resp, error) { - - 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": host, - } - - r := req.New() - return r.Do(method, p.Url + path, headers) -} - -// GetInfo - Fetches get_info from API -// --------------------------------------------------------- -func GetInfo(params ReqParams) (Info, error) { - - var info Info - - r, err := send(params, "GET", "/v1/chain/get_info") - if err == nil { - resp := r.Response() - body, _ := ioutil.ReadAll(resp.Body) - - // Set HTTPStatusCode - info.HTTPStatusCode = resp.StatusCode - - // Parse json - err = json.Unmarshal(body, &info) - } - return info, err -} - -func GetHealth(params ReqParams) (Health, error) { - - var health Health; - - r, err := send(params, "GET", "/v2/health") - if err == nil { - resp := r.Response() - body, _ := ioutil.ReadAll(resp.Body) - - // Set HTTPStatusCode - health.HTTPStatusCode = resp.StatusCode - - // Parse json - err = json.Unmarshal(body, &health) - } - return health, err -} diff --git a/src/eosapi/go.mod b/src/eosapi/go.mod deleted file mode 100644 index 425b42c..0000000 --- a/src/eosapi/go.mod +++ /dev/null @@ -1,8 +0,0 @@ -module internal/eosapi - -go 1.14 - -require ( - github.com/imroc/req v0.3.2 - github.com/liamylian/jsontime/v2 v2.0.0 -) diff --git a/src/eosapi/types.go b/src/eosapi/types.go deleted file mode 100644 index 393cb48..0000000 --- a/src/eosapi/types.go +++ /dev/null @@ -1,27 +0,0 @@ - -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"` - HTTPStatusCode int -} - -// 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"` - HTTPStatusCode int -} diff --git a/src/server.go b/src/server.go index 7bc2e9c..80f4669 100644 --- a/src/server.go +++ b/src/server.go @@ -5,8 +5,8 @@ import ( "time" "strings" "strconv" - "internal/eosapi" "internal/utils" + "github.com/eosswedenorg-go/eosapi" "github.com/eosswedenorg-go/haproxy" "github.com/eosswedenorg-go/tcp_server" )