Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mybee
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
mybee
Commits
b120c01b
Unverified
Commit
b120c01b
authored
Apr 01, 2020
by
Janoš Guljaš
Committed by
GitHub
Apr 01, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add debugapi /addresses endpoint to expose libp2p mutiaddresses (#44)
parent
e67df738
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
119 additions
and
0 deletions
+119
-0
debugapi_test.go
pkg/debugapi/debugapi_test.go
+11
-0
export_test.go
pkg/debugapi/export_test.go
+1
-0
p2p.go
pkg/debugapi/p2p.go
+28
-0
p2p_test.go
pkg/debugapi/p2p_test.go
+61
-0
router.go
pkg/debugapi/router.go
+3
-0
mock.go
pkg/p2p/mock/mock.go
+14
-0
p2p.go
pkg/p2p/p2p.go
+1
-0
No files found.
pkg/debugapi/debugapi_test.go
View file @
b120c01b
...
...
@@ -17,6 +17,7 @@ import (
"github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/topology/mock"
"github.com/multiformats/go-multiaddr"
"resenje.org/web"
)
...
...
@@ -61,3 +62,13 @@ func newTestServer(t *testing.T, o testServerOptions) *testServer {
Cleanup
:
cleanup
,
}
}
func
mustMultiaddr
(
t
*
testing
.
T
,
s
string
)
multiaddr
.
Multiaddr
{
t
.
Helper
()
a
,
err
:=
multiaddr
.
NewMultiaddr
(
s
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
return
a
}
pkg/debugapi/export_test.go
View file @
b120c01b
...
...
@@ -8,4 +8,5 @@ type (
StatusResponse
=
statusResponse
PeerConnectResponse
=
peerConnectResponse
PeersResponse
=
peersResponse
AddressesResponse
=
addressesResponse
)
pkg/debugapi/p2p.go
0 → 100644
View file @
b120c01b
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
debugapi
import
(
"net/http"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/multiformats/go-multiaddr"
)
type
addressesResponse
struct
{
Addresses
[]
multiaddr
.
Multiaddr
`json:"addresses"`
}
func
(
s
*
server
)
addressesHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
addresses
,
err
:=
s
.
P2P
.
Addresses
()
if
err
!=
nil
{
s
.
Logger
.
Debugf
(
"debug api: p2p addresses: %v"
,
err
)
jsonhttp
.
InternalServerError
(
w
,
err
)
return
}
jsonhttp
.
OK
(
w
,
addressesResponse
{
Addresses
:
addresses
,
})
}
pkg/debugapi/p2p_test.go
0 → 100644
View file @
b120c01b
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
debugapi_test
import
(
"errors"
"net/http"
"testing"
"github.com/ethersphere/bee/pkg/debugapi"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
"github.com/ethersphere/bee/pkg/p2p/mock"
"github.com/multiformats/go-multiaddr"
)
func
TestAddresses
(
t
*
testing
.
T
)
{
addresses
:=
[]
multiaddr
.
Multiaddr
{
mustMultiaddr
(
t
,
"/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb"
),
mustMultiaddr
(
t
,
"/ip4/192.168.0.101/tcp/7071/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb"
),
mustMultiaddr
(
t
,
"/ip4/127.0.0.1/udp/7071/quic/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb"
),
}
testServer
:=
newTestServer
(
t
,
testServerOptions
{
P2P
:
mock
.
New
(
mock
.
WithAddressesFunc
(
func
()
([]
multiaddr
.
Multiaddr
,
error
)
{
return
addresses
,
nil
})),
})
defer
testServer
.
Cleanup
()
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/addresses"
,
nil
,
http
.
StatusOK
,
debugapi
.
AddressesResponse
{
Addresses
:
addresses
,
})
})
t
.
Run
(
"post method not allowed"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodPost
,
"/addresses"
,
nil
,
http
.
StatusMethodNotAllowed
,
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusMethodNotAllowed
,
Message
:
http
.
StatusText
(
http
.
StatusMethodNotAllowed
),
})
})
}
func
TestAddresses_error
(
t
*
testing
.
T
)
{
testErr
:=
errors
.
New
(
"test error"
)
testServer
:=
newTestServer
(
t
,
testServerOptions
{
P2P
:
mock
.
New
(
mock
.
WithAddressesFunc
(
func
()
([]
multiaddr
.
Multiaddr
,
error
)
{
return
nil
,
testErr
})),
})
defer
testServer
.
Cleanup
()
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/addresses"
,
nil
,
http
.
StatusInternalServerError
,
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusInternalServerError
,
Message
:
testErr
.
Error
(),
})
}
pkg/debugapi/router.go
View file @
b120c01b
...
...
@@ -40,6 +40,9 @@ func (s *server) setupRouting() {
router
.
HandleFunc
(
"/health"
,
s
.
statusHandler
)
router
.
HandleFunc
(
"/readiness"
,
s
.
statusHandler
)
router
.
Handle
(
"/addresses"
,
jsonhttp
.
MethodHandler
{
"GET"
:
http
.
HandlerFunc
(
s
.
addressesHandler
),
})
router
.
Handle
(
"/connect/{multi-address:.+}"
,
jsonhttp
.
MethodHandler
{
"POST"
:
http
.
HandlerFunc
(
s
.
peerConnectHandler
),
})
...
...
pkg/p2p/mock/mock.go
View file @
b120c01b
...
...
@@ -19,6 +19,7 @@ type Service struct {
disconnectFunc
func
(
overlay
swarm
.
Address
)
error
peersFunc
func
()
[]
p2p
.
Peer
setPeerAddedHandlerFunc
func
(
func
(
context
.
Context
,
swarm
.
Address
)
error
)
addressesFunc
func
()
([]
ma
.
Multiaddr
,
error
)
}
func
WithAddProtocolFunc
(
f
func
(
p2p
.
ProtocolSpec
)
error
)
Option
{
...
...
@@ -51,6 +52,12 @@ func WithSetPeerAddedHandlerFunc(f func(func(context.Context, swarm.Address) err
})
}
func
WithAddressesFunc
(
f
func
()
([]
ma
.
Multiaddr
,
error
))
Option
{
return
optionFunc
(
func
(
s
*
Service
)
{
s
.
addressesFunc
=
f
})
}
func
New
(
opts
...
Option
)
*
Service
{
s
:=
new
(
Service
)
for
_
,
o
:=
range
opts
{
...
...
@@ -88,6 +95,13 @@ func (s *Service) SetPeerAddedHandler(f func(context.Context, swarm.Address) err
s
.
setPeerAddedHandlerFunc
(
f
)
}
func
(
s
*
Service
)
Addresses
()
([]
ma
.
Multiaddr
,
error
)
{
if
s
.
addressesFunc
==
nil
{
return
nil
,
errors
.
New
(
"function Addresses not configured"
)
}
return
s
.
addressesFunc
()
}
func
(
s
*
Service
)
Peers
()
[]
p2p
.
Peer
{
if
s
.
peersFunc
==
nil
{
return
nil
...
...
pkg/p2p/p2p.go
View file @
b120c01b
...
...
@@ -19,6 +19,7 @@ type Service interface {
Disconnect
(
overlay
swarm
.
Address
)
error
Peers
()
[]
Peer
SetPeerAddedHandler
(
func
(
context
.
Context
,
swarm
.
Address
)
error
)
Addresses
()
([]
ma
.
Multiaddr
,
error
)
}
// Streamer is able to create a new Stream.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment