coreapi.WithOptions
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Dec 10, 2018 at 14:21 UTC
50aea0725728648736f695cec2b1bf06b0c4b13c
2 files changed
+94
-71
core/coreapi/coreapi.go
+88
-71
@@ -17,6 +17,7 @@ import (
17
"context"
18
"errors"
19
"fmt"
20
+
21
"github.com/ipfs/go-ipfs/core"
22
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
23
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
@@ -25,18 +26,18 @@ import (
26
"github.com/ipfs/go-ipfs/repo"
27
28
ci "gx/ipfs/QmNiJiXwWE3kRhZrC5ej3kSjWHm337pYfhjLGSCDNKJP2s/go-libp2p-crypto"
28
- "gx/ipfs/QmP2g3VxmC7g7fyRJDj1VJ72KHZbJ9UW24YjSWEj1XTb4H/go-ipfs-exchange-interface"
29
+ exchange "gx/ipfs/QmP2g3VxmC7g7fyRJDj1VJ72KHZbJ9UW24YjSWEj1XTb4H/go-ipfs-exchange-interface"
30
bserv "gx/ipfs/QmPoh3SrQzFBWtdGK6qmHDV4EanKR6kYPj4DD3J2NLoEmZ/go-blockservice"
30
- "gx/ipfs/QmRASJXJUFygM5qU4YrH7k7jD6S4Hg8nJmgqJ4bYJvLatd/go-libp2p-routing"
31
- "gx/ipfs/QmS2aqUZLJp8kF1ihE5rvDGE5LvmKDPnx32w9Z1BW9xLV5/go-ipfs-blockstore"
32
- "gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer"
31
+ routing "gx/ipfs/QmRASJXJUFygM5qU4YrH7k7jD6S4Hg8nJmgqJ4bYJvLatd/go-libp2p-routing"
32
+ blockstore "gx/ipfs/QmS2aqUZLJp8kF1ihE5rvDGE5LvmKDPnx32w9Z1BW9xLV5/go-ipfs-blockstore"
33
+ peer "gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer"
34
pstore "gx/ipfs/QmZ9zH2FnLcxv1xyzFeUpDUeo55xEhZQHgveZijcxr7TLj/go-libp2p-peerstore"
35
pubsub "gx/ipfs/QmaqGyUhWLsJbVo1QAujSu13mxNjFJ98Kt2VWGSnShGE1Q/go-libp2p-pubsub"
36
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
37
logging "gx/ipfs/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/go-log"
38
dag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
39
offlineroute "gx/ipfs/QmdmWkx54g7VfVyxeG8ic84uf4G6Eq1GohuyKA3XDuJ8oC/go-ipfs-routing/offline"
39
- "gx/ipfs/QmfARXVCzpwFXQdepAJZuqyNDgV9doEsMnVCo1ssmuSe1U/go-libp2p-record"
40
+ record "gx/ipfs/QmfARXVCzpwFXQdepAJZuqyNDgV9doEsMnVCo1ssmuSe1U/go-libp2p-record"
41
p2phost "gx/ipfs/QmfD51tKgJiTMnW9JEiDiPwsCY4mqUoxkhKhBfyW12spTC/go-libp2p-host"
42
)
43
@@ -45,7 +46,7 @@ var log = logging.Logger("core/coreapi")
46
type CoreAPI struct {
47
nctx context.Context
48
48
- identity peer.ID //TODO: check mutable structs
49
+ identity peer.ID
50
privateKey ci.PrivKey
51
52
repo repo.Repo
@@ -69,16 +70,80 @@ type CoreAPI struct {
70
// TODO: this can be generalized to all functions when we implement some
71
// api based security mechanism
72
isPublishAllowed func() error
73
+
74
+ // ONLY for re-applying options in WithOptions, DO NOT USE ANYWHERE ELSE
75
+ nd *core.IpfsNode
76
}
77
78
// NewCoreAPI creates new instance of IPFS CoreAPI backed by go-ipfs Node.
79
func NewCoreAPI(n *core.IpfsNode, opts ...options.ApiOption) (coreiface.CoreAPI, error) {
80
+ return (&CoreAPI{nd: n}).WithOptions(opts...)
81
+}
82
+
83
+// Unixfs returns the UnixfsAPI interface implementation backed by the go-ipfs node
84
+func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI {
85
+ return (*UnixfsAPI)(api)
86
+}
87
+
88
+// Block returns the BlockAPI interface implementation backed by the go-ipfs node
89
+func (api *CoreAPI) Block() coreiface.BlockAPI {
90
+ return (*BlockAPI)(api)
91
+}
92
+
93
+// Dag returns the DagAPI interface implementation backed by the go-ipfs node
94
+func (api *CoreAPI) Dag() coreiface.DagAPI {
95
+ return (*DagAPI)(api)
96
+}
97
+
98
+// Name returns the NameAPI interface implementation backed by the go-ipfs node
99
+func (api *CoreAPI) Name() coreiface.NameAPI {
100
+ return (*NameAPI)(api)
101
+}
102
+
103
+// Key returns the KeyAPI interface implementation backed by the go-ipfs node
104
+func (api *CoreAPI) Key() coreiface.KeyAPI {
105
+ return (*KeyAPI)(api)
106
+}
107
+
108
+// Object returns the ObjectAPI interface implementation backed by the go-ipfs node
109
+func (api *CoreAPI) Object() coreiface.ObjectAPI {
110
+ return (*ObjectAPI)(api)
111
+}
112
+
113
+// Pin returns the PinAPI interface implementation backed by the go-ipfs node
114
+func (api *CoreAPI) Pin() coreiface.PinAPI {
115
+ return (*PinAPI)(api)
116
+}
117
+
118
+// Dht returns the DhtAPI interface implementation backed by the go-ipfs node
119
+func (api *CoreAPI) Dht() coreiface.DhtAPI {
120
+ return (*DhtAPI)(api)
121
+}
122
+
123
+// Swarm returns the SwarmAPI interface implementation backed by the go-ipfs node
124
+func (api *CoreAPI) Swarm() coreiface.SwarmAPI {
125
+ return (*SwarmAPI)(api)
126
+}
127
+
128
+// PubSub returns the PubSubAPI interface implementation backed by the go-ipfs node
129
+func (api *CoreAPI) PubSub() coreiface.PubSubAPI {
130
+ return (*PubSubAPI)(api)
131
+}
132
+
133
+// WithOptions returns api with global options applied
134
+func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, error) {
135
settings, err := options.ApiOptions(opts...)
136
if err != nil {
137
return nil, err
138
}
139
81
- api := &CoreAPI{
140
+ if api.nd == nil {
141
+ return nil, errors.New("cannot apply options to api without node")
142
+ }
143
+
144
+ n := api.nd
145
+
146
+ subApi := &CoreAPI{
147
nctx: n.Context(),
148
149
identity: n.Identity,
@@ -99,9 +164,11 @@ func NewCoreAPI(n *core.IpfsNode, opts ...options.ApiOption) (coreiface.CoreAPI,
164
exchange: n.Exchange,
165
166
pubSub: n.PubSub,
167
+
168
+ nd: n,
169
}
170
104
- api.routing = func(allowOffline bool) (routing.IpfsRouting, error) {
171
+ subApi.routing = func(allowOffline bool) (routing.IpfsRouting, error) {
172
if !n.OnlineMode() {
173
if !allowOffline {
174
return nil, coreiface.ErrOffline
@@ -109,8 +176,8 @@ func NewCoreAPI(n *core.IpfsNode, opts ...options.ApiOption) (coreiface.CoreAPI,
176
if err := n.SetupOfflineRouting(); err != nil {
177
return nil, err
178
}
112
- api.privateKey = n.PrivateKey
113
- api.namesys = n.Namesys
179
+ subApi.privateKey = n.PrivateKey
180
+ subApi.namesys = n.Namesys
181
return n.Routing, nil
182
}
183
if !settings.Offline {
@@ -120,7 +187,6 @@ func NewCoreAPI(n *core.IpfsNode, opts ...options.ApiOption) (coreiface.CoreAPI,
187
return nil, coreiface.ErrOffline
188
}
189
123
- //todo: might want to cache this
190
cfg, err := n.Repo.Config()
191
if err != nil {
192
return nil, err
@@ -134,13 +200,13 @@ func NewCoreAPI(n *core.IpfsNode, opts ...options.ApiOption) (coreiface.CoreAPI,
200
return nil, fmt.Errorf("cannot specify negative resolve cache size")
201
}
202
137
- offroute := offlineroute.NewOfflineRouter(api.repo.Datastore(), api.recordValidator)
138
- api.namesys = namesys.NewNameSystem(offroute, api.repo.Datastore(), cs)
203
+ offroute := offlineroute.NewOfflineRouter(subApi.repo.Datastore(), subApi.recordValidator)
204
+ subApi.namesys = namesys.NewNameSystem(offroute, subApi.repo.Datastore(), cs)
205
206
return offroute, nil
207
}
208
143
- api.isPublishAllowed = func() error {
209
+ subApi.isPublishAllowed = func() error {
210
if n.Mounts.Ipns != nil && n.Mounts.Ipns.IsActive() {
211
return errors.New("cannot manually publish while IPNS is mounted")
212
}
@@ -148,71 +214,22 @@ func NewCoreAPI(n *core.IpfsNode, opts ...options.ApiOption) (coreiface.CoreAPI,
214
}
215
216
if settings.Offline {
151
- api.peerstore = nil
152
- api.peerHost = nil
153
- api.namesys = nil
154
- api.recordValidator = nil
155
- api.exchange = nil
217
+ subApi.peerstore = nil
218
+ subApi.peerHost = nil
219
+ subApi.namesys = nil
220
+ subApi.recordValidator = nil
221
+ subApi.exchange = nil
222
}
223
158
- return api, nil
159
-}
160
-
161
-// Unixfs returns the UnixfsAPI interface implementation backed by the go-ipfs node
162
-func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI {
163
- return (*UnixfsAPI)(api)
164
-}
165
-
166
-// Block returns the BlockAPI interface implementation backed by the go-ipfs node
167
-func (api *CoreAPI) Block() coreiface.BlockAPI {
168
- return (*BlockAPI)(api)
169
-}
170
-
171
-// Dag returns the DagAPI interface implementation backed by the go-ipfs node
172
-func (api *CoreAPI) Dag() coreiface.DagAPI {
173
- return (*DagAPI)(api)
174
-}
175
-
176
-// Name returns the NameAPI interface implementation backed by the go-ipfs node
177
-func (api *CoreAPI) Name() coreiface.NameAPI {
178
- return (*NameAPI)(api)
179
-}
180
-
181
-// Key returns the KeyAPI interface implementation backed by the go-ipfs node
182
-func (api *CoreAPI) Key() coreiface.KeyAPI {
183
- return (*KeyAPI)(api)
184
-}
185
-
186
-// Object returns the ObjectAPI interface implementation backed by the go-ipfs node
187
-func (api *CoreAPI) Object() coreiface.ObjectAPI {
188
- return (*ObjectAPI)(api)
189
-}
190
-
191
-// Pin returns the PinAPI interface implementation backed by the go-ipfs node
192
-func (api *CoreAPI) Pin() coreiface.PinAPI {
193
- return (*PinAPI)(api)
194
-}
195
-
196
-// Dht returns the DhtAPI interface implementation backed by the go-ipfs node
197
-func (api *CoreAPI) Dht() coreiface.DhtAPI {
198
- return (*DhtAPI)(api)
199
-}
200
-
201
-// Swarm returns the SwarmAPI interface implementation backed by the go-ipfs node
202
-func (api *CoreAPI) Swarm() coreiface.SwarmAPI {
203
- return (*SwarmAPI)(api)
204
-}
205
-
206
-// PubSub returns the PubSubAPI interface implementation backed by the go-ipfs node
207
-func (api *CoreAPI) PubSub() coreiface.PubSubAPI {
208
- return (*PubSubAPI)(api)
224
+ return subApi, nil
225
}
226
227
// getSession returns new api backed by the same node with a read-only session DAG
228
func (api *CoreAPI) getSession(ctx context.Context) *CoreAPI {
229
sesApi := *api
230
215
- //TODO: we may want to apply this to other things too
231
+ // TODO: We could also apply this to api.blocks, and compose into writable api,
232
+ // but this requires some changes in blockservice/merkledag
233
sesApi.dag = dag.NewReadOnlyDagService(dag.NewSession(ctx, api.dag))
234
235
return &sesApi
core/coreapi/interface/coreapi.go
+6
@@ -5,6 +5,8 @@ package iface
5
import (
6
"context"
7
8
+ "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9
+
10
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
11
)
12
@@ -46,4 +48,8 @@ type CoreAPI interface {
48
// ResolveNode resolves the path (if not resolved already) using Unixfs
49
// resolver, gets and returns the resolved Node
50
ResolveNode(context.Context, Path) (ipld.Node, error)
51
+
52
+ // WithOptions creates new instance of CoreAPI based on this instance with
53
+ // a set of options applied
54
+ WithOptions(...options.ApiOption) (CoreAPI, error)
55
}