Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
65 lines (60 loc) · 1.6 KB
/
Copy pathexample_test.go
File metadata and controls
65 lines (60 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package orbitdb_test
import (
"context"
"fmt"
"os"
"time"
orbitdb "github.com/orbitdb/go-orbitdb"
"github.com/orbitdb/go-orbitdb/accesscontrollers"
"github.com/orbitdb/go-orbitdb/ipfs"
)
// Two peers replicate a keyvalue database. The second one only needs the
// address; everything else comes from the first over the network.
func Example() {
ctx := context.Background()
newPeer := func() (*ipfs.Node, *orbitdb.OrbitDB, func()) {
dir, _ := os.MkdirTemp("", "orbitdb-example")
node, err := ipfs.New(ctx, ipfs.Options{})
if err != nil {
panic(err)
}
odb, err := orbitdb.New(ctx, orbitdb.Options{IPFS: node, Directory: dir})
if err != nil {
panic(err)
}
return node, odb, func() {
_ = odb.Stop()
_ = node.Close()
_ = os.RemoveAll(dir)
}
}
nodeA, alice, closeA := newPeer()
defer closeA()
nodeB, bob, closeB := newPeer()
defer closeB()
if err := nodeB.Connect(ctx, nodeA.AddrInfo()); err != nil {
panic(err)
}
// Alice creates a database anyone may write to.
settings, err := alice.OpenKeyValue(ctx, "settings",
orbitdb.WithAccessController(accesscontrollers.IPFS(accesscontrollers.IPFSOptions{Write: []string{"*"}})))
if err != nil {
panic(err)
}
if _, err := settings.Put(ctx, "theme", "dark"); err != nil {
panic(err)
}
// Bob opens it by address and waits for the value to arrive.
replica, err := bob.OpenKeyValue(ctx, settings.Address())
if err != nil {
panic(err)
}
for range 100 {
if v, err := replica.Get(ctx, "theme"); err == nil {
fmt.Println("bob sees theme =", v)
break
}
time.Sleep(100 * time.Millisecond)
}
// Output: bob sees theme = dark
}