Expose a Libp2p host's Ping service through HTTP.
go get github.com/drgomesp/go-libp2p-pinghttp
g
Initialize the http ping service with the libp2p host and start listening:
h1, err := libp2p.New()
if err != nil {
log.Fatal(err)
}
h2, err := libp2p.New()
if err != nil {
log.Fatal(err)
}
h1.Peerstore().AddAddrs(h2.ID(), h2.Addrs(), peerstore.PermanentAddrTTL)
ctx := context.Background()
svc, err := libp2pping.NewHttpPingService(ctx, h1, libp2pping.WithHttpAddr(":4000"))
if err != nil {
log.Fatal(err)
}
go func() {
_ = svc.ListenAndServe()
}()
log.Println(fmt.Sprintf("visit: http://localhost:4000/v1/ping?peerId=%s", h2.ID().String()))
<-ctx.Done()
Visiting the suggested URL will give you a response like this:
$ curl http://localhost:4000/v1/ping\?peerId\=12D3KooWNNU1i2FqRbekHLMFssUJwAxmDyJvKs4D7VXU8F3rBpYq | jq
{
"duration": "231.905µs",
"error": ""
}
PRs accepted.
MIT © Daniel Ribeiro