-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
81 lines (64 loc) · 2.01 KB
/
init.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package server
import (
"context"
"fmt"
"path/filepath"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
"github.com/ipfs/go-ipfs/repo/fsrepo"
config "github.com/ipfs/go-ipfs-config"
libp2p "github.com/ipfs/go-ipfs/core/node/libp2p"
icore "github.com/ipfs/interface-go-ipfs-core"
// This package is needed so that all the preloaded plugins are loaded automatically
"github.com/ipfs/go-ipfs/plugin/loader"
)
func setupPlugins(externalPluginsPath string) error {
// Load any external plugins if available on externalPluginsPath
plugins, err := loader.NewPluginLoader(filepath.Join(externalPluginsPath, "plugins"))
if err != nil {
return fmt.Errorf("error loading plugins: %s", err)
}
// Load preloaded and external plugins
if err := plugins.Initialize(); err != nil {
return fmt.Errorf("error initializing plugins: %s", err)
}
if err := plugins.Inject(); err != nil {
return fmt.Errorf("error initializing plugins: %s", err)
}
return nil
}
// Creates an IPFS node and returns its coreAPI
func createNode(ctx context.Context, repoPath string) (icore.CoreAPI, error) {
// Open the repo
repo, err := fsrepo.Open(repoPath)
if err != nil {
return nil, err
}
// Construct the node
nodeOptions := &core.BuildCfg{
Online: true,
// This option sets the node to be a full DHT node (both fetching and storing DHT Records)
Routing: libp2p.DHTOption,
// This option sets the node to be a client DHT node (only fetching records)
// Routing: libp2p.DHTClientOption,
Repo: repo,
}
node, err := core.NewNode(ctx, nodeOptions)
if err != nil {
return nil, err
}
// Attach the Core API to the constructed node
return coreapi.NewCoreAPI(node)
}
// Spawns a node on the default repo location, if the repo exists
func SpawnDefault(ctx context.Context) (icore.CoreAPI, error) {
defaultPath, err := config.PathRoot()
if err != nil {
// shouldn't be possible
return nil, err
}
if err := setupPlugins(defaultPath); err != nil {
return nil, err
}
return createNode(ctx, defaultPath)
}