@cryptotaxi247 / kubo / commits / e260d2fd0

coreapi: smarter way of dealing with the different APIs

License: MIT Signed-off-by: Lars Gierth <larsg@systemli.org>

Lars Gierth committed Nov 16, 2016 at 06:21 UTC e260d2fd066158dfd7ae8e5ae7e25ac202cdaa90
6 files changed +26 -22
core/coreapi/coreapi.go
+13
@@ -10,6 +10,19 @@ import (
10 ipld "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node"
11 )
12
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 +
26 func resolve(ctx context.Context, n *core.IpfsNode, p string) (ipld.Node, error) {
27 pp, err := path.ParsePath(p)
28 if err != nil {
core/coreapi/interface/interface.go
+4 -5
@@ -9,11 +9,6 @@ import (
9 ipld "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node"
10 )
11
12 -// type CoreAPI interface {
13 -// ID() CoreID
14 -// Version() CoreVersion
15 -// }
16 -
12 type Link ipld.Link
13
14 type Reader interface {
@@ -21,6 +16,10 @@ type Reader interface {
16 io.Closer
17 }
18
19 +type CoreAPI interface {
20 + Unixfs() UnixfsAPI
21 +}
22 +
23 type UnixfsAPI interface {
24 Add(context.Context, io.Reader) (*cid.Cid, error)
25 Cat(context.Context, string) (Reader, error)
core/coreapi/unixfs.go
+1 -9
@@ -4,7 +4,6 @@ import (
4 "context"
5 "io"
6
7 - core "github.com/ipfs/go-ipfs/core"
7 coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
8 coreunix "github.com/ipfs/go-ipfs/core/coreunix"
9 uio "github.com/ipfs/go-ipfs/unixfs/io"
@@ -12,14 +11,7 @@ import (
11 cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid"
12 )
13
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
15
16 func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (*cid.Cid, error) {
17 k, err := coreunix.AddWithContext(ctx, api.node, r)
core/coreapi/unixfs_test.go
+1 -1
@@ -41,7 +41,7 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) {
41 if err != nil {
42 return nil, nil, err
43 }
44 - api := coreapi.NewUnixfsAPI(node)
44 + api := coreapi.NewCoreAPI(node).Unixfs()
45 return node, api, nil
46 }
47
core/corehttp/gateway.go
+1 -1
@@ -28,7 +28,7 @@ func GatewayOption(writable bool, paths ...string) ServeOption {
28 Headers: cfg.Gateway.HTTPHeaders,
29 Writable: writable,
30 PathPrefixes: cfg.Gateway.PathPrefixes,
31 - }, coreapi.NewUnixfsAPI(n))
31 + }, coreapi.NewCoreAPI(n))
32
33 for _, p := range paths {
34 mux.Handle(p+"/", gateway)
core/corehttp/gateway_handler.go
+6 -6
@@ -37,10 +37,10 @@ const (
37 type gatewayHandler struct {
38 node *core.IpfsNode
39 config GatewayConfig
40 - api coreiface.UnixfsAPI
40 + api coreiface.CoreAPI
41 }
42
43 -func newGatewayHandler(n *core.IpfsNode, c GatewayConfig, api coreiface.UnixfsAPI) *gatewayHandler {
43 +func newGatewayHandler(n *core.IpfsNode, c GatewayConfig, api coreiface.CoreAPI) *gatewayHandler {
44 i := &gatewayHandler{
45 node: n,
46 config: c,
@@ -158,7 +158,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
158 ipnsHostname = true
159 }
160
161 - dr, err := i.api.Cat(ctx, urlPath)
161 + dr, err := i.api.Unixfs().Cat(ctx, urlPath)
162 dir := false
163 switch err {
164 case nil:
@@ -218,7 +218,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
218 return
219 }
220
221 - links, err := i.api.Ls(ctx, urlPath)
221 + links, err := i.api.Unixfs().Ls(ctx, urlPath)
222 if err != nil {
223 internalWebError(w, err)
224 return
@@ -247,7 +247,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
247 }
248
249 // return index page instead.
250 - dr, err := i.api.Cat(ctx, p.String())
250 + dr, err := i.api.Unixfs().Cat(ctx, p.String())
251 if err != nil {
252 internalWebError(w, err)
253 return
@@ -314,7 +314,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
314 }
315
316 func (i *gatewayHandler) postHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
317 - k, err := i.api.Add(ctx, r.Body)
317 + k, err := i.api.Unixfs().Add(ctx, r.Body)
318 if err != nil {
319 internalWebError(w, err)
320 return