Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): add api initialization notice #4869

Merged
merged 13 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ type Service struct {
redistributionAgent *storageincentives.Agent

statusService *status.Service

isFullAPIEnabled bool
}

func (s *Service) SetP2P(p2p p2p.DebugService) {
Expand Down
14 changes: 5 additions & 9 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func newTestServer(t *testing.T, o testServerOptions) (*http.Client, *websocket.
erc20 := erc20mock.New(o.Erc20Opts...)
backend := backendmock.New(o.BackendOpts...)

var extraOpts = api.ExtraOptions{
extraOpts := api.ExtraOptions{
TopologyDriver: topologyDriver,
Accounting: acc,
Pseudosettle: recipient,
Expand Down Expand Up @@ -231,9 +231,7 @@ func newTestServer(t *testing.T, o testServerOptions) (*http.Client, *websocket.
WsPingPeriod: o.WsPingPeriod,
}, extraOpts, 1, erc20)

s.MountTechnicalDebug()
s.MountDebug()
s.MountAPI()
s.Mount(api.WithFullAPI())

if o.DirectUpload {
chanStore = newChanStore(o.Storer.PusherFeed())
Expand Down Expand Up @@ -316,7 +314,7 @@ func TestParseName(t *testing.T) {
const bzzHash = "89c17d0d8018a19057314aa035e61c9d23c47581a61dd3a79a7839692c617e4d"
log := log.Noop

var errInvalidNameOrAddress = errors.New("invalid name or bzz address")
errInvalidNameOrAddress := errors.New("invalid name or bzz address")

testCases := []struct {
desc string
Expand Down Expand Up @@ -377,7 +375,7 @@ func TestParseName(t *testing.T) {

s := api.New(pk.PublicKey, pk.PublicKey, common.Address{}, nil, log, nil, nil, 1, false, false, nil, []string{"*"}, inmemstore.New())
s.Configure(signer, nil, api.Options{}, api.ExtraOptions{Resolver: tC.res}, 1, nil)
s.MountAPI()
s.Mount(api.WithFullAPI())

tC := tC
t.Run(tC.desc, func(t *testing.T) {
Expand Down Expand Up @@ -503,9 +501,7 @@ func TestPostageHeaderError(t *testing.T) {
func TestOptions(t *testing.T) {
t.Parallel()

var (
client, _, _, _ = newTestServer(t, testServerOptions{})
)
client, _, _, _ := newTestServer(t, testServerOptions{})
for _, tc := range []struct {
endpoint string
expectedMethods string // expectedMethods contains HTTP methods like GET, POST, HEAD, PATCH, DELETE, OPTIONS. These are in alphabetical sorted order
Expand Down
69 changes: 43 additions & 26 deletions pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,18 @@ const (
rootPath = "/" + apiVersion
)

func (s *Service) MountTechnicalDebug() {
type MountOption func(*Service)

func (s *Service) Mount(opts ...MountOption) {
router := mux.NewRouter()

router.NotFoundHandler = http.HandlerFunc(jsonhttp.NotFoundHandler)

s.router = router

s.mountTechnicalDebug()

s.Handler = web.ChainHandlers(
httpaccess.NewHTTPAccessLogHandler(s.logger, s.tracer, "api access"),
handlers.CompressHandler,
s.corsHandler,
web.NoCacheHeadersHandler,
web.FinalHandler(router),
)
}

func (s *Service) MountDebug() {
s.mountBusinessDebug()
s.mountAPI()

s.Handler = web.ChainHandlers(
httpaccess.NewHTTPAccessLogHandler(s.logger, s.tracer, "api access"),
Expand All @@ -51,15 +45,26 @@ func (s *Service) MountDebug() {
web.NoCacheHeadersHandler,
web.FinalHandler(s.router),
)

for _, o := range opts {
o(s)
}
}

func (s *Service) MountAPI() {
if s.router == nil {
s.router = mux.NewRouter()
s.router.NotFoundHandler = http.HandlerFunc(jsonhttp.NotFoundHandler)
// WithFullAPI will enable all available endpoints, because some endpoints are not available during syncing.
func WithFullAPI() MountOption {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why have an option if we can explicitly enable the the full API using EnableFullAPI()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial idea to provide options is for unit testing purposes. Was considering to add WithFullAPIDisabled option as well. And then expand testServerOptions in api_test.go.
Also it can be used in one line to mount and enable apiService.Mount(api.WithFullAPI()), which is the case in devnode.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Option is removed

return func(s *Service) {
s.EnableFullAPI()
}
}

s.mountAPI()
// EnableFullAPI will enable all available endpoints, because some endpoints are not available during syncing.
func (s *Service) EnableFullAPI() {
if s == nil {
return
}

s.isFullAPIEnabled = true

compressHandler := func(h http.Handler) http.Handler {
downloadEndpoints := []string{
Expand Down Expand Up @@ -140,11 +145,11 @@ func (s *Service) mountTechnicalDebug() {
u.Path += "/"
http.Redirect(w, r, u.String(), http.StatusPermanentRedirect)
}))

s.router.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
s.router.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
s.router.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
s.router.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))

s.router.PathPrefix("/debug/pprof/").Handler(http.HandlerFunc(pprof.Index))
s.router.Handle("/debug/vars", expvar.Handler())

Expand All @@ -154,12 +159,14 @@ func (s *Service) mountTechnicalDebug() {
web.FinalHandlerFunc(s.loggerGetHandler),
),
})

s.router.Handle("/loggers/{exp}", jsonhttp.MethodHandler{
"GET": web.ChainHandlers(
httpaccess.NewHTTPAccessSuppressLogHandler(),
web.FinalHandlerFunc(s.loggerGetHandler),
),
})

s.router.Handle("/loggers/{exp}/{verbosity}", jsonhttp.MethodHandler{
"PUT": web.ChainHandlers(
httpaccess.NewHTTPAccessSuppressLogHandler(),
Expand All @@ -178,6 +185,16 @@ func (s *Service) mountTechnicalDebug() {
))
}

func (s *Service) checkRouteAvailability(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !s.isFullAPIEnabled {
jsonhttp.ServiceUnavailable(w, "Node is syncing. This endpoint is unavailable. Try again later.")
return
}
handler.ServeHTTP(w, r)
})
}

func (s *Service) mountAPI() {
subdomainRouter := s.router.Host("{subdomain:.*}.swarm.localhost").Subrouter()

Expand All @@ -197,8 +214,9 @@ func (s *Service) mountAPI() {

// handle is a helper closure which simplifies the router setup.
handle := func(path string, handler http.Handler) {
s.router.Handle(path, handler)
s.router.Handle(rootPath+path, handler)
routeHandler := s.checkRouteAvailability(handler)
s.router.Handle(path, routeHandler)
s.router.Handle(rootPath+path, routeHandler)
}

handle("/bytes", jsonhttp.MethodHandler{
Expand Down Expand Up @@ -384,14 +402,16 @@ func (s *Service) mountAPI() {

func (s *Service) mountBusinessDebug() {
handle := func(path string, handler http.Handler) {
s.router.Handle(path, handler)
s.router.Handle(rootPath+path, handler)
routeHandler := s.checkRouteAvailability(handler)
s.router.Handle(path, routeHandler)
s.router.Handle(rootPath+path, routeHandler)
}

if s.transaction != nil {
handle("/transactions", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.transactionListHandler),
})

handle("/transactions/{hash}", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.transactionDetailHandler),
"POST": http.HandlerFunc(s.transactionResendHandler),
Expand Down Expand Up @@ -423,10 +443,6 @@ func (s *Service) mountBusinessDebug() {
"DELETE": http.HandlerFunc(s.peerDisconnectHandler),
})

//handle("/chunks/{address}", jsonhttp.MethodHandler{
// "GET": http.HandlerFunc(s.hasChunkHandler),
//})

handle("/topology", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.topologyHandler),
})
Expand Down Expand Up @@ -511,6 +527,7 @@ func (s *Service) mountBusinessDebug() {
handle("/wallet", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.walletHandler),
})

if s.swapEnabled {
handle("/wallet/withdraw/{coin}", jsonhttp.MethodHandler{
"POST": web.ChainHandlers(
Expand Down
1 change: 0 additions & 1 deletion pkg/jsonhttp/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func HandleMethods(methods map[string]http.Handler, body string, contentType str
w.Header().Set("Content-Type", contentType)
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintln(w, body)

}

func NotFoundHandler(w http.ResponseWriter, _ *http.Request) {
Expand Down
22 changes: 8 additions & 14 deletions pkg/node/devnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func NewDevBee(logger log.Logger, o *DevOptions) (b *DevBee, err error) {
return nil, fmt.Errorf("blockchain address: %w", err)
}

var mockTransaction = transactionmock.New(transactionmock.WithPendingTransactionsFunc(func() ([]common.Hash, error) {
mockTransaction := transactionmock.New(transactionmock.WithPendingTransactionsFunc(func() ([]common.Hash, error) {
return []common.Hash{common.HexToHash("abcd")}, nil
}), transactionmock.WithResendTransactionFunc(func(ctx context.Context, txHash common.Hash) error {
return nil
Expand Down Expand Up @@ -303,13 +303,11 @@ func NewDevBee(logger log.Logger, o *DevOptions) (b *DevBee, err error) {
))
)

var (
// syncStatusFn mocks sync status because complete sync is required in order to curl certain apis e.g. /stamps.
// this allows accessing those apis by passing true to isDone in devNode.
syncStatusFn = func() (isDone bool, err error) {
return true, nil
}
)
// syncStatusFn mocks sync status because complete sync is required in order to curl certain apis e.g. /stamps.
// this allows accessing those apis by passing true to isDone in devNode.
syncStatusFn := func() (isDone bool, err error) {
return true, nil
}

mockFeeds := factory.New(localStore.Download(true))
mockResolver := resolverMock.NewResolver()
Expand Down Expand Up @@ -351,7 +349,7 @@ func NewDevBee(logger log.Logger, o *DevOptions) (b *DevBee, err error) {
SyncStatus: syncStatusFn,
}

var erc20 = erc20mock.New(
erc20 := erc20mock.New(
erc20mock.WithBalanceOfFunc(func(ctx context.Context, address common.Address) (*big.Int, error) {
return big.NewInt(0), nil
}),
Expand All @@ -366,10 +364,8 @@ func NewDevBee(logger log.Logger, o *DevOptions) (b *DevBee, err error) {
CORSAllowedOrigins: o.CORSAllowedOrigins,
WsPingPeriod: 60 * time.Second,
}, debugOpts, 1, erc20)
apiService.MountTechnicalDebug()
apiService.MountDebug()
apiService.MountAPI()

apiService.Mount(api.WithFullAPI())
apiService.SetProbe(probe)
apiService.SetP2P(p2ps)
apiService.SetSwarmAddress(&swarmAddress)
Expand Down Expand Up @@ -444,7 +440,6 @@ func pong(_ context.Context, _ swarm.Address, _ ...string) (rtt time.Duration, e
}

func randomAddress() (swarm.Address, error) {

b := make([]byte, 32)

_, err := rand.Read(b)
Expand All @@ -453,5 +448,4 @@ func randomAddress() (swarm.Address, error) {
}

return swarm.NewAddress(b), nil

}
6 changes: 3 additions & 3 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,8 @@ func NewBee(
o.CORSAllowedOrigins,
stamperStore,
)
apiService.MountTechnicalDebug()

apiService.Mount()
apiService.SetProbe(probe)

apiService.SetSwarmAddress(&swarmAddress)
Expand Down Expand Up @@ -1184,8 +1185,7 @@ func NewBee(
WsPingPeriod: 60 * time.Second,
}, extraOpts, chainID, erc20Service)

apiService.MountDebug()
apiService.MountAPI()
apiService.EnableFullAPI()

apiService.SetRedistributionAgent(agent)
}
Expand Down
Loading