github.com/aergoio/aergo@v1.3.1/cmd/polaris/polaris.go (about) 1 /* 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 package main 6 7 import ( 8 "fmt" 9 "github.com/aergoio/aergo-actor/actor" 10 "github.com/aergoio/aergo-lib/log" 11 "github.com/aergoio/aergo/config" 12 "github.com/aergoio/aergo/internal/common" 13 "github.com/aergoio/aergo/p2p/p2pkey" 14 "github.com/aergoio/aergo/pkg/component" 15 common2 "github.com/aergoio/aergo/polaris/common" 16 "github.com/aergoio/aergo/polaris/server" 17 "github.com/spf13/cobra" 18 "net/http" 19 _ "net/http/pprof" 20 "os" 21 ) 22 23 func main() { 24 if err := rootCmd.Execute(); err != nil { 25 os.Exit(1) 26 } 27 } 28 29 var ( 30 rootCmd = &cobra.Command{ 31 Use: "polaris", 32 Short: "Polaris node discovery service", 33 Long: "Polaris node discovery service for providing peer addresses to connect.", 34 Run: rootRun, 35 } 36 homePath string 37 configFilePath string 38 enableTestmode bool 39 svrlog *log.Logger 40 41 cfg *config.Config 42 ) 43 44 func init() { 45 cobra.OnInitialize(initConfig) 46 fs := rootCmd.PersistentFlags() 47 fs.StringVar(&homePath, "home", "", "path of aergo home") 48 fs.StringVar(&configFilePath, "config", "", "path of configuration file") 49 fs.BoolVar(&enableTestmode, "testmode", false, "enable unsafe test mode (skips certain validations)") 50 } 51 52 func initConfig() { 53 serverCtx := config.NewServerContext(homePath, configFilePath) 54 cfg = serverCtx.GetDefaultConfig().(*config.Config) 55 // change some different default props for polaris 56 arrangeDefaultCfgForPolaris(cfg) 57 err := serverCtx.LoadOrCreateConfig(cfg) 58 if err != nil { 59 fmt.Printf("Fail to load configuration file %v: %v", serverCtx.Vc.ConfigFileUsed(), err.Error()) 60 os.Exit(1) 61 } 62 if enableTestmode { 63 cfg.EnableTestmode = true 64 } 65 cfg.Consensus.EnableBp = false 66 } 67 68 func arrangeDefaultCfgForPolaris(cfg *config.Config) { 69 cfg.RPC.NetServicePort = common2.DefaultRPCPort 70 cfg.P2P.NetProtocolPort = common2.DefaultSrvPort 71 } 72 73 func rootRun(cmd *cobra.Command, args []string) { 74 75 svrlog = log.NewLogger("polaris") 76 svrlog.Info().Msg("POLARIS STARTED") 77 78 p2pkey.InitNodeInfo(&cfg.BaseConfig, cfg.P2P, githash, svrlog) 79 80 if cfg.EnableProfile { 81 svrlog.Info().Msgf("Enable Profiling on localhost: %d", cfg.ProfilePort) 82 go func() { 83 err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", cfg.ProfilePort), nil) 84 svrlog.Info().Err(err).Msg("Run Profile Server") 85 }() 86 } 87 88 if cfg.EnableTestmode { 89 svrlog.Warn().Msgf("Running with unsafe test mode. Turn off test mode for production use!") 90 } 91 92 p2pkey.InitNodeInfo(&cfg.BaseConfig, cfg.P2P, "TODO", svrlog) 93 94 compMng := component.NewComponentHub() 95 96 lNTC := server.NewNTContainer(cfg) 97 polarisSvc := server.NewPolarisService(cfg, lNTC) 98 rpcSvc := server.NewPolarisRPC(cfg) 99 100 // Register services to Hub. Don't need to do nil-check since Register 101 // function skips nil parameters. 102 compMng.Register(lNTC, polarisSvc, rpcSvc) 103 104 //consensusSvc, err := impl.New(cfg.Consensus, compMng, chainSvc) 105 //if err != nil { 106 // svrlog.Error().Err(err).Msg("Failed to start consensus service.") 107 // os.Exit(1) 108 //} 109 110 // All the services objects including Consensus must be created before the 111 // actors are started. 112 compMng.Start() 113 114 var interrupt = common.HandleKillSig(func() { 115 //consensus.Stop(consensusSvc) 116 compMng.Stop() 117 }, svrlog) 118 119 // Wait main routine to stop 120 <-interrupt.C 121 } 122 123 type RedirectService struct { 124 *component.BaseComponent 125 } 126 127 func NewRedirectService(cfg *config.Config, svcPid string) *RedirectService { 128 logger := log.NewLogger(svcPid) 129 rs := &RedirectService{} 130 rs.BaseComponent = component.NewBaseComponent(svcPid, rs, logger) 131 132 return rs 133 } 134 135 func (rs *RedirectService) Receive(context actor.Context) { 136 // ignore for now 137 } 138 139 func (rs *RedirectService) BeforeStart() {} 140 func (rs *RedirectService) AfterStart() {} 141 func (rs *RedirectService) BeforeStop() {} 142 143 func (rs *RedirectService) Statistics() *map[string]interface{} { 144 dummy := make(map[string]interface{}) 145 return &dummy 146 }