Commit 2c7c62ee authored by David Dias's avatar David Dias
Browse files

docs(examples): give a pass through remaining examples

parent a8926ed7
......@@ -2,7 +2,7 @@ gx:
go get github.com/whyrusleeping/gx
go get github.com/whyrusleeping/gx-go
deps-examples: deps
deps-protocol-muxing: deps
go get -u github.com/multiformats/go-multicodec
go get -u github.com/jbenet/go-msgio
......@@ -12,4 +12,3 @@ deps: gx
publish:
gx-go rewrite --undo
......@@ -2,24 +2,21 @@
This examples shows how to create a simple HTTP proxy service with libp2p:
```
XXX
XX XXXXXX
X XX
XXXXXXX XX XX XXXXXXXXXX
+---------------------+ +---------------------+ XXX XXX XXX XXX
HTTP Request | | | | XX XX
+-------------------> | libp2p stream | | HTTP X X
| Local peer <------------------> Remote peer <-------------> HTTP SERVER - THE INTERNET XX
<-------------------+ | | | Req & Resp XX X
HTTP Response | libp2p host | | libp2p host | XXXX XXXX XXXXXXXXXXXXXXXXXXXX XXXX
+---------------------+ +---------------------+ XXXXX
XXX
XX XXXXXX
X XX
XXXXXXX XX XX XXXXXXXXXX
+----------------+ +-----------------+ XXX XXX XXX XXX
HTTP Request | | | | XX XX
+-----------------> | libp2p stream | | HTTP X X
| Local peer <----------------> Remote peer <-------------> HTTP SERVER - THE INTERNET XX
<-----------------+ | | | Req & Resp XX X
HTTP Response | libp2p host | | libp2p host | XXXX XXXX XXXXXXXXXXXXXXXXXXXX XXXX
+----------------+ +-----------------+ XXXXX
```
In order to proxy an HTTP request, we create a local peer which listens on `localhost:9900`. HTTP requests performed to that address are tunneled via a libp2p stream to a remote peer, which then performs the HTTP requests and sends the response back to the local peer, which relays it
to the user.
In order to proxy an HTTP request, we create a local peer which listens on `localhost:9900`. HTTP requests performed to that address are tunneled via a libp2p stream to a remote peer, which then performs the HTTP requests and sends the response back to the local peer, which relays it to the user.
Note that this is a very simple approach to a proxy, and does not perform any header management, nor supports HTTPS. The `proxy.go` code is thoroughly commeted, detailing what is happening in every step.
......@@ -37,7 +34,7 @@ From `go-libp2p` base folder:
First run the "remote" peer as follows. It will print a local peer address. If you would like to run this on a separate machine, please replace the IP accordingly:
```sh
$ ./http-proxy
> ./http-proxy
Proxy server is ready
libp2p-peer addresses:
/ip4/127.0.0.1/tcp/12000/ipfs/QmddTrQXhA9AkCpXPTkcY7e22NK73TwkUms3a44DhTKJTD
......@@ -46,7 +43,7 @@ libp2p-peer addresses:
The run the local peer, indicating that it will need to forward http requests to the remote peer as follows:
```
$ ./http-proxy -d /ip4/127.0.0.1/tcp/12000/ipfs/QmddTrQXhA9AkCpXPTkcY7e22NK73TwkUms3a44DhTKJTD
> ./http-proxy -d /ip4/127.0.0.1/tcp/12000/ipfs/QmddTrQXhA9AkCpXPTkcY7e22NK73TwkUms3a44DhTKJTD
Proxy server is ready
libp2p-peer addresses:
/ip4/127.0.0.1/tcp/12001/ipfs/Qmaa2AYTha1UqcFVX97p9R1UP7vbzDLY7bqWsZw1135QvN
......@@ -56,7 +53,6 @@ proxy listening on 127.0.0.1:9900
As you can see, the proxy prints the listening address `127.0.0.1:9900`. You can now use this address as proxy, for example with `curl`:
```
$ curl -x "127.0.0.1:9900" "http://ipfs.io/ipfs/QmfUX75pGRBRDnjeoMkQzuQczuCup2aYbeLxz5NzeSu9G6"
> curl -x "127.0.0.1:9900" "http://ipfs.io/ipfs/QmfUX75pGRBRDnjeoMkQzuQczuCup2aYbeLxz5NzeSu9G6"
it works!
```
# The libp2p 'host'
For most applications, the host is the basic building block you'll need to get
started. This guide will show how to construct and use a simple host.
The host is an abstraction that manages services on top of a swarm. It provides
a clean interface to connect to a service on a given remote peer.
For most applications, the host is the basic building block you'll need to get started. This guide will show how to construct and use a simple host.
The host is an abstraction that manages services on top of a swarm. It provides a clean interface to connect to a service on a given remote peer.
First, you'll need an ID, and a place to store that ID. To generate an ID, you can do the following:
First, you'll need an ID, and a place to store that ID. To generate an
ID, you can do the following:
```go
import (
"crypto/rand"
......@@ -35,8 +34,8 @@ ps.AddPrivKey(pid, priv)
ps.AddPubKey(pid, priv)
```
Next, you'll need at least one address that you want to listen on. You can go
from a string to a multiaddr like this:
Next, you'll need at least one address that you want to listen on. You can go from a string to a multiaddr like this:
```go
import ma "github.com/multiformats/go-multiaddr"
......@@ -48,10 +47,8 @@ if err != nil {
}
```
Now you know who you are, and where you live (in a manner of speaking). The
next step is setting up a 'swarm network' to handle all the peers you will
connect to. The swarm handles incoming connections from other peers, and
handles the negotiation of new outbound connections.
Now you know who you are, and where you live (in a manner of speaking). The next step is setting up a 'swarm network' to handle all the peers you will connect to. The swarm handles incoming connections from other peers, and handles the negotiation of new outbound connections.
```go
import (
"context"
......@@ -68,20 +65,16 @@ if err != nil {
}
```
At this point, we have everything needed to finally construct a host. That call
is the simplest one so far:
At this point, we have everything needed to finally construct a host. That call is the simplest one so far:
```go
import bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
myhost := bhost.New(netw)
```
And thats it, you have a libp2p host and you're ready to start doing some
awesome p2p networking!
And thats it, you have a libp2p host and you're ready to start doing some awesome p2p networking!
In future guides we will go over ways to use hosts, configure them differently
(hint: there are a huge number of ways to set these up), and interesting ways
to apply this technology to various applications you might want to build.
In future guides we will go over ways to use hosts, configure them differently (hint: there are a huge number of ways to set these up), and interesting ways to apply this technology to various applications you might want to build.
To see this code all put together, take a look at the `host.go` file in this
directory.
To see this code all put together, take a look at the `host.go` file in this directory.
# Using multicodecs with LibP2P
# Protocol Multiplexing using multicodecs with libp2p
This examples shows how to use multicodecs (i.e. json) to encode and transmit information between LibP2P hosts using LibP2P Streams.
......@@ -11,8 +11,8 @@ This example expects that you area already familiar with the [echo example](http
From `go-libp2p` base folder:
```
> make deps-examples
> go build ./examples/multicodecs
> make deps-protocol-muxing
> go build ./examples/protocol-multiplexing-with-multicodecs
```
## Usage
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment