Fix startup flags

This commit is contained in:
Jeffrey Picard 2021-11-24 18:56:34 -05:00
parent ca0e8562f3
commit 283686ecac
6 changed files with 81 additions and 93 deletions

14
main.go
View file

@ -26,19 +26,7 @@ func main() {
s := server.MakeHubServer(ctxWCancel, args) s := server.MakeHubServer(ctxWCancel, args)
s.Run() s.Run()
//l, err := net.Listen("tcp", ":"+args.Port)
//if err != nil {
// log.Fatalf("failed to listen: %v", err)
//}
//
//pb.RegisterHubServer(s.GrpcServer, s)
//reflection.Register(s.GrpcServer)
//
//log.Printf("listening on %s\n", l.Addr().String())
//log.Println(s.Args)
//if err := s.GrpcServer.Serve(l); err != nil {
// log.Fatalf("failed to serve: %v", err)
//}
return return
} }

View file

@ -16,42 +16,42 @@ const (
// Args struct contains the arguments to the hub server. // Args struct contains the arguments to the hub server.
type Args struct { type Args struct {
CmdType int CmdType int
Host string Host string
Port string Port string
EsHost string EsHost string
EsPort string EsPort string
PrometheusPort string PrometheusPort string
EsIndex string EsIndex string
RefreshDelta int RefreshDelta int
CacheTTL int CacheTTL int
PeerFile string PeerFile string
Country string Country string
DisableEs bool DisableEs bool
Debug bool Debug bool
LoadPeers bool DisableLoadPeers bool
StartPrometheus bool DisableStartPrometheus bool
StartUDP bool DisableStartUDP bool
WritePeers bool DisableWritePeers bool
DisableFederation bool DisableFederation bool
} }
const ( const (
DefaultHost = "0.0.0.0" DefaultHost = "0.0.0.0"
DefaultPort = "50051" DefaultPort = "50051"
DefaultEsHost = "http://localhost" DefaultEsHost = "http://localhost"
DefaultEsIndex = "claims" DefaultEsIndex = "claims"
DefaultEsPort = "9200" DefaultEsPort = "9200"
DefaultPrometheusPort = "2112" DefaultPrometheusPort = "2112"
DefaultRefreshDelta = 5 DefaultRefreshDelta = 5
DefaultCacheTTL = 5 DefaultCacheTTL = 5
DefaultPeerFile = "peers.txt" DefaultPeerFile = "peers.txt"
DefaultCountry = "US" DefaultCountry = "US"
DefaultLoadPeers = true DefaultDisableLoadPeers = false
DefaultStartPrometheus = true DefaultDisableStartPrometheus = false
DefaultStartUDP = true DefaultDisableStartUDP = false
DefaultWritePeers = true DefaultDisableWritePeers = false
DefaultDisableFederation = false DefaultDisableFederation = false
) )
// GetEnvironment takes the environment variables as an array of strings // GetEnvironment takes the environment variables as an array of strings
@ -97,10 +97,10 @@ func ParseArgs(searchRequest *pb.SearchRequest) *Args {
debug := parser.Flag("", "debug", &argparse.Options{Required: false, Help: "enable debug logging", Default: false}) debug := parser.Flag("", "debug", &argparse.Options{Required: false, Help: "enable debug logging", Default: false})
disableEs := parser.Flag("", "disable-es", &argparse.Options{Required: false, Help: "Disable elastic search, for running/testing independently", Default: false}) disableEs := parser.Flag("", "disable-es", &argparse.Options{Required: false, Help: "Disable elastic search, for running/testing independently", Default: false})
loadPeers := parser.Flag("", "load-peers", &argparse.Options{Required: false, Help: "load peers from disk at startup", Default: DefaultLoadPeers}) disableLoadPeers := parser.Flag("", "disable-load-peers", &argparse.Options{Required: false, Help: "Disable load peers from disk at startup", Default: DefaultDisableLoadPeers})
startPrometheus := parser.Flag("", "start-prometheus", &argparse.Options{Required: false, Help: "Start prometheus server", Default: DefaultStartPrometheus}) disableStartPrometheus := parser.Flag("", "disable-start-prometheus", &argparse.Options{Required: false, Help: "Disable start prometheus server", Default: DefaultDisableStartPrometheus})
startUdp := parser.Flag("", "start-udp", &argparse.Options{Required: false, Help: "Start UDP ping server", Default: DefaultStartUDP}) disableStartUdp := parser.Flag("", "disable-start-udp", &argparse.Options{Required: false, Help: "Disable start UDP ping server", Default: DefaultDisableStartUDP})
writePeers := parser.Flag("", "write-peers", &argparse.Options{Required: false, Help: "Write peer to disk as we learn about them", Default: DefaultWritePeers}) disableWritePeers := parser.Flag("", "disable-write-peers", &argparse.Options{Required: false, Help: "Disable write peer to disk as we learn about them", Default: DefaultDisableWritePeers})
disableFederation := parser.Flag("", "disable-federation", &argparse.Options{Required: false, Help: "Disable server federation", Default: DefaultDisableFederation}) disableFederation := parser.Flag("", "disable-federation", &argparse.Options{Required: false, Help: "Disable server federation", Default: DefaultDisableFederation})
text := parser.String("", "text", &argparse.Options{Required: false, Help: "text query"}) text := parser.String("", "text", &argparse.Options{Required: false, Help: "text query"})
@ -120,24 +120,24 @@ func ParseArgs(searchRequest *pb.SearchRequest) *Args {
} }
args := &Args{ args := &Args{
CmdType: SearchCmd, CmdType: SearchCmd,
Host: *host, Host: *host,
Port: *port, Port: *port,
EsHost: *esHost, EsHost: *esHost,
EsPort: *esPort, EsPort: *esPort,
PrometheusPort: *prometheusPort, PrometheusPort: *prometheusPort,
EsIndex: *esIndex, EsIndex: *esIndex,
RefreshDelta: *refreshDelta, RefreshDelta: *refreshDelta,
CacheTTL: *cacheTTL, CacheTTL: *cacheTTL,
PeerFile: *peerFile, PeerFile: *peerFile,
Country: *country, Country: *country,
DisableEs: *disableEs, DisableEs: *disableEs,
Debug: *debug, Debug: *debug,
LoadPeers: *loadPeers, DisableLoadPeers: *disableLoadPeers,
StartPrometheus: *startPrometheus, DisableStartPrometheus: *disableStartPrometheus,
StartUDP: *startUdp, DisableStartUDP: *disableStartUdp,
WritePeers: *writePeers, DisableWritePeers: *disableWritePeers,
DisableFederation: *disableFederation, DisableFederation: *disableFederation,
} }
if esHost, ok := environment["ELASTIC_HOST"]; ok { if esHost, ok := environment["ELASTIC_HOST"]; ok {

View file

@ -237,7 +237,7 @@ func (s *Server) helloPeer(server *FederatedServer) (*pb.HelloMessage, error) {
// writePeers writes our current known peers to disk. // writePeers writes our current known peers to disk.
func (s *Server) writePeers() { func (s *Server) writePeers() {
if !s.Args.WritePeers { if s.Args.DisableWritePeers {
return return
} }
f, err := os.Create(s.Args.PeerFile) f, err := os.Create(s.Args.PeerFile)

View file

@ -45,23 +45,23 @@ func removeFile(fileName string) {
func makeDefaultArgs() *Args { func makeDefaultArgs() *Args {
args := &Args{ args := &Args{
CmdType: ServeCmd, CmdType: ServeCmd,
Host: DefaultHost, Host: DefaultHost,
Port: DefaultPort, Port: DefaultPort,
EsHost: DefaultEsHost, EsHost: DefaultEsHost,
EsPort: DefaultEsPort, EsPort: DefaultEsPort,
PrometheusPort: DefaultPrometheusPort, PrometheusPort: DefaultPrometheusPort,
EsIndex: DefaultEsIndex, EsIndex: DefaultEsIndex,
RefreshDelta: DefaultRefreshDelta, RefreshDelta: DefaultRefreshDelta,
CacheTTL: DefaultCacheTTL, CacheTTL: DefaultCacheTTL,
PeerFile: DefaultPeerFile, PeerFile: DefaultPeerFile,
Country: DefaultCountry, Country: DefaultCountry,
DisableEs: true, DisableEs: true,
Debug: true, Debug: true,
LoadPeers: false, DisableLoadPeers: true,
StartPrometheus: false, DisableStartPrometheus: true,
StartUDP: false, DisableStartUDP: true,
WritePeers: false, DisableWritePeers: true,
} }
return args return args
@ -129,7 +129,7 @@ func TestAddPeer(t *testing.T) {
func TestPeerWriter(t *testing.T) { func TestPeerWriter(t *testing.T) {
ctx := context.Background() ctx := context.Background()
args := makeDefaultArgs() args := makeDefaultArgs()
args.WritePeers = true args.DisableWritePeers = false
tests := []struct { tests := []struct {
name string name string
@ -426,10 +426,10 @@ func TestAddPeerEndpoint3(t *testing.T) {
func TestUDPServer(t *testing.T) { func TestUDPServer(t *testing.T) {
ctx := context.Background() ctx := context.Background()
args := makeDefaultArgs() args := makeDefaultArgs()
args.StartUDP = true args.DisableStartUDP = false
args2 := makeDefaultArgs() args2 := makeDefaultArgs()
args2.Port = "50052" args2.Port = "50052"
args2.StartUDP = true args2.DisableStartUDP = false
tests := []struct { tests := []struct {
name string name string

View file

@ -205,10 +205,10 @@ func MakeHubServer(ctx context.Context, args *Args) *Server {
} }
// Start up our background services // Start up our background services
if args.StartPrometheus { if !args.DisableStartPrometheus {
go s.prometheusEndpoint(s.Args.PrometheusPort, "metrics") go s.prometheusEndpoint(s.Args.PrometheusPort, "metrics")
} }
if args.StartUDP { if !args.DisableStartUDP {
go func() { go func() {
err := UDPServer(args) err := UDPServer(args)
if err != nil { if err != nil {
@ -217,7 +217,7 @@ func MakeHubServer(ctx context.Context, args *Args) *Server {
}() }()
} }
// Load peers from disk and subscribe to one if there are any // Load peers from disk and subscribe to one if there are any
if args.LoadPeers { if !args.DisableLoadPeers {
go func() { go func() {
err := s.loadPeers() err := s.loadPeers()
if err != nil { if err != nil {

View file

@ -10,7 +10,7 @@ import (
// TestAddPeer tests the ability to add peers // TestAddPeer tests the ability to add peers
func TestUDPPing(t *testing.T) { func TestUDPPing(t *testing.T) {
args := makeDefaultArgs() args := makeDefaultArgs()
args.StartUDP = false args.DisableStartUDP = true
tests := []struct { tests := []struct {
name string name string