wire CoreAPI into request context
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Feb 2, 2018 at 16:01 UTC
76a897cc3d94619f9212d1d5c32453dceb3770b2
1 file changed
+22
commands/request.go
+22
@@ -7,6 +7,8 @@ import (
7
"time"
8
9
"github.com/ipfs/go-ipfs/core"
10
+ coreapi "github.com/ipfs/go-ipfs/core/coreapi"
11
+ coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
12
"github.com/ipfs/go-ipfs/repo/config"
13
14
"gx/ipfs/QmNueRyPRQiV7PUEpnP4GgGLuK1rKQLaRW7sfPvUetYig1/go-ipfs-cmds"
@@ -22,6 +24,7 @@ type Context struct {
24
config *config.Config
25
LoadConfig func(path string) (*config.Config, error)
26
27
+ api coreiface.CoreAPI
28
node *core.IpfsNode
29
ConstructNode func() (*core.IpfsNode, error)
30
}
@@ -52,6 +55,25 @@ func (c *Context) GetNode() (*core.IpfsNode, error) {
55
return c.node, err
56
}
57
58
+// GetApi returns CoreAPI instance backed by ipfs node.
59
+// It may construct the node with the provided function
60
+func (c *Context) GetApi() (coreiface.CoreAPI, error) {
61
+ if c.api == nil {
62
+ n, err := c.GetNode()
63
+ if err != nil {
64
+ return nil, err
65
+ }
66
+ c.api = coreapi.NewCoreAPI(n)
67
+ }
68
+ return c.api, nil
69
+}
70
+
71
+// NodeWithoutConstructing returns the underlying node variable
72
+// so that clients may close it.
73
+func (c *Context) NodeWithoutConstructing() *core.IpfsNode {
74
+ return c.node
75
+}
76
+
77
// Context returns the node's context.
78
func (c *Context) Context() context.Context {
79
n, err := c.GetNode()