Skip to content
This repository has been archived by the owner on Jul 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #30 from eelcocramer/master
Browse files Browse the repository at this point in the history
Changes examples to use new node builder interface.
  • Loading branch information
jbenet committed Oct 20, 2015
2 parents 372ec08 + 83c7569 commit 8fa3aa3
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 81 deletions.
16 changes: 10 additions & 6 deletions examples/api/demoapp/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"io"
"os"

"code.google.com/p/go.net/context"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreunix"
"github.com/ipfs/go-ipfs/repo/fsrepo"
"golang.org/x/net/context"
)

func CountChars(r io.Reader) map[byte]int {
Expand All @@ -26,13 +26,17 @@ func CountChars(r io.Reader) map[byte]int {

func SetupIpfs() (*core.IpfsNode, error) {
// Assume the user has run 'ipfs init'
r := fsrepo.At("~/.ipfs")
if err := r.Open(); err != nil {
r, err := fsrepo.Open("~/.ipfs")
if err != nil {
return nil, err
}

builder := core.NewNodeBuilder().Online().SetRepo(r)
return builder.Build(context.Background())
cfg := &core.BuildCfg{
Repo: r,
Online: true,
}

return core.NewNode(context.Background(), cfg)
}

func main() {
Expand All @@ -48,7 +52,7 @@ func main() {
}
keytofetch := os.Args[1]

read, err := coreunix.Cat(nd, keytofetch)
read, err := coreunix.Cat(context.Background(), nd, keytofetch)
if err != nil {
fmt.Println(err)
return
Expand Down
18 changes: 13 additions & 5 deletions examples/api/demoapp/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
"io"
"os"
"code.google.com/p/go.net/context"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreunix"
"github.com/ipfs/go-ipfs/repo/fsrepo"
"code.google.com/p/go.net/context"
)
```

Expand All @@ -44,13 +44,21 @@ Alright, now for the ipfs goodness:
```
func SetupIpfs() (*core.IpfsNode, error) {
// Assume the user has run 'ipfs init'
r := fsrepo.At("~/.ipfs")
if err := r.Open(); err != nil {
r, err := fsrepo.Open("~/.ipfs")
if err != nil {
return nil, err
}
builder := core.NewNodeBuilder().Online().SetRepo(r)
return builder.Build(context.Background())
cfg := &core.BuildCfg{
Repo: r,
Online: true,
}
cfg := new(core.BuildCfg)
cfg.Repo = r
cfg.Online = true
return core.NewNode(context.Background(), cfg)
}
```

Expand Down
27 changes: 15 additions & 12 deletions examples/api/randobj/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,33 @@ func ServeIpfsRand(w http.ResponseWriter, r *http.Request) {

And now, lets tie it all together in a main function.

```
func main() {
builder := core.NewNodeBuilder().Online()
```

Set up our builder, and use the users standard ipfs configuration directory.
Set up our node configuration, and use the users standard ipfs configuration directory.

```
r := fsrepo.At("~/.ipfs")
if err := r.Open(); err != nil {
func main() {
r, err := fsrepo.Open("~/.ipfs")
if err != nil {
panic(err)
}
builder.SetRepo(r)
```

Now we need to set up our context and finally build our node!
Now we need to set up our context

```
// Make our 'master' context and defer cancelling it
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
```

Then create a configuration and finally create our node!

```
cfg := &core.BuildCfg{
Repo: r,
Online: true,
}
node, err := builder.Build(ctx)
node, err := core.NewNode(ctx, cfg)
if err != nil {
panic(err)
}
Expand Down
71 changes: 29 additions & 42 deletions examples/api/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,30 @@ At its simplest, you only need to create a node:
import "github.com/ipfs/go-ipfs/core"
.
.
// setup ctx
// setup cfg
.
builder := core.NewNodeBuilder()
node, err := builder.Build(ctx)
```

The above code snippet is the simplest way to create an ipfs node. There are
a couple different things that I think deserve explanation.

#### NodeBuilder
The NodeBuilder is an object following the 'builder' pattern (who would have
guessed?). It can be used to configure the node before its actually constructed.
It has a few different setters and other options that we will discuss in a bit.
#### Contexts
If youve never dealt with contexts before, I highly recommend you first go read
[this wonderful explanation](https://blog.golang.org/context). Now, the context
we pass into `Build` is the "master" context to the entire ipfs node, cancelling
that context will shut down every single subprocess that ipfs runs.

The easiest way to set up a context for an ipfs node is something like this:
node, err := core.NewNode(ctx, cfg)
```
ctx, cancel := context.WithCancel(context.Background)
```
This creates a context, and an anonymous function that can be called to cancel
the context, and by extension, all of the ipfs node.

### Ipfs Options
So, now that all of that is out of the way, lets look at different configuration
options.
The above code snippet is the simplest way to create an ipfs node. Below is explained how to get the context and the configuration objects in place.

### Configuration
Lets create a Node's build configuration:

#### Online/Offline
The default state for a nodebuilder is 'Offline', so set it to 'Online' simply
call the `Online()` method.
```
builder := core.NewNodeBuilder().Online()
```
Or:
```
builder := core.NewNodeBuilder()
builder.Online()
cfg := &core.BuildCfg{
Repo: r,
Online: true,
Routing: myRoutingOption,
}
```

A node created in 'Online' mode will start up bootstrapping, bitswap exchange,
and other network interfaces.

#### SetRepo
#### Repo
The ipfs 'repo' or repository represents all data that persists past a single
instance. This currently includes the configuration file and the local
datastore. By default, you will be given a blank config and an in memory
Expand All @@ -63,21 +41,30 @@ import "github.com/ipfs/go-ipfs/repo/fsrepo"
.
.
.
r := fsrepo.At("/path/to/.ipfs")
if err := r.Open(); err != nil {
r := fsrepo.Open("/path/to/.ipfs")
if err != nil {
// Deal with the error
}
builder.SetRepo(r)
```

#### SetRouting
ipfs by default will use our DHT network for getting provider information and
ipns entries. If you wish to implement a separate routing system for your node
to get this information through, just make an object that implements the
IpfsRouting interface and pass the builder a RoutingOption for it.
IpfsRouting interface and pass the build configuration a RoutingOption for it.

### Context
If you have never dealt with contexts before, I highly recommend you first go read
[this wonderful explanation](https://blog.golang.org/context). Now, the context
we pass into the new `Node` we are creating is the "master" context to the entire
ipfs node, cancelling that context will shut down every single subprocess that ipfs
runs.

The easiest way to set up a context for an ipfs node is something like this:
```
builder := core.NewNodeBuilder().Online()
builder.SetRouting(myRoutingOption)
ctx, cancel := context.WithCancel(context.Background)
```
This creates a context, and an anonymous function that can be called to cancel
the context, and by extension, all of the ipfs node.


11 changes: 7 additions & 4 deletions examples/api/service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ func main() {
panic(err)
}

nb := core.NewNodeBuilder().Online()
nb.SetRepo(r)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

nd, err := nb.Build(ctx)
cfg := &core.BuildCfg{
Repo: r,
Online: true,
}

nd, err := core.NewNode(ctx, cfg)

if err != nil {
panic(err)
}
Expand Down
11 changes: 7 additions & 4 deletions examples/api/service/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ func main() {
panic(err)
}

nb := core.NewNodeBuilder().Online()
nb.SetRepo(r)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

nd, err := nb.Build(ctx)
cfg := &core.BuildCfg{
Repo: r,
Online: true,
}

nd, err := core.NewNode(ctx, cfg)

if err != nil {
panic(err)
}
Expand Down
24 changes: 16 additions & 8 deletions examples/api/service/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ func main() {
panic(err)
}
nb := core.NewNodeBuilder().Online()
nb.SetRepo(r)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
nd, err := nb.Build(ctx)
cfg := &core.BuildCfg{
Repo: r,
Online: true,
}
nd, err := core.NewNode(ctx, cfg)
if err != nil {
panic(err)
}
Expand All @@ -55,6 +58,7 @@ Next, we are going to build our service.
if err != nil {
panic(err)
}
fmt.Printf("I am peer: %s\n", nd.Identity.Pretty())
for {
Expand All @@ -63,6 +67,7 @@ Next, we are going to build our service.
fmt.Println(err)
return
}
defer con.Close()
fmt.Fprintln(con, "Hello! This is whyrusleepings awesome ipfs service")
Expand Down Expand Up @@ -110,13 +115,16 @@ func main() {
panic(err)
}
nb := core.NewNodeBuilder().Online()
nb.SetRepo(r)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
nd, err := nb.Build(ctx)
cfg := &core.BuildCfg{
Repo: r,
Online: true,
}
nd, err := core.NewNode(ctx, cfg)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 8fa3aa3

Please sign in to comment.