libp2pping

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2022 License: MIT Imports: 8 Imported by: 0

README

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HttpPingService

type HttpPingService struct {
	v1.UnimplementedPingServiceServer
	// contains filtered or unexported fields
}

func NewHttpPingService

func NewHttpPingService(ctx context.Context, h host.Host, opts ...ServiceOption) (*HttpPingService, error)

func (*HttpPingService) ListenAndServe

func (s *HttpPingService) ListenAndServe() error

func (*HttpPingService) Ping

type ServiceOption

type ServiceOption func(service *HttpPingService)

func WithHttpAddr

func WithHttpAddr(httpAddr string) ServiceOption

func WithServeMux

func WithServeMux(mux *runtime.ServeMux) ServiceOption

Directories

Path Synopsis
examples
proto
v1
Package v1 is a reverse proxy.
Package v1 is a reverse proxy.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL