mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-07-02 11:43:42 +02:00
src/server.go: use code from parse_request.go
This commit is contained in:
parent
da8a53aa3c
commit
83a2ddf2ed
2 changed files with 2 additions and 67 deletions
2
Makefile
2
Makefile
|
|
@ -5,7 +5,7 @@ GOLDFLAGS = -ldflags="-s -w"
|
||||||
PREFIX = /usr/local
|
PREFIX = /usr/local
|
||||||
|
|
||||||
PROGRAM_NAME=eosio-api-healthcheck
|
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
|
.PHONY: all build/$(PROGRAM_NAME) clean
|
||||||
all: build
|
all: build
|
||||||
|
|
|
||||||
|
|
@ -1,84 +1,19 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
"strings"
|
||||||
"strconv"
|
|
||||||
"github.com/eosswedenorg/eosio-api-healthcheck/src/api"
|
|
||||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
"github.com/eosswedenorg-go/tcp_server"
|
"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
|
// onTcpMessage callback function
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
func onTcpMessage(c *tcp_server.Client, args string) {
|
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.
|
// Check api.
|
||||||
// -------------------
|
// -------------------
|
||||||
healthCheckApi, err := createApi(&a)
|
healthCheckApi, err := ParseRequest(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn("Agent request error", "message", err)
|
logger.Warn("Agent request error", "message", err)
|
||||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue