1
0
Fork 0
mirror of https://github.com/eosswedenorg/antelope-api-healthcheck synced 2026-06-16 04:44:55 +02:00

src/server.go: use code from parse_request.go

This commit is contained in:
Henrik Hautakoski 2022-08-16 19:49:25 +02:00
parent da8a53aa3c
commit 83a2ddf2ed
No known key found for this signature in database
GPG key ID: 608414D93E862CCD
2 changed files with 2 additions and 67 deletions

View file

@ -5,7 +5,7 @@ GOLDFLAGS = -ldflags="-s -w"
PREFIX = /usr/local
PROGRAM_NAME=eosio-api-healthcheck
SOURCES=src/main.go src/server.go
SOURCES=src/main.go src/server.go src/parse_request.go
.PHONY: all build/$(PROGRAM_NAME) clean
all: build

View file

@ -1,84 +1,19 @@
package main
import (
"fmt"
"strings"
"strconv"
"github.com/eosswedenorg/eosio-api-healthcheck/src/api"
"github.com/eosswedenorg-go/haproxy/agentcheck"
"github.com/eosswedenorg-go/tcp_server"
)
type arguments struct {
api string
url string
host string
num_blocks int
}
func createApi(a *arguments) (api.ApiInterface, error) {
switch a.api {
case "v1":
return api.NewEosioV1(a.url, a.host, float64(a.num_blocks / 2)), nil
case "v2":
return api.NewEosioV2(a.url, a.host, int64(a.num_blocks)), nil
case "contract":
return api.NewEosioContract(a.url, float64(a.num_blocks / 2)), nil
case "test":
return api.NewTestApi(a.url), nil
}
return nil, fmt.Errorf("Invalid API '%s'", a.api)
}
// onTcpMessage callback function
// ---------------------------------------------------------
func onTcpMessage(c *tcp_server.Client, args string) {
a := arguments{
api: "v1",
num_blocks: 10,
}
// Parse arguments.
// -------------------
split := strings.Split(strings.TrimSpace(args), "|")
if len(split) < 2 {
msg := "Invalid number of parameters in agent request"
logger.Warn("Agent request error", "message", msg, "args", split)
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
c.WriteString(resp.String())
c.Close()
return
}
// 1. Api
a.api = split[0]
// 2. url (scheme + ip/domain + port)
a.url = split[1]
// 3. num blocks
if len(split) > 2 {
num, err := strconv.ParseInt(split[2], 10, 32)
if err == nil {
a.num_blocks = int(num)
}
}
// 4. Host
if len(split) > 3 {
a.host = split[3]
}
// Check api.
// -------------------
healthCheckApi, err := createApi(&a)
healthCheckApi, err := ParseRequest(args)
if err != nil {
logger.Warn("Agent request error", "message", err)
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")