Socket
Socket
Sign inDemoInstall

github.com/libp2p/go-libp2p-http

Package Overview
Dependencies
86
Maintainers
0
Alerts
File Explorer

Install Socket

Protect your apps from supply chain attacks

Install

github.com/libp2p/go-libp2p-http

Package p2phttp allows to serve HTTP endpoints and make HTTP requests through LibP2P (https://github.com/libp2p/libp2p) using Go's standard "http" and "net" stacks. Instead of the regular "host:port" addressing, `p2phttp` uses a Peer ID and lets LibP2P take care of the routing, thus taking advantage of features like multi-routes, NAT transversal and stream multiplexing over a single connection. When already running a LibP2P facility, this package allows to expose existing HTTP-based services (like REST APIs) through LibP2P and to use those services with minimal changes to the code-base. For example, a simple http.Server on LibP2P works as: As shown above, a Server only needs a "github.com/libp2p/go-libp2p-gostream" listener. This listener will use a libP2P host to watch for stream tagged with our Protocol. On the other side, a client just needs to be initialized with a custom LibP2P host-based transport to perform requests to such server: In the example above, the client registers a "libp2p" protocol for which the custom transport is used. It can still perform regular "http" requests. The protocol name used is arbitraty and non standard. Note that LibP2P hosts cannot dial to themselves, so there is no possibility of using the same host as server and as client.

    v0.5.0

Version published
Maintainers
0

Readme

# go-libp2p-http

[![Build Status](https://travis-ci.org/libp2p/go-libp2p-http.svg?branch=master)](https://travis-ci.org/libp2p/go-libp2p-http)
[![codecov](https://codecov.io/gh/libp2p/go-libp2p-http/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/go-libp2p-http)
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg)](https://github.com/RichardLitt/standard-readme)


> HTTP on top of libp2p

Package `p2phttp` allows to serve HTTP endpoints and make HTTP requests through [libp2p](https://github.com/libp2p/libp2p) using Go's standard "http" and "net" stack.

Instead of the regular "host:port" addressing, `p2phttp` uses a Peer ID and lets libp2p take care of the routing, thus taking advantage of features like multi-routes,  NAT transversal and stream multiplexing over a single connection.

## Table of Contents

- [Install](#install)
- [Usage](#usage)
- [Contribute](#contribute)
- [License](#license)

## Install

This package is a library that uses Go modules for dependency management.

## Usage

Full documentation can be read at [Godoc](https://godoc.org/github.com/libp2p/go-libp2p-http). The important bits follow.

A simple `http.Server` on libp2p works as:

```go
listener, _ := gostream.Listen(host1, p2phttp.DefaultP2PProtocol)
defer listener.Close()
go func() {
	http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hi!"))
	})
	server := &http.Server{}
	server.Serve(listener)
}
```

The listener is provided by https://github.com/libp2p/go-libp2p-gostream .

A client just needs to be initialized with a custom libp2p host-based transport to perform requests to such server:

```go
tr := &http.Transport{}
tr.RegisterProtocol("libp2p", p2phttp.NewTransport(clientHost))
client := &http.Client{Transport: tr}
res, err := client.Get("libp2p://Qmaoi4isbcTbFfohQyn28EiYM5CDWQx9QRCjDh3CTeiY7P/hello")
```

## Contribute

PRs accepted.

## License

MIT © Protocol Labs, Inc.

FAQs

Last updated on 26 Feb 2023

Did you know?

Socket installs a GitHub app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc