mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-08-27 20:58:13 +02:00
move internal/*.go files into internal/server
This commit is contained in:
parent
e9976fbbee
commit
1b1f601678
4 changed files with 7 additions and 7 deletions
58
internal/server/parse_request.go
Normal file
58
internal/server/parse_request.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/eosswedenorg/eosio-api-healthcheck/internal/api"
|
||||
)
|
||||
|
||||
func ParseArguments(args []string) api.ApiArguments {
|
||||
a := api.ApiArguments{
|
||||
NumBlocks: 10,
|
||||
}
|
||||
|
||||
// 1. url (scheme + ip/domain + port)
|
||||
a.Url = args[0]
|
||||
|
||||
// 2. num blocks
|
||||
if len(args) > 1 {
|
||||
num, err := strconv.ParseInt(args[1], 10, 32)
|
||||
if err == nil {
|
||||
a.NumBlocks = int(num)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Host
|
||||
if len(args) > 2 {
|
||||
a.Host = args[2]
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
func ParseRequest(request string) (api.ApiInterface, error) {
|
||||
factories := map[string]api.Factory{
|
||||
"v1": api.EosioV1Factory,
|
||||
"v2": api.EosioV2Factory,
|
||||
"contract": api.EosioContractFactory,
|
||||
"debug": api.DebugApiFactory,
|
||||
}
|
||||
|
||||
// Parse arguments.
|
||||
// -------------------
|
||||
p := strings.Split(strings.TrimSpace(request), "|")
|
||||
|
||||
if len(p) < 2 {
|
||||
return nil, fmt.Errorf("invalid number of parameters in agent request")
|
||||
}
|
||||
|
||||
a := ParseArguments(p[1:])
|
||||
|
||||
if factory, ok := factories[p[0]]; ok {
|
||||
return factory(a), nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("invalid API '%s'", p[0])
|
||||
}
|
||||
104
internal/server/parse_request_test.go
Normal file
104
internal/server/parse_request_test.go
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"testing"
|
||||
// "fmt"
|
||||
"github.com/eosswedenorg/eosio-api-healthcheck/internal/api"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParseWithInvalidApi(t *testing.T) {
|
||||
api, err := ParseRequest("invalid|http://api.example.com")
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, err.Error(), "invalid API 'invalid'")
|
||||
assert.Nil(t, api)
|
||||
}
|
||||
|
||||
func TestParseWithInvalidParams(t *testing.T) {
|
||||
api, err := ParseRequest("v1")
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, err.Error(), "invalid number of parameters in agent request")
|
||||
assert.Nil(t, api)
|
||||
}
|
||||
|
||||
// EosioV1
|
||||
// --------------------------------
|
||||
|
||||
func TestParseEosioV1(t *testing.T) {
|
||||
expected := api.NewEosioV1("http://api.example.com", "", 5)
|
||||
|
||||
api, err := ParseRequest("v1|http://api.example.com")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
|
||||
func TestParseEosioV1WithBlockNumber(t *testing.T) {
|
||||
expected := api.NewEosioV1("http://api.example.com", "", 1000)
|
||||
|
||||
api, err := ParseRequest("v1|http://api.example.com|2000")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
|
||||
func TestParseEosioV1Full(t *testing.T) {
|
||||
expected := api.NewEosioV1("http://api.example.com", "http://host.example.com", 500)
|
||||
|
||||
api, err := ParseRequest("v1|http://api.example.com|1000|http://host.example.com")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
|
||||
// EosioV2
|
||||
// --------------------------------
|
||||
|
||||
func TestParseEosioV2(t *testing.T) {
|
||||
expected := api.NewEosioV2("http://api.v2.example.com", "", 10)
|
||||
|
||||
api, err := ParseRequest("v2|http://api.v2.example.com")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
|
||||
func TestParseEosioV2WithOffset(t *testing.T) {
|
||||
expected := api.NewEosioV2("http://api.v2.example.com", "", 1000)
|
||||
|
||||
api, err := ParseRequest("v2|http://api.v2.example.com|1000")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
|
||||
func TestParseEosioV2Full(t *testing.T) {
|
||||
expected := api.NewEosioV2("http://api.v2.example.com", "http://host.example.com", 1000)
|
||||
|
||||
api, err := ParseRequest("v2|http://api.v2.example.com|1000|http://host.example.com")
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
|
||||
// EosioContract
|
||||
// --------------------------------
|
||||
|
||||
func TestParseEosioContract(t *testing.T) {
|
||||
expected := api.NewEosioContract("http://api.contract.example.com", 5)
|
||||
|
||||
api, err := ParseRequest("contract|http://api.contract.example.com")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
|
||||
func TestParseEosioContractWithBlockTime(t *testing.T) {
|
||||
expected := api.NewEosioContract("http://api.contract.example.com", 256)
|
||||
|
||||
api, err := ParseRequest("contract|http://api.contract.example.com|512")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
|
||||
func TestParseDebugApi(t *testing.T) {
|
||||
expected := api.NewDebugApi("some_api_call")
|
||||
|
||||
api, err := ParseRequest("debug|some_api_call")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
64
internal/server/server.go
Normal file
64
internal/server/server.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
"github.com/eosswedenorg-go/tcp_server"
|
||||
"github.com/eosswedenorg/eosio-api-healthcheck/internal/api"
|
||||
log "github.com/inconshreveable/log15"
|
||||
)
|
||||
|
||||
// onTcpMessage callback function
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func onTcpMessage(c *tcp_server.Client, args string) {
|
||||
logger := log.Root()
|
||||
|
||||
// Check api.
|
||||
// -------------------
|
||||
healthCheckApi, err := ParseRequest(args)
|
||||
if err != nil {
|
||||
logger.Warn("Agent request error", "message", err)
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||
|
||||
_, err = c.WriteString(resp.String())
|
||||
if err != nil {
|
||||
logger.Error("WriteString", "message", err)
|
||||
}
|
||||
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
|
||||
status, msg := healthCheckApi.Call()
|
||||
|
||||
params := api.LogParams{}
|
||||
params.Add("status", strings.TrimSpace(status.String()))
|
||||
|
||||
if msg != "OK" && len(msg) > 0 {
|
||||
params.Add("error", msg)
|
||||
}
|
||||
|
||||
logger.Info("API Check", params.Combine(healthCheckApi.LogInfo())...)
|
||||
// Report status to HAproxy
|
||||
_, err = c.WriteString(status.String())
|
||||
if err != nil {
|
||||
logger.Error("WriteString", "message", err)
|
||||
}
|
||||
c.Close()
|
||||
}
|
||||
|
||||
// Start
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func Start(addr string) error {
|
||||
server := tcp_server.New(addr)
|
||||
server.OnMessage(onTcpMessage)
|
||||
|
||||
err := server.Connect()
|
||||
if err == nil {
|
||||
go server.Listen()
|
||||
}
|
||||
return err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue