-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathipfs-lite.go
61 lines (50 loc) · 1.07 KB
/
ipfs-lite.go
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
package main
import (
"context"
"fmt"
"io/ioutil"
ipfslite "github.com/hsanjuan/ipfs-lite"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-log"
crypto "github.com/libp2p/go-libp2p-crypto"
"github.com/multiformats/go-multiaddr"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log.SetLogLevel("*", "warn")
ds, err := ipfslite.IPFSBadgerDatastore()
if err != nil {
panic(err)
}
priv, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
if err != nil {
panic(err)
}
listen, _ := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/4005")
h, dht, err := ipfslite.SetupLibp2p(
ctx,
priv,
nil,
[]multiaddr.Multiaddr{listen},
ipfslite.DefaultBootstrapPeers(),
)
if err != nil {
panic(err)
}
lite, err := ipfslite.New(ctx, ds, h, dht, nil)
if err != nil {
panic(err)
}
c, _ := cid.Decode("QmQzd3DhkmdfJtC65JLp9bt1bSHh5GXZvrFHPanZdRTPX3")
rsc, err := lite.GetFile(ctx, c)
if err != nil {
panic(err)
}
defer rsc.Close()
content, err := ioutil.ReadAll(rsc)
if err != nil {
panic(err)
}
fmt.Println(string(content))
}