Skip to content

Commit e1c190a

Browse files
author
Lars Gierth
committed
coreapi: smarter way of dealing with the different APIs
License: MIT Signed-off-by: Lars Gierth <[email protected]>
1 parent 2aded67 commit e1c190a

File tree

6 files changed

+26
-22
lines changed

6 files changed

+26
-22
lines changed

core/coreapi/coreapi.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ import (
1010
ipld "gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/go-ipld-node"
1111
)
1212

13+
type CoreAPI struct {
14+
node *core.IpfsNode
15+
}
16+
17+
func NewCoreAPI(n *core.IpfsNode) coreiface.CoreAPI {
18+
api := &CoreAPI{n}
19+
return api
20+
}
21+
22+
func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI {
23+
return (*UnixfsAPI)(api)
24+
}
25+
1326
func resolve(ctx context.Context, n *core.IpfsNode, p string) (ipld.Node, error) {
1427
pp, err := path.ParsePath(p)
1528
if err != nil {

core/coreapi/interface/interface.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ import (
99
cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
1010
)
1111

12-
// type CoreAPI interface {
13-
// ID() CoreID
14-
// Version() CoreVersion
15-
// }
16-
1712
type Link ipld.Link
1813

1914
type Reader interface {
2015
io.ReadSeeker
2116
io.Closer
2217
}
2318

19+
type CoreAPI interface {
20+
Unixfs() UnixfsAPI
21+
}
22+
2423
type UnixfsAPI interface {
2524
Add(context.Context, io.Reader) (*cid.Cid, error)
2625
Cat(context.Context, string) (Reader, error)

core/coreapi/unixfs.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,14 @@ import (
44
"context"
55
"io"
66

7-
core "github.com/ipfs/go-ipfs/core"
87
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
98
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
109
uio "github.com/ipfs/go-ipfs/unixfs/io"
1110

1211
cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
1312
)
1413

15-
type UnixfsAPI struct {
16-
node *core.IpfsNode
17-
}
18-
19-
func NewUnixfsAPI(n *core.IpfsNode) coreiface.UnixfsAPI {
20-
api := &UnixfsAPI{n}
21-
return api
22-
}
14+
type UnixfsAPI CoreAPI
2315

2416
func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (*cid.Cid, error) {
2517
k, err := coreunix.AddWithContext(ctx, api.node, r)

core/coreapi/unixfs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) {
4141
if err != nil {
4242
return nil, nil, err
4343
}
44-
api := coreapi.NewUnixfsAPI(node)
44+
api := coreapi.NewCoreAPI(node).Unixfs()
4545
return node, api, nil
4646
}
4747

core/corehttp/gateway.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func GatewayOption(writable bool, paths ...string) ServeOption {
2828
Headers: cfg.Gateway.HTTPHeaders,
2929
Writable: writable,
3030
PathPrefixes: cfg.Gateway.PathPrefixes,
31-
}, coreapi.NewUnixfsAPI(n))
31+
}, coreapi.NewCoreAPI(n))
3232

3333
for _, p := range paths {
3434
mux.Handle(p+"/", gateway)

core/corehttp/gateway_handler.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ const (
3737
type gatewayHandler struct {
3838
node *core.IpfsNode
3939
config GatewayConfig
40-
api coreiface.UnixfsAPI
40+
api coreiface.CoreAPI
4141
}
4242

43-
func newGatewayHandler(n *core.IpfsNode, c GatewayConfig, api coreiface.UnixfsAPI) *gatewayHandler {
43+
func newGatewayHandler(n *core.IpfsNode, c GatewayConfig, api coreiface.CoreAPI) *gatewayHandler {
4444
i := &gatewayHandler{
4545
node: n,
4646
config: c,
@@ -158,7 +158,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
158158
ipnsHostname = true
159159
}
160160

161-
dr, err := i.api.Cat(ctx, urlPath)
161+
dr, err := i.api.Unixfs().Cat(ctx, urlPath)
162162
dir := false
163163
switch err {
164164
case nil:
@@ -231,7 +231,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
231231
return
232232
}
233233

234-
links, err := i.api.Ls(ctx, urlPath)
234+
links, err := i.api.Unixfs().Ls(ctx, urlPath)
235235
if err != nil {
236236
internalWebError(w, err)
237237
return
@@ -260,7 +260,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
260260
}
261261

262262
// return index page instead.
263-
dr, err := i.api.Cat(ctx, p.String())
263+
dr, err := i.api.Unixfs().Cat(ctx, p.String())
264264
if err != nil {
265265
internalWebError(w, err)
266266
return
@@ -327,7 +327,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
327327
}
328328

329329
func (i *gatewayHandler) postHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
330-
k, err := i.api.Add(ctx, r.Body)
330+
k, err := i.api.Unixfs().Add(ctx, r.Body)
331331
if err != nil {
332332
internalWebError(w, err)
333333
return

0 commit comments

Comments
 (0)