Skip to content

Commit

Permalink
coreapi: smarter way of dealing with the different APIs
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Lars Gierth <[email protected]>
  • Loading branch information
Lars Gierth committed Dec 9, 2016
1 parent 2aded67 commit e1c190a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
13 changes: 13 additions & 0 deletions core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ import (
ipld "gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/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 {
Expand Down
9 changes: 4 additions & 5 deletions core/coreapi/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ import (
cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
)

// type CoreAPI interface {
// ID() CoreID
// Version() CoreVersion
// }

type Link ipld.Link

type Reader interface {
io.ReadSeeker
io.Closer
}

type CoreAPI interface {
Unixfs() UnixfsAPI
}

type UnixfsAPI interface {
Add(context.Context, io.Reader) (*cid.Cid, error)
Cat(context.Context, string) (Reader, error)
Expand Down
10 changes: 1 addition & 9 deletions core/coreapi/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,14 @@ 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"

cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/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)
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/unixfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion core/corehttp/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -231,7 +231,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
Expand Down Expand Up @@ -260,7 +260,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
Expand Down Expand Up @@ -327,7 +327,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
Expand Down

0 comments on commit e1c190a

Please sign in to comment.