From e260d2fd066158dfd7ae8e5ae7e25ac202cdaa90 Mon Sep 17 00:00:00 2001 From: Lars Gierth Date: Wed, 16 Nov 2016 06:21:15 +0100 Subject: [PATCH] coreapi: smarter way of dealing with the different APIs License: MIT Signed-off-by: Lars Gierth --- core/coreapi/coreapi.go | 13 +++++++++++++ core/coreapi/interface/interface.go | 9 ++++----- core/coreapi/unixfs.go | 10 +--------- core/coreapi/unixfs_test.go | 2 +- core/corehttp/gateway.go | 2 +- core/corehttp/gateway_handler.go | 12 ++++++------ 6 files changed, 26 insertions(+), 22 deletions(-) diff --git a/core/coreapi/coreapi.go b/core/coreapi/coreapi.go index f86b0e648..a44af4876 100644 --- a/core/coreapi/coreapi.go +++ b/core/coreapi/coreapi.go @@ -10,6 +10,19 @@ import ( ipld "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node" ) +type CoreAPI struct { + node *core.IpfsNode +} + +func NewCoreAPI(n *core.IpfsNode) coreiface.CoreAPI { + api := &CoreAPI{n} + return api +} + +func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI { + return (*UnixfsAPI)(api) +} + func resolve(ctx context.Context, n *core.IpfsNode, p string) (ipld.Node, error) { pp, err := path.ParsePath(p) if err != nil { diff --git a/core/coreapi/interface/interface.go b/core/coreapi/interface/interface.go index b506e6509..7bf9d5c0a 100644 --- a/core/coreapi/interface/interface.go +++ b/core/coreapi/interface/interface.go @@ -9,11 +9,6 @@ import ( ipld "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node" ) -// type CoreAPI interface { -// ID() CoreID -// Version() CoreVersion -// } - type Link ipld.Link type Reader interface { @@ -21,6 +16,10 @@ type Reader interface { io.Closer } +type CoreAPI interface { + Unixfs() UnixfsAPI +} + type UnixfsAPI interface { Add(context.Context, io.Reader) (*cid.Cid, error) Cat(context.Context, string) (Reader, error) diff --git a/core/coreapi/unixfs.go b/core/coreapi/unixfs.go index db97b2594..0ec8cf851 100644 --- a/core/coreapi/unixfs.go +++ b/core/coreapi/unixfs.go @@ -4,7 +4,6 @@ import ( "context" "io" - core "github.com/ipfs/go-ipfs/core" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" coreunix "github.com/ipfs/go-ipfs/core/coreunix" uio "github.com/ipfs/go-ipfs/unixfs/io" @@ -12,14 +11,7 @@ import ( cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid" ) -type UnixfsAPI struct { - node *core.IpfsNode -} - -func NewUnixfsAPI(n *core.IpfsNode) coreiface.UnixfsAPI { - api := &UnixfsAPI{n} - return api -} +type UnixfsAPI CoreAPI func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (*cid.Cid, error) { k, err := coreunix.AddWithContext(ctx, api.node, r) diff --git a/core/coreapi/unixfs_test.go b/core/coreapi/unixfs_test.go index 612705510..3cda629af 100644 --- a/core/coreapi/unixfs_test.go +++ b/core/coreapi/unixfs_test.go @@ -41,7 +41,7 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) { if err != nil { return nil, nil, err } - api := coreapi.NewUnixfsAPI(node) + api := coreapi.NewCoreAPI(node).Unixfs() return node, api, nil } diff --git a/core/corehttp/gateway.go b/core/corehttp/gateway.go index e212c183b..84b9b2467 100644 --- a/core/corehttp/gateway.go +++ b/core/corehttp/gateway.go @@ -28,7 +28,7 @@ func GatewayOption(writable bool, paths ...string) ServeOption { Headers: cfg.Gateway.HTTPHeaders, Writable: writable, PathPrefixes: cfg.Gateway.PathPrefixes, - }, coreapi.NewUnixfsAPI(n)) + }, coreapi.NewCoreAPI(n)) for _, p := range paths { mux.Handle(p+"/", gateway) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 67dae923b..03d5ca18f 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -37,10 +37,10 @@ const ( type gatewayHandler struct { node *core.IpfsNode config GatewayConfig - api coreiface.UnixfsAPI + api coreiface.CoreAPI } -func newGatewayHandler(n *core.IpfsNode, c GatewayConfig, api coreiface.UnixfsAPI) *gatewayHandler { +func newGatewayHandler(n *core.IpfsNode, c GatewayConfig, api coreiface.CoreAPI) *gatewayHandler { i := &gatewayHandler{ node: n, config: c, @@ -158,7 +158,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr ipnsHostname = true } - dr, err := i.api.Cat(ctx, urlPath) + dr, err := i.api.Unixfs().Cat(ctx, urlPath) dir := false switch err { case nil: @@ -218,7 +218,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr return } - links, err := i.api.Ls(ctx, urlPath) + links, err := i.api.Unixfs().Ls(ctx, urlPath) if err != nil { internalWebError(w, err) return @@ -247,7 +247,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr } // return index page instead. - dr, err := i.api.Cat(ctx, p.String()) + dr, err := i.api.Unixfs().Cat(ctx, p.String()) if err != nil { internalWebError(w, err) return @@ -314,7 +314,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr } func (i *gatewayHandler) postHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { - k, err := i.api.Add(ctx, r.Body) + k, err := i.api.Unixfs().Add(ctx, r.Body) if err != nil { internalWebError(w, err) return