Giter VIP home page Giter VIP logo

go-libp2p-http's Introduction

go-libp2p-http

Build Status codecov standard-readme compliant

HTTP on top of libp2p

Package p2phttp allows to serve HTTP endpoints and make HTTP requests through 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

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

Usage

Full documentation can be read at Godoc. The important bits follow.

A simple http.Server on libp2p works as:

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:

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.

go-libp2p-http's People

Contributors

dependabot-preview[bot] avatar edwargix avatar hsanjuan avatar kubuxu avatar lanzafame avatar raulk avatar stebalien avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.