/go-libp2p-pinghttp

Expose a Libp2p host's Ping service through HTTP

Primary LanguageGoMIT LicenseMIT

go-libp2p-pinghttp

madeby Go Report Card build codecov

Expose a Libp2p host's Ping service through HTTP.

Table of Contents

Install

go get github.com/drgomesp/go-libp2p-pinghttp

g

Features

Usage

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": ""
}

Contributing

PRs accepted.

License

MIT © Daniel Ribeiro