diff --git a/pkg/api/router.go b/pkg/api/router.go index 8e9346e005c..c86485ea5df 100644 --- a/pkg/api/router.go +++ b/pkg/api/router.go @@ -110,7 +110,6 @@ func (s *Service) mountTechnicalDebug() { handler = web.ChainHandlers(auth.PermissionCheckHandler(s.auth), web.FinalHandler(handler)) } s.router.Handle(path, handler) - s.router.Handle(rootPath+path, handler) } handle("/node", jsonhttp.MethodHandler{ "GET": http.HandlerFunc(s.nodeGetHandler), diff --git a/pkg/node/node.go b/pkg/node/node.go index 4f7286d36af..95d3247b2c6 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -402,6 +402,113 @@ func NewBee( } b.stamperStoreCloser = stamperStore + var apiService *api.Service + var debugService *api.Service + + if o.DebugAPIAddr != "" { + if o.MutexProfile { + _ = runtime.SetMutexProfileFraction(1) + } + + if o.BlockProfile { + runtime.SetBlockProfileRate(1) + } + + debugAPIListener, err := net.Listen("tcp", o.DebugAPIAddr) + if err != nil { + return nil, fmt.Errorf("debug api listener: %w", err) + } + + debugService = api.New( + *publicKey, + pssPrivateKey.PublicKey, + overlayEthAddress, + o.WhitelistedWithdrawalAddress, + logger, + transactionService, + batchStore, + beeNodeMode, + o.ChequebookEnable, + o.SwapEnable, + chainBackend, + o.CORSAllowedOrigins, + stamperStore, + ) + debugService.Restricted = o.Restricted + debugService.MountTechnicalDebug() + debugService.SetProbe(probe) + + debugAPIServer := &http.Server{ + IdleTimeout: 30 * time.Second, + ReadHeaderTimeout: 3 * time.Second, + Handler: debugService, + ErrorLog: stdlog.New(b.errorLogWriter, "", 0), + } + + go func() { + logger.Info("starting debug server", "address", debugAPIListener.Addr()) + + if err := debugAPIServer.Serve(debugAPIListener); err != nil && !errors.Is(err, http.ErrServerClosed) { + logger.Debug("debug api server failed to start", "error", err) + logger.Error(nil, "debug api server failed to start") + } + }() + + b.debugAPIServer = debugAPIServer + } + + if o.APIAddr != "" { + if o.MutexProfile { + _ = runtime.SetMutexProfileFraction(1) + } + if o.BlockProfile { + runtime.SetBlockProfileRate(1) + } + + apiServer := &http.Server{ + IdleTimeout: 30 * time.Second, + ReadHeaderTimeout: 3 * time.Second, + Handler: apiService, + ErrorLog: stdlog.New(b.errorLogWriter, "", 0), + } + + apiListener, err := net.Listen("tcp", o.APIAddr) + if err != nil { + return nil, fmt.Errorf("api listener: %w", err) + } + + apiService = api.New( + *publicKey, + pssPrivateKey.PublicKey, + overlayEthAddress, + o.WhitelistedWithdrawalAddress, + logger, + transactionService, + batchStore, + beeNodeMode, + o.ChequebookEnable, + o.SwapEnable, + chainBackend, + o.CORSAllowedOrigins, + stamperStore, + ) + apiService.Restricted = o.Restricted + apiService.MountTechnicalDebug() + apiService.SetProbe(probe) + + go func() { + logger.Info("starting debug & api server", "address", apiListener.Addr()) + + if err := apiServer.Serve(apiListener); err != nil && !errors.Is(err, http.ErrServerClosed) { + logger.Debug("debug & api server failed to start", "error", err) + logger.Error(nil, "debug & api server failed to start") + } + }() + + b.apiServer = apiServer + b.apiCloser = apiServer + } + // Sync the with the given Ethereum backend: isSynced, _, err := transaction.IsSynced(ctx, chainBackend, maxDelay) if err != nil { @@ -532,10 +639,9 @@ func NewBee( var registry *promc.Registry - var apiService *api.Service - - if o.APIAddr != "" { - apiService = api.New(*publicKey, pssPrivateKey.PublicKey, overlayEthAddress, o.WhitelistedWithdrawalAddress, logger, transactionService, batchStore, beeNodeMode, o.ChequebookEnable, o.SwapEnable, chainBackend, o.CORSAllowedOrigins, stamperStore) + if debugService != nil { + registry = debugService.MetricsRegistry() + } else if apiService != nil { registry = apiService.MetricsRegistry() } @@ -553,6 +659,8 @@ func NewBee( return nil, fmt.Errorf("p2p service: %w", err) } + apiService.SetP2P(p2ps) + b.p2pService = p2ps b.p2pHalter = p2ps @@ -996,52 +1104,6 @@ func NewBee( } if o.APIAddr != "" { - apiService.SetProbe(probe) - apiService.SetRedistributionAgent(agent) - apiService.SetP2P(p2ps) - apiService.SetSwarmAddress(&swarmAddress) - - if o.MutexProfile { - _ = runtime.SetMutexProfileFraction(1) - } - - if o.BlockProfile { - runtime.SetBlockProfileRate(1) - } - - apiService.Configure(signer, authenticator, tracer, api.Options{ - CORSAllowedOrigins: o.CORSAllowedOrigins, - WsPingPeriod: 60 * time.Second, - Restricted: o.Restricted, - }, extraOpts, chainID, erc20Service) - - apiService.MountTechnicalDebug() - apiService.MountAPI() - apiService.MountDebug() - - apiServer := &http.Server{ - IdleTimeout: 30 * time.Second, - ReadHeaderTimeout: 3 * time.Second, - Handler: apiService, - ErrorLog: stdlog.New(b.errorLogWriter, "", 0), - } - - apiListener, err := net.Listen("tcp", o.APIAddr) - if err != nil { - return nil, fmt.Errorf("api listener: %w", err) - } - - go func() { - logger.Info("starting api server", "address", apiListener.Addr()) - if err := apiServer.Serve(apiListener); err != nil && !errors.Is(err, http.ErrServerClosed) { - logger.Debug("api server failed to start", "error", err) - logger.Error(nil, "api server failed to start") - } - }() - - b.apiServer = apiServer - b.apiCloser = apiService - // register metrics from components apiService.MustRegisterMetrics(p2ps.Metrics()...) apiService.MustRegisterMetrics(pingPong.Metrics()...) @@ -1078,9 +1140,7 @@ func NewBee( if swapBackendMetrics, ok := chainBackend.(metrics.Collector); ok { apiService.MustRegisterMetrics(swapBackendMetrics.Metrics()...) } - if apiService != nil { - apiService.MustRegisterMetrics(apiService.Metrics()...) - } + if l, ok := logger.(metrics.Collector); ok { apiService.MustRegisterMetrics(l.Metrics()...) } @@ -1088,69 +1148,22 @@ func NewBee( if swapService != nil { apiService.MustRegisterMetrics(swapService.Metrics()...) } - } - - if o.DebugAPIAddr != "" { - debugService := api.New( - *publicKey, - pssPrivateKey.PublicKey, - overlayEthAddress, - o.WhitelistedWithdrawalAddress, - logger, - transactionService, - batchStore, - beeNodeMode, - o.ChequebookEnable, - o.SwapEnable, - chainBackend, - o.CORSAllowedOrigins, - stamperStore, - ) - - debugService.SetProbe(probe) - debugService.SetP2P(p2ps) - debugService.SetSwarmAddress(&swarmAddress) - debugService.SetRedistributionAgent(agent) - - if o.MutexProfile { - _ = runtime.SetMutexProfileFraction(1) - } - if o.BlockProfile { - runtime.SetBlockProfileRate(1) - } - - debugService.Configure(signer, authenticator, tracer, api.Options{ + apiService.Configure(signer, authenticator, tracer, api.Options{ CORSAllowedOrigins: o.CORSAllowedOrigins, WsPingPeriod: 60 * time.Second, Restricted: o.Restricted, }, extraOpts, chainID, erc20Service) - debugService.MountTechnicalDebug() - debugService.MountDebug() - - debugAPIServer := &http.Server{ - IdleTimeout: 30 * time.Second, - ReadHeaderTimeout: 3 * time.Second, - Handler: debugService, - ErrorLog: stdlog.New(b.errorLogWriter, "", 0), - } - - debugAPIListener, err := net.Listen("tcp", o.DebugAPIAddr) - if err != nil { - return nil, fmt.Errorf("debug api listener: %w", err) - } - go func() { - logger.Info("starting debug server", "address", debugAPIListener.Addr()) - - if err := debugAPIServer.Serve(debugAPIListener); err != nil && !errors.Is(err, http.ErrServerClosed) { - logger.Debug("debug api server failed to start", "error", err) - logger.Error(nil, "debug api server failed to start") - } - }() + apiService.MountAPI() + apiService.MountDebug() - b.debugAPIServer = debugAPIServer + debugService.SetP2P(p2ps) + debugService.SetSwarmAddress(&swarmAddress) + debugService.SetRedistributionAgent(agent) + } + if o.DebugAPIAddr != "" { // register metrics from components debugService.MustRegisterMetrics(p2ps.Metrics()...) debugService.MustRegisterMetrics(pingPong.Metrics()...) @@ -1197,6 +1210,17 @@ func NewBee( if swapService != nil { debugService.MustRegisterMetrics(swapService.Metrics()...) } + + debugService.Configure(signer, authenticator, tracer, api.Options{ + CORSAllowedOrigins: o.CORSAllowedOrigins, + WsPingPeriod: 60 * time.Second, + Restricted: o.Restricted, + }, extraOpts, chainID, erc20Service) + + debugService.SetP2P(p2ps) + debugService.SetSwarmAddress(&swarmAddress) + debugService.MountDebug() + debugService.SetRedistributionAgent(agent) } if err := kad.Start(ctx); err != nil {