From 283686ecac7130e481ea83d9744f1f6a39dc2883 Mon Sep 17 00:00:00 2001 From: Jeffrey Picard Date: Wed, 24 Nov 2021 18:56:34 -0500 Subject: [PATCH] Fix startup flags --- main.go | 14 +---- server/args.go | 110 +++++++++++++++++++------------------- server/federation.go | 2 +- server/federation_test.go | 40 +++++++------- server/server.go | 6 +-- server/udp_test.go | 2 +- 6 files changed, 81 insertions(+), 93 deletions(-) diff --git a/main.go b/main.go index f99ddc8..f87acd3 100644 --- a/main.go +++ b/main.go @@ -26,19 +26,7 @@ func main() { s := server.MakeHubServer(ctxWCancel, args) 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 } diff --git a/server/args.go b/server/args.go index 00b24c9..655de9a 100644 --- a/server/args.go +++ b/server/args.go @@ -16,42 +16,42 @@ const ( // Args struct contains the arguments to the hub server. type Args struct { - CmdType int - Host string - Port string - EsHost string - EsPort string - PrometheusPort string - EsIndex string - RefreshDelta int - CacheTTL int - PeerFile string - Country string - DisableEs bool - Debug bool - LoadPeers bool - StartPrometheus bool - StartUDP bool - WritePeers bool - DisableFederation bool + CmdType int + Host string + Port string + EsHost string + EsPort string + PrometheusPort string + EsIndex string + RefreshDelta int + CacheTTL int + PeerFile string + Country string + DisableEs bool + Debug bool + DisableLoadPeers bool + DisableStartPrometheus bool + DisableStartUDP bool + DisableWritePeers bool + DisableFederation bool } const ( - DefaultHost = "0.0.0.0" - DefaultPort = "50051" - DefaultEsHost = "http://localhost" - DefaultEsIndex = "claims" - DefaultEsPort = "9200" - DefaultPrometheusPort = "2112" - DefaultRefreshDelta = 5 - DefaultCacheTTL = 5 - DefaultPeerFile = "peers.txt" - DefaultCountry = "US" - DefaultLoadPeers = true - DefaultStartPrometheus = true - DefaultStartUDP = true - DefaultWritePeers = true - DefaultDisableFederation = false + DefaultHost = "0.0.0.0" + DefaultPort = "50051" + DefaultEsHost = "http://localhost" + DefaultEsIndex = "claims" + DefaultEsPort = "9200" + DefaultPrometheusPort = "2112" + DefaultRefreshDelta = 5 + DefaultCacheTTL = 5 + DefaultPeerFile = "peers.txt" + DefaultCountry = "US" + DefaultDisableLoadPeers = false + DefaultDisableStartPrometheus = false + DefaultDisableStartUDP = false + DefaultDisableWritePeers = false + DefaultDisableFederation = false ) // 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}) 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}) - startPrometheus := parser.Flag("", "start-prometheus", &argparse.Options{Required: false, Help: "Start prometheus server", Default: DefaultStartPrometheus}) - startUdp := parser.Flag("", "start-udp", &argparse.Options{Required: false, Help: "Start UDP ping server", Default: DefaultStartUDP}) - writePeers := parser.Flag("", "write-peers", &argparse.Options{Required: false, Help: "Write peer to disk as we learn about them", Default: DefaultWritePeers}) + disableLoadPeers := parser.Flag("", "disable-load-peers", &argparse.Options{Required: false, Help: "Disable load peers from disk at startup", Default: DefaultDisableLoadPeers}) + disableStartPrometheus := parser.Flag("", "disable-start-prometheus", &argparse.Options{Required: false, Help: "Disable start prometheus server", Default: DefaultDisableStartPrometheus}) + disableStartUdp := parser.Flag("", "disable-start-udp", &argparse.Options{Required: false, Help: "Disable start UDP ping server", Default: DefaultDisableStartUDP}) + 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}) text := parser.String("", "text", &argparse.Options{Required: false, Help: "text query"}) @@ -120,24 +120,24 @@ func ParseArgs(searchRequest *pb.SearchRequest) *Args { } args := &Args{ - CmdType: SearchCmd, - Host: *host, - Port: *port, - EsHost: *esHost, - EsPort: *esPort, - PrometheusPort: *prometheusPort, - EsIndex: *esIndex, - RefreshDelta: *refreshDelta, - CacheTTL: *cacheTTL, - PeerFile: *peerFile, - Country: *country, - DisableEs: *disableEs, - Debug: *debug, - LoadPeers: *loadPeers, - StartPrometheus: *startPrometheus, - StartUDP: *startUdp, - WritePeers: *writePeers, - DisableFederation: *disableFederation, + CmdType: SearchCmd, + Host: *host, + Port: *port, + EsHost: *esHost, + EsPort: *esPort, + PrometheusPort: *prometheusPort, + EsIndex: *esIndex, + RefreshDelta: *refreshDelta, + CacheTTL: *cacheTTL, + PeerFile: *peerFile, + Country: *country, + DisableEs: *disableEs, + Debug: *debug, + DisableLoadPeers: *disableLoadPeers, + DisableStartPrometheus: *disableStartPrometheus, + DisableStartUDP: *disableStartUdp, + DisableWritePeers: *disableWritePeers, + DisableFederation: *disableFederation, } if esHost, ok := environment["ELASTIC_HOST"]; ok { diff --git a/server/federation.go b/server/federation.go index abfabf1..304aff6 100644 --- a/server/federation.go +++ b/server/federation.go @@ -237,7 +237,7 @@ func (s *Server) helloPeer(server *FederatedServer) (*pb.HelloMessage, error) { // writePeers writes our current known peers to disk. func (s *Server) writePeers() { - if !s.Args.WritePeers { + if s.Args.DisableWritePeers { return } f, err := os.Create(s.Args.PeerFile) diff --git a/server/federation_test.go b/server/federation_test.go index 071d326..ed29dd2 100644 --- a/server/federation_test.go +++ b/server/federation_test.go @@ -45,23 +45,23 @@ func removeFile(fileName string) { func makeDefaultArgs() *Args { args := &Args{ - CmdType: ServeCmd, - Host: DefaultHost, - Port: DefaultPort, - EsHost: DefaultEsHost, - EsPort: DefaultEsPort, - PrometheusPort: DefaultPrometheusPort, - EsIndex: DefaultEsIndex, - RefreshDelta: DefaultRefreshDelta, - CacheTTL: DefaultCacheTTL, - PeerFile: DefaultPeerFile, - Country: DefaultCountry, - DisableEs: true, - Debug: true, - LoadPeers: false, - StartPrometheus: false, - StartUDP: false, - WritePeers: false, + CmdType: ServeCmd, + Host: DefaultHost, + Port: DefaultPort, + EsHost: DefaultEsHost, + EsPort: DefaultEsPort, + PrometheusPort: DefaultPrometheusPort, + EsIndex: DefaultEsIndex, + RefreshDelta: DefaultRefreshDelta, + CacheTTL: DefaultCacheTTL, + PeerFile: DefaultPeerFile, + Country: DefaultCountry, + DisableEs: true, + Debug: true, + DisableLoadPeers: true, + DisableStartPrometheus: true, + DisableStartUDP: true, + DisableWritePeers: true, } return args @@ -129,7 +129,7 @@ func TestAddPeer(t *testing.T) { func TestPeerWriter(t *testing.T) { ctx := context.Background() args := makeDefaultArgs() - args.WritePeers = true + args.DisableWritePeers = false tests := []struct { name string @@ -426,10 +426,10 @@ func TestAddPeerEndpoint3(t *testing.T) { func TestUDPServer(t *testing.T) { ctx := context.Background() args := makeDefaultArgs() - args.StartUDP = true + args.DisableStartUDP = false args2 := makeDefaultArgs() args2.Port = "50052" - args2.StartUDP = true + args2.DisableStartUDP = false tests := []struct { name string diff --git a/server/server.go b/server/server.go index ff45f19..16472ce 100644 --- a/server/server.go +++ b/server/server.go @@ -205,10 +205,10 @@ func MakeHubServer(ctx context.Context, args *Args) *Server { } // Start up our background services - if args.StartPrometheus { + if !args.DisableStartPrometheus { go s.prometheusEndpoint(s.Args.PrometheusPort, "metrics") } - if args.StartUDP { + if !args.DisableStartUDP { go func() { err := UDPServer(args) 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 - if args.LoadPeers { + if !args.DisableLoadPeers { go func() { err := s.loadPeers() if err != nil { diff --git a/server/udp_test.go b/server/udp_test.go index 43a521e..8b8db8c 100644 --- a/server/udp_test.go +++ b/server/udp_test.go @@ -10,7 +10,7 @@ import ( // TestAddPeer tests the ability to add peers func TestUDPPing(t *testing.T) { args := makeDefaultArgs() - args.StartUDP = false + args.DisableStartUDP = true tests := []struct { name string