-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gateway: use core api for serving GET/HEAD/POST #3244
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
46239e8
coreapi: get going, add Cat() and Ls()
029f971
gateway: use core api for serving GET/HEAD requests
036ca3a
coreapi: add Add()
c31e4f7
gateway: move context/close-notify wiring
0097b42
gateway: use core api for serving POST requests
f610e19
coreapi: reuse go-ipld-node.Link
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package coreapi | ||
|
||
import ( | ||
"context" | ||
|
||
core "github.com/ipfs/go-ipfs/core" | ||
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" | ||
path "github.com/ipfs/go-ipfs/path" | ||
|
||
ipld "gx/ipfs/QmU7bFWQ793qmvNy7outdCaMfSDNk8uqhx4VNrxYj5fj5g/go-ipld-node" | ||
) | ||
|
||
func resolve(ctx context.Context, n *core.IpfsNode, p string) (ipld.Node, error) { | ||
pp, err := path.ParsePath(p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dagnode, err := core.Resolve(ctx, n.Namesys, n.Resolver, pp) | ||
if err == core.ErrNoNamesys { | ||
return nil, coreiface.ErrOffline | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
return dagnode, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package iface | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
|
||
ipld "gx/ipfs/QmU7bFWQ793qmvNy7outdCaMfSDNk8uqhx4VNrxYj5fj5g/go-ipld-node" | ||
cid "gx/ipfs/QmXfiyr2RWEXpVDdaYnD2HNiBk6UBddsvEP4RPfXb6nGqY/go-cid" | ||
) | ||
|
||
// type CoreAPI interface { | ||
// ID() CoreID | ||
// Version() CoreVersion | ||
// } | ||
|
||
type Link ipld.Link | ||
|
||
type Reader interface { | ||
io.ReadSeeker | ||
io.Closer | ||
} | ||
|
||
type UnixfsAPI interface { | ||
Add(context.Context, io.Reader) (*cid.Cid, error) | ||
Cat(context.Context, string) (Reader, error) | ||
Ls(context.Context, string) ([]*Link, error) | ||
} | ||
|
||
// type ObjectAPI interface { | ||
// New() (cid.Cid, Object) | ||
// Get(string) (Object, error) | ||
// Links(string) ([]*Link, error) | ||
// Data(string) (Reader, error) | ||
// Stat(string) (ObjectStat, error) | ||
// Put(Object) (cid.Cid, error) | ||
// SetData(string, Reader) (cid.Cid, error) | ||
// AppendData(string, Data) (cid.Cid, error) | ||
// AddLink(string, string, string) (cid.Cid, error) | ||
// RmLink(string, string) (cid.Cid, error) | ||
// } | ||
|
||
// type ObjectStat struct { | ||
// Cid cid.Cid | ||
// NumLinks int | ||
// BlockSize int | ||
// LinksSize int | ||
// DataSize int | ||
// CumulativeSize int | ||
// } | ||
|
||
var ErrIsDir = errors.New("object is a directory") | ||
var ErrIsNonDag = errors.New("not a merkledag object") | ||
var ErrOffline = errors.New("can't resolve, ipfs node is offline") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package coreapi | ||
|
||
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/QmXfiyr2RWEXpVDdaYnD2HNiBk6UBddsvEP4RPfXb6nGqY/go-cid" | ||
) | ||
|
||
type UnixfsAPI struct { | ||
node *core.IpfsNode | ||
} | ||
|
||
func NewUnixfsAPI(n *core.IpfsNode) coreiface.UnixfsAPI { | ||
api := &UnixfsAPI{n} | ||
return api | ||
} | ||
|
||
func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (*cid.Cid, error) { | ||
k, err := coreunix.AddWithContext(ctx, api.node, r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return cid.Decode(k) | ||
} | ||
|
||
func (api *UnixfsAPI) Cat(ctx context.Context, p string) (coreiface.Reader, error) { | ||
dagnode, err := resolve(ctx, api.node, p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
r, err := uio.NewDagReader(ctx, dagnode, api.node.DAG) | ||
if err == uio.ErrIsDir { | ||
return nil, coreiface.ErrIsDir | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
return r, nil | ||
} | ||
|
||
func (api *UnixfsAPI) Ls(ctx context.Context, p string) ([]*coreiface.Link, error) { | ||
dagnode, err := resolve(ctx, api.node, p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
l := dagnode.Links() | ||
links := make([]*coreiface.Link, len(l)) | ||
for i, l := range l { | ||
links[i] = &coreiface.Link{l.Name, l.Size, l.Cid} | ||
} | ||
return links, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe AddWithContent should return a cid? not sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, but not in this PR. Happy to convert the unixfs package to using CIDs in another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eh I meant coreunix. Maybe we can just rip coreunix out.