1
0
Fork 0
mirror of https://github.com/eosswedenorg/antelope-api-healthcheck synced 2026-07-03 11:53:43 +02:00

move some "non server" code from src/server.go to src/main.go

This commit is contained in:
Henrik Hautakoski 2020-06-26 03:55:41 +02:00
parent dc28fbd762
commit fe8724aae5
3 changed files with 62 additions and 61 deletions

View file

@ -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/server.go SOURCES=src/main.go src/server.go
DEPENDANCIES= github.com/firstrow/tcp_server \ DEPENDANCIES= github.com/firstrow/tcp_server \
github.com/liamylian/jsontime/v2 \ github.com/liamylian/jsontime/v2 \
github.com/imroc/req \ github.com/imroc/req \

58
src/main.go Normal file
View file

@ -0,0 +1,58 @@
package main
import (
"./log"
"./pid"
"github.com/pborman/getopt/v2"
)
// Command line flags
// ---------------------------------------------------------
var pidFile string
// argv_listen_addr
// Parse listen address from command line.
// ---------------------------------------------------------
func argv_listen_addr() string {
var addr string
argv := getopt.Args()
if len(argv) > 0 {
addr = argv[0]
} else {
addr = "127.0.0.1"
}
addr += ":"
if len(argv) > 1 {
addr += argv[1]
} else {
addr += "1337"
}
return addr
}
// main
// ---------------------------------------------------------
func main() {
// Command line parsing
getopt.FlagLong(&pidFile, "pid", 'p', "Path to pid file", "file")
getopt.Parse()
log.Info("Process is starting with PID: %d", pid.Get())
if len(pidFile) > 0 {
log.Info("Writing pidfile: %s", pidFile)
_, err := pid.Save(pidFile)
if err != nil {
log.Error("Failed to write pidfile: %v", err)
}
}
spawnTcpServer(argv_listen_addr());
}

View file

@ -6,12 +6,10 @@ import (
"strings" "strings"
"strconv" "strconv"
"./log" "./log"
"./pid"
"./haproxy" "./haproxy"
"./eosapi" "./eosapi"
"./utils" "./utils"
"github.com/firstrow/tcp_server" "github.com/firstrow/tcp_server"
"github.com/pborman/getopt/v2"
) )
// check_api - Validates head block time. // check_api - Validates head block time.
@ -81,35 +79,6 @@ func check_api_v2(p eosapi.ReqParams, offset int64) (haproxy.HealthCheckStatus,
return haproxy.HealthCheckUp, "OK" return haproxy.HealthCheckUp, "OK"
} }
// Command line flags
// ---------------------------------------------------------
var pidFile string
// argv_listen_addr
// Parse listen address from command line.
// ---------------------------------------------------------
func argv_listen_addr() string {
var addr string
argv := getopt.Args()
if len(argv) > 0 {
addr = argv[0]
} else {
addr = "127.0.0.1"
}
addr += ":"
if len(argv) > 1 {
addr += argv[1]
} else {
addr += "1337"
}
return addr
}
// onTcpMessage callback function // onTcpMessage callback function
// --------------------------------------------------------- // ---------------------------------------------------------
@ -167,38 +136,12 @@ func onTcpMessage(c *tcp_server.Client, args string) {
c.Close() c.Close()
} }
// main // spawnTcpServer
// --------------------------------------------------------- // ---------------------------------------------------------
func main() {
// Command line parsing func spawnTcpServer(addr string) {
getopt.FlagLong(&pidFile, "pid", 'p', "Path to pid file", "file")
getopt.Parse()
log.Info("Process is starting with PID: %d", pid.Get()) server := tcp_server.New(addr)
if len(pidFile) > 0 {
log.Info("Writing pidfile: %s", pidFile)
_, err := pid.Save(pidFile)
if err != nil {
log.Error("Failed to write pidfile: %v", err)
}
}
server := tcp_server.New(argv_listen_addr())
// TCP Client connect.
server.OnNewClient(func(c *tcp_server.Client) {
//fmt.Println("# Client connected")
});
// TCP Client sends message.
server.OnNewMessage(onTcpMessage); server.OnNewMessage(onTcpMessage);
// TCP Client disconnect.
server.OnClientConnectionClosed(func(c *tcp_server.Client, err error) {
//fmt.Println("# Client disconnected")
});
server.Listen() server.Listen()
} }