kubo/docs/examples/dep-injection-multiple-nodes
2020-08-28 09:49:05 +02:00
..
go.mod Added dep-injection-multiple-nodes example 2020-08-18 11:57:28 +02:00
go.sum Added dep-injection-multiple-nodes example 2020-08-18 11:57:28 +02:00
main.go Minor fix Unixfs().Get() 2020-08-28 09:49:05 +02:00
README.md Minor fix Unixfs().Get() 2020-08-28 09:49:05 +02:00

Manual dependency injection and spawning multiple nodes

By the end of this tutorial, you will learn how to:

  • Manually inject dependencies with your desired configuration to a node.
  • Spawn two different IPFS nodes with the same configuration in different ports.
  • Generate a random file and add it from one node.
  • Retrieve the added file from the other node.

All of this using only golang!

In order to complete this tutorial, you will need:

Disclaimer: This example was an attempt to understand how to manually inject dependencies to spawn an IPFS node with custom configurations without having to rely on repo.Repo and BuildCfg. For this specific example the focus was on being able to initialize our own exchange.Interface and set IPFS nodes on different ports. Expect in the future further improvements to this example in order to clean the code and include additional custom configurations. The example is inspired by the following function

Getting started

Note: Make sure you have installed.

Download go-ipfs and jump into the example folder:

> go get -u github.com/ipfs/go-ipfs
cd $GOPATH/src/github.com/ipfs/go-ipfs/docs/examples/dep-injection-multiple-nodes

Running the example as-is

To run the example, simply do:

> go run main.go

You should see the following as output:

[*] Spawned first node listening at:  [/ip4/192.168.0.56/tcp/36911 /ip4/127.0.0.1/tcp/36911 /ip6/::1/tcp/39831 /ip4/192.168.0.56/udp/60407/quic /ip4/127.0.0.1/udp/60407/quic /ip6/::1/udp/41561/quic /ip4/2.137.154.240/udp/60407/quic /ip4/2.137.154.240/tcp/36911]
[*] Spawned first node listening at:  [/ip6/::1/udp/41482/quic /ip4/192.168.0.56/tcp/42321 /ip4/127.0.0.1/tcp/42321 /ip6/::1/tcp/34749 /ip4/192.168.0.56/udp/37708/quic /ip4/127.0.0.1/udp/37708/quic]
[*] Connected fron node1 to node2
[*] Added a test file to the network: /ipfs/QmXG1dKK7B4srPsiiCi6xZ4HkvpShKoDxuBr7BoNEGic2M
[*] Searching for /ipfs/QmXG1dKK7B4srPsiiCi6xZ4HkvpShKoDxuBr7BoNEGic2M from node 2
[*] Retrieved file with size:  1324643

Understanding the example

The example comprises the following parts: