Commit 8d746790 authored by Janoš Guljaš's avatar Janoš Guljaš Committed by GitHub

move pingpong api to debug api (#280)

parent b5f84f47
...@@ -162,10 +162,10 @@ curl localhost:6062/addresses ...@@ -162,10 +162,10 @@ curl localhost:6062/addresses
Make sure that Debug API is enabled and address configured as in examples above. Make sure that Debug API is enabled and address configured as in examples above.
And use that address in the API call on another node, for example, local node 1: And use that address in the Debug API call on another node, for example, local node 1:
```sh ```sh
curl -XPOST localhost:8081/pingpong/d4440baf2d79e481c3c6fd93a2014d2e6fe0386418829439f26d13a8253d04f1 curl -XPOST localhost:6061/pingpong/d4440baf2d79e481c3c6fd93a2014d2e6fe0386418829439f26d13a8253d04f1
``` ```
## Generating protobuf ## Generating protobuf
......
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
"github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/logging"
m "github.com/ethersphere/bee/pkg/metrics" m "github.com/ethersphere/bee/pkg/metrics"
"github.com/ethersphere/bee/pkg/pingpong"
"github.com/ethersphere/bee/pkg/storage" "github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/tags" "github.com/ethersphere/bee/pkg/tags"
"github.com/ethersphere/bee/pkg/tracing" "github.com/ethersphere/bee/pkg/tracing"
...@@ -27,11 +26,10 @@ type server struct { ...@@ -27,11 +26,10 @@ type server struct {
} }
type Options struct { type Options struct {
Pingpong pingpong.Interface Tags *tags.Tags
Tags *tags.Tags Storer storage.Storer
Storer storage.Storer Logger logging.Logger
Logger logging.Logger Tracer *tracing.Tracer
Tracer *tracing.Tracer
} }
func New(o Options) Service { func New(o Options) Service {
......
...@@ -31,10 +31,9 @@ func newTestServer(t *testing.T, o testServerOptions) *http.Client { ...@@ -31,10 +31,9 @@ func newTestServer(t *testing.T, o testServerOptions) *http.Client {
o.Logger = logging.New(ioutil.Discard, 0) o.Logger = logging.New(ioutil.Discard, 0)
} }
s := api.New(api.Options{ s := api.New(api.Options{
Pingpong: o.Pingpong, Tags: o.Tags,
Tags: o.Tags, Storer: o.Storer,
Storer: o.Storer, Logger: o.Logger,
Logger: o.Logger,
}) })
ts := httptest.NewServer(s) ts := httptest.NewServer(s)
t.Cleanup(ts.Close) t.Cleanup(ts.Close)
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
package api package api
type ( type (
PingpongResponse = pingpongResponse RawPostResponse = rawPostResponse
RawPostResponse = rawPostResponse TagResponse = tagResponse
TagResponse = tagResponse
) )
...@@ -38,12 +38,6 @@ func newMetrics() metrics { ...@@ -38,12 +38,6 @@ func newMetrics() metrics {
Help: "Histogram of API response durations.", Help: "Histogram of API response durations.",
Buckets: []float64{0.01, 0.1, 0.25, 0.5, 1, 2.5, 5, 10}, Buckets: []float64{0.01, 0.1, 0.25, 0.5, 1, 2.5, 5, 10},
}), }),
PingRequestCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "ping_request_count",
Help: "Number HTTP API ping requests.",
}),
} }
} }
......
...@@ -28,10 +28,6 @@ func (s *server) setupRouting() { ...@@ -28,10 +28,6 @@ func (s *server) setupRouting() {
fmt.Fprintln(w, "User-agent: *\nDisallow: /") fmt.Fprintln(w, "User-agent: *\nDisallow: /")
}) })
router.Handle("/pingpong/{peer-id}", jsonhttp.MethodHandler{
"POST": http.HandlerFunc(s.pingpongHandler),
})
router.Handle("/bzz-raw", jsonhttp.MethodHandler{ router.Handle("/bzz-raw", jsonhttp.MethodHandler{
"POST": http.HandlerFunc(s.rawUploadHandler), "POST": http.HandlerFunc(s.rawUploadHandler),
}) })
......
...@@ -10,10 +10,12 @@ import ( ...@@ -10,10 +10,12 @@ import (
"github.com/ethersphere/bee/pkg/addressbook" "github.com/ethersphere/bee/pkg/addressbook"
"github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/p2p" "github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/pingpong"
"github.com/ethersphere/bee/pkg/storage" "github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm" "github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/tags" "github.com/ethersphere/bee/pkg/tags"
"github.com/ethersphere/bee/pkg/topology" "github.com/ethersphere/bee/pkg/topology"
"github.com/ethersphere/bee/pkg/tracing"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
...@@ -32,10 +34,12 @@ type server struct { ...@@ -32,10 +34,12 @@ type server struct {
type Options struct { type Options struct {
Overlay swarm.Address Overlay swarm.Address
P2P p2p.Service P2P p2p.Service
Pingpong pingpong.Interface
Addressbook addressbook.GetPutter Addressbook addressbook.GetPutter
TopologyDriver topology.Notifier TopologyDriver topology.Notifier
Storer storage.Storer Storer storage.Storer
Logger logging.Logger Logger logging.Logger
Tracer *tracing.Tracer
Tags *tags.Tags Tags *tags.Tags
} }
......
...@@ -16,6 +16,7 @@ import ( ...@@ -16,6 +16,7 @@ import (
"github.com/ethersphere/bee/pkg/debugapi" "github.com/ethersphere/bee/pkg/debugapi"
"github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/p2p" "github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/pingpong"
mockstore "github.com/ethersphere/bee/pkg/statestore/mock" mockstore "github.com/ethersphere/bee/pkg/statestore/mock"
"github.com/ethersphere/bee/pkg/storage" "github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm" "github.com/ethersphere/bee/pkg/swarm"
...@@ -29,6 +30,7 @@ import ( ...@@ -29,6 +30,7 @@ import (
type testServerOptions struct { type testServerOptions struct {
Overlay swarm.Address Overlay swarm.Address
P2P p2p.Service P2P p2p.Service
Pingpong pingpong.Interface
Storer storage.Storer Storer storage.Storer
TopologyOpts []mock.Option TopologyOpts []mock.Option
Tags *tags.Tags Tags *tags.Tags
...@@ -48,6 +50,7 @@ func newTestServer(t *testing.T, o testServerOptions) *testServer { ...@@ -48,6 +50,7 @@ func newTestServer(t *testing.T, o testServerOptions) *testServer {
s := debugapi.New(debugapi.Options{ s := debugapi.New(debugapi.Options{
Overlay: o.Overlay, Overlay: o.Overlay,
P2P: o.P2P, P2P: o.P2P,
Pingpong: o.Pingpong,
Tags: o.Tags, Tags: o.Tags,
Logger: logging.New(ioutil.Discard, 0), Logger: logging.New(ioutil.Discard, 0),
Addressbook: addrbook, Addressbook: addrbook,
......
...@@ -6,6 +6,7 @@ package debugapi ...@@ -6,6 +6,7 @@ package debugapi
type ( type (
StatusResponse = statusResponse StatusResponse = statusResponse
PingpongResponse = pingpongResponse
PeerConnectResponse = peerConnectResponse PeerConnectResponse = peerConnectResponse
PeersResponse = peersResponse PeersResponse = peersResponse
AddressesResponse = addressesResponse AddressesResponse = addressesResponse
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package api package debugapi
import ( import (
"errors" "errors"
...@@ -44,7 +44,6 @@ func (s *server) pingpongHandler(w http.ResponseWriter, r *http.Request) { ...@@ -44,7 +44,6 @@ func (s *server) pingpongHandler(w http.ResponseWriter, r *http.Request) {
jsonhttp.InternalServerError(w, nil) jsonhttp.InternalServerError(w, nil)
return return
} }
s.metrics.PingRequestCount.Inc()
logger.Infof("pingpong succeeded to peer %s", peerID) logger.Infof("pingpong succeeded to peer %s", peerID)
jsonhttp.OK(w, pingpongResponse{ jsonhttp.OK(w, pingpongResponse{
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package api_test package debugapi_test
import ( import (
"context" "context"
...@@ -11,7 +11,7 @@ import ( ...@@ -11,7 +11,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/ethersphere/bee/pkg/api" "github.com/ethersphere/bee/pkg/debugapi"
"github.com/ethersphere/bee/pkg/jsonhttp" "github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest" "github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
"github.com/ethersphere/bee/pkg/p2p" "github.com/ethersphere/bee/pkg/p2p"
...@@ -36,39 +36,39 @@ func TestPingpong(t *testing.T) { ...@@ -36,39 +36,39 @@ func TestPingpong(t *testing.T) {
return rtt, nil return rtt, nil
}) })
client := newTestServer(t, testServerOptions{ ts := newTestServer(t, testServerOptions{
Pingpong: pingpongService, Pingpong: pingpongService,
}) })
t.Run("ok", func(t *testing.T) { t.Run("ok", func(t *testing.T) {
jsonhttptest.ResponseDirect(t, client, http.MethodPost, "/pingpong/"+peerID.String(), nil, http.StatusOK, api.PingpongResponse{ jsonhttptest.ResponseDirect(t, ts.Client, http.MethodPost, "/pingpong/"+peerID.String(), nil, http.StatusOK, debugapi.PingpongResponse{
RTT: rtt.String(), RTT: rtt.String(),
}) })
}) })
t.Run("peer not found", func(t *testing.T) { t.Run("peer not found", func(t *testing.T) {
jsonhttptest.ResponseDirect(t, client, http.MethodPost, "/pingpong/"+unknownPeerID.String(), nil, http.StatusNotFound, jsonhttp.StatusResponse{ jsonhttptest.ResponseDirect(t, ts.Client, http.MethodPost, "/pingpong/"+unknownPeerID.String(), nil, http.StatusNotFound, jsonhttp.StatusResponse{
Code: http.StatusNotFound, Code: http.StatusNotFound,
Message: "peer not found", Message: "peer not found",
}) })
}) })
t.Run("invalid peer address", func(t *testing.T) { t.Run("invalid peer address", func(t *testing.T) {
jsonhttptest.ResponseDirect(t, client, http.MethodPost, "/pingpong/invalid-address", nil, http.StatusBadRequest, jsonhttp.StatusResponse{ jsonhttptest.ResponseDirect(t, ts.Client, http.MethodPost, "/pingpong/invalid-address", nil, http.StatusBadRequest, jsonhttp.StatusResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
Message: "invalid peer address", Message: "invalid peer address",
}) })
}) })
t.Run("error", func(t *testing.T) { t.Run("error", func(t *testing.T) {
jsonhttptest.ResponseDirect(t, client, http.MethodPost, "/pingpong/"+errorPeerID.String(), nil, http.StatusInternalServerError, jsonhttp.StatusResponse{ jsonhttptest.ResponseDirect(t, ts.Client, http.MethodPost, "/pingpong/"+errorPeerID.String(), nil, http.StatusInternalServerError, jsonhttp.StatusResponse{
Code: http.StatusInternalServerError, Code: http.StatusInternalServerError,
Message: http.StatusText(http.StatusInternalServerError), // do not leak internal error Message: http.StatusText(http.StatusInternalServerError), // do not leak internal error
}) })
}) })
t.Run("get method not allowed", func(t *testing.T) { t.Run("get method not allowed", func(t *testing.T) {
jsonhttptest.ResponseDirect(t, client, http.MethodGet, "/pingpong/"+peerID.String(), nil, http.StatusMethodNotAllowed, jsonhttp.StatusResponse{ jsonhttptest.ResponseDirect(t, ts.Client, http.MethodGet, "/pingpong/"+peerID.String(), nil, http.StatusMethodNotAllowed, jsonhttp.StatusResponse{
Code: http.StatusMethodNotAllowed, Code: http.StatusMethodNotAllowed,
Message: http.StatusText(http.StatusMethodNotAllowed), Message: http.StatusText(http.StatusMethodNotAllowed),
}) })
......
...@@ -50,6 +50,10 @@ func (s *server) setupRouting() { ...@@ -50,6 +50,10 @@ func (s *server) setupRouting() {
web.FinalHandlerFunc(s.statusHandler), web.FinalHandlerFunc(s.statusHandler),
)) ))
router.Handle("/pingpong/{peer-id}", jsonhttp.MethodHandler{
"POST": http.HandlerFunc(s.pingpongHandler),
})
router.Handle("/addresses", jsonhttp.MethodHandler{ router.Handle("/addresses", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.addressesHandler), "GET": http.HandlerFunc(s.addressesHandler),
}) })
......
...@@ -242,11 +242,10 @@ func NewBee(o Options) (*Bee, error) { ...@@ -242,11 +242,10 @@ func NewBee(o Options) (*Bee, error) {
if o.APIAddr != "" { if o.APIAddr != "" {
// API server // API server
apiService = api.New(api.Options{ apiService = api.New(api.Options{
Pingpong: pingPong, Tags: tag,
Tags: tag, Storer: ns,
Storer: ns, Logger: logger,
Logger: logger, Tracer: tracer,
Tracer: tracer,
}) })
apiListener, err := net.Listen("tcp", o.APIAddr) apiListener, err := net.Listen("tcp", o.APIAddr)
if err != nil { if err != nil {
...@@ -275,7 +274,9 @@ func NewBee(o Options) (*Bee, error) { ...@@ -275,7 +274,9 @@ func NewBee(o Options) (*Bee, error) {
debugAPIService := debugapi.New(debugapi.Options{ debugAPIService := debugapi.New(debugapi.Options{
Overlay: address, Overlay: address,
P2P: p2ps, P2P: p2ps,
Pingpong: pingPong,
Logger: logger, Logger: logger,
Tracer: tracer,
Addressbook: addressbook, Addressbook: addressbook,
TopologyDriver: topologyDriver, TopologyDriver: topologyDriver,
Storer: storer, Storer: storer,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment