Skip to content
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 uses minimal IPFS Core API #2876

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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"
dag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"
uio "github.com/ipfs/go-ipfs/unixfs/io"
cid "gx/ipfs/QmfSc2xehWmWLnwwYR91Y8QF4xdASypTFVknutoKQS3GHp/go-cid"
)

type UnixfsAPI struct {
Context context.Context
Node *core.IpfsNode
}

func (api *UnixfsAPI) resolve(p string) (*dag.Node, error) {
pp, err := path.ParsePath(p)
if err != nil {
return nil, err
}

dagnode, err := core.Resolve(api.Context, api.Node, pp)
if err == core.ErrNoNamesys {
return nil, coreiface.ErrOffline
} else if err != nil {
return nil, err
}
return dagnode, nil
}

func (api *UnixfsAPI) Add(r io.Reader) (*cid.Cid, error) {
k, err := coreunix.Add(api.Node, r)
if err != nil {
return nil, err
}
return cid.Decode(k)
}

func (api *UnixfsAPI) Cat(p string) (coreiface.Reader, error) {
dagnode, err := api.resolve(p)
if err != nil {
return nil, err
}

r, err := uio.NewDagReader(api.Context, 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(p string) ([]*coreiface.Link, error) {
dagnode, err := api.resolve(p)
if err != nil {
return nil, err
}

links := make([]*coreiface.Link, len(dagnode.Links))
for i, l := range dagnode.Links {
links[i] = &coreiface.Link{l.Name, l.Size, cid.NewCidV0(l.Hash)}
}
return links, nil
}
127 changes: 127 additions & 0 deletions core/coreapi/coreapi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package coreapi_test

import (
"bytes"
"context"
"io"
"strings"
"testing"

core "github.com/ipfs/go-ipfs/core"
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
// coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
repo "github.com/ipfs/go-ipfs/repo"
config "github.com/ipfs/go-ipfs/repo/config"
testutil "github.com/ipfs/go-ipfs/thirdparty/testutil"
)

// `ipfs object new unixfs-dir`
var emptyUnixfsDir = "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"

// echo -n | ipfs add
// curl -X POST localhost:8080/ipfs/
var emptyUnixfsFile = "QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"

func makeAPI(ctx context.Context) (*core.IpfsNode, *coreapi.UnixfsAPI, error) {
r := &repo.Mock{
C: config.Config{
Identity: config.Identity{
PeerID: "Qmfoo", // required by offline node
},
},
D: testutil.ThreadSafeCloserMapDatastore(),
}
node, err := core.NewNode(ctx, &core.BuildCfg{Repo: r})
if err != nil {
return nil, nil, err
}
api := &coreapi.UnixfsAPI{Node: node, Context: ctx}
return node, api, nil
}

func testAddBasic(t *testing.T) {
t.Skip("TODO: implement me")
}

func TestAddEmpty(t *testing.T) {
t.Skip("TODO: implement me")
}

func TestCatBasic(t *testing.T) {
node, api, err := makeAPI(context.Background())
if err != nil {
t.Fatal(err)
}

hello := "hello, world!"
hr := strings.NewReader(hello)
k, err := coreunix.Add(node, hr)
if err != nil {
t.Fatal(err)
}

r, err := api.Cat(k)
if err != nil {
t.Fatal(err)
}

buf := make([]byte, len(hello))
n, err := io.ReadFull(r, buf)
if err != nil && err != io.EOF {
t.Error(err)
}
if string(buf) != hello {
t.Fatalf("expected [hello, world!], got [%s] [err=%s]", string(buf), n, err)
}
}

func TestCatEmptyFile(t *testing.T) {
node, api, err := makeAPI(context.Background())
if err != nil {
t.Fatal(err)
}

_, err = coreunix.Add(node, strings.NewReader(""))
if err != nil {
t.Fatal(err)
}

r, err := api.Cat(emptyUnixfsFile)
if err != nil {
t.Fatal(err)
}

buf := make([]byte, 1) // non-zero so that Read() actually tries to read
n, err := io.ReadFull(r, buf)
if err != nil && err != io.EOF {
t.Error(err)
}
if !bytes.HasPrefix(buf, []byte{0x00}) {
t.Fatalf("expected empty data, got [%s] [read=%d]", buf, n)
}
}

func TestCatDir(t *testing.T) {
t.Skip("TODO: implement me")
}

func TestCatNonUnixfs(t *testing.T) {
t.Skip("TODO: implement me")
}

func TestCatOffline(t *testing.T) {
t.Skip("TODO: implement me")
}

func TestLs(t *testing.T) {
t.Skip("TODO: implement me")
}

func TestLsEmpty(t *testing.T) {
t.Skip("TODO: implement me")
}

func TestLsNonUnixfs(t *testing.T) {
t.Skip("TODO: implement me")
}
55 changes: 55 additions & 0 deletions core/coreapi/interface/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package iface

import (
"errors"
"io"

cid "gx/ipfs/QmfSc2xehWmWLnwwYR91Y8QF4xdASypTFVknutoKQS3GHp/go-cid"
)

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

type Link struct {
Name string
Size uint64
Cid *cid.Cid
}

type Reader interface {
io.ReadCloser
io.Seeker
}

type UnixfsAPI interface {
Add(io.Reader) (*cid.Cid, error)
Cat(string) (Reader, error)
Ls(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 ErrOffline = errors.New("can't resolve, ipfs node is offline")
13 changes: 11 additions & 2 deletions core/corehttp/gateway.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package corehttp

import (
"context"
"fmt"
"net"
"net/http"

core "github.com/ipfs/go-ipfs/core"
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
config "github.com/ipfs/go-ipfs/repo/config"
id "gx/ipfs/Qmf4ETeAWXuThBfWwonVyFqGFSgTWepUDEr1txcctvpTXS/go-libp2p/p2p/protocol/identify"
)
Expand All @@ -16,18 +19,24 @@ type GatewayConfig struct {
PathPrefixes []string
}

type apiOption func(context.Context) coreiface.UnixfsAPI

func GatewayOption(paths ...string) ServeOption {
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
cfg, err := n.Repo.Config()
if err != nil {
return nil, err
}

apiOpt := func(ctx context.Context) coreiface.UnixfsAPI {
api := &coreapi.UnixfsAPI{Context: ctx, Node: n}
return api
}
gateway := newGatewayHandler(n, GatewayConfig{
Headers: cfg.Gateway.HTTPHeaders,
Writable: cfg.Gateway.Writable,
PathPrefixes: cfg.Gateway.PathPrefixes,
})
}, apiOpt)

for _, p := range paths {
mux.Handle(p+"/", gateway)
Expand All @@ -37,7 +46,7 @@ func GatewayOption(paths ...string) ServeOption {
}

func VersionOption() ServeOption {
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Commit: %s\n", config.CurrentCommit)
fmt.Fprintf(w, "Client Version: %s\n", id.ClientVersion)
Expand Down
Loading