mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-06-16 04:44:55 +02:00
move some "non server" code from src/server.go to src/main.go
This commit is contained in:
parent
dc28fbd762
commit
fe8724aae5
3 changed files with 62 additions and 61 deletions
2
Makefile
2
Makefile
|
|
@ -5,7 +5,7 @@ GOLDFLAGS = -ldflags="-s -w"
|
|||
PREFIX = /usr/local
|
||||
|
||||
PROGRAM_NAME=eosio-api-healthcheck
|
||||
SOURCES=src/server.go
|
||||
SOURCES=src/main.go src/server.go
|
||||
DEPENDANCIES= github.com/firstrow/tcp_server \
|
||||
github.com/liamylian/jsontime/v2 \
|
||||
github.com/imroc/req \
|
||||
|
|
|
|||
58
src/main.go
Normal file
58
src/main.go
Normal 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());
|
||||
}
|
||||
|
|
@ -6,12 +6,10 @@ import (
|
|||
"strings"
|
||||
"strconv"
|
||||
"./log"
|
||||
"./pid"
|
||||
"./haproxy"
|
||||
"./eosapi"
|
||||
"./utils"
|
||||
"github.com/firstrow/tcp_server"
|
||||
"github.com/pborman/getopt/v2"
|
||||
)
|
||||
|
||||
// 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"
|
||||
}
|
||||
|
||||
// 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
|
||||
// ---------------------------------------------------------
|
||||
|
||||
|
|
@ -167,38 +136,12 @@ func onTcpMessage(c *tcp_server.Client, args string) {
|
|||
c.Close()
|
||||
}
|
||||
|
||||
// main
|
||||
// spawnTcpServer
|
||||
// ---------------------------------------------------------
|
||||
func main() {
|
||||
|
||||
// Command line parsing
|
||||
getopt.FlagLong(&pidFile, "pid", 'p', "Path to pid file", "file")
|
||||
getopt.Parse()
|
||||
func spawnTcpServer(addr string) {
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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 := tcp_server.New(addr)
|
||||
server.OnNewMessage(onTcpMessage);
|
||||
|
||||
// TCP Client disconnect.
|
||||
server.OnClientConnectionClosed(func(c *tcp_server.Client, err error) {
|
||||
//fmt.Println("# Client disconnected")
|
||||
});
|
||||
|
||||
server.Listen()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue