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

feat(gateway): improve GO API interface, remove Writable API #145

Merged
merged 2 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/ipfs/go-libipfs/blocks"
"github.com/ipfs/go-libipfs/files"
iface "github.com/ipfs/interface-go-ipfs-core"
options "github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/path"
)

Expand All @@ -21,10 +20,10 @@ type Config struct {
// API defines the minimal set of API services required for a gateway handler.
type API interface {
Copy link
Member

@lidel lidel Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: something like IPFSBackend would be more descriptive, but can be changed later, won't block on this one thing (this interface will most likely get revamp in near future anyway)

// GetUnixFsNode returns a read-only handle to a file tree referenced by a path.
GetUnixFsNode(context.Context, path.Path) (files.Node, error)
GetUnixFsNode(context.Context, path.Resolved) (files.Node, error)

// LsUnixFsDir returns the list of links in a directory.
LsUnixFsDir(context.Context, path.Path, ...options.UnixfsLsOption) (<-chan iface.DirEntry, error)
LsUnixFsDir(context.Context, path.Resolved) (<-chan iface.DirEntry, error)

// GetBlock return a block from a certain CID.
GetBlock(context.Context, cid.Cid) (blocks.Block, error)
Expand All @@ -36,7 +35,9 @@ type API interface {
// IsCached returns whether or not the path exists locally.
IsCached(context.Context, path.Path) bool

// ResolvePath resolves the path using UnixFS resolver
// ResolvePath resolves the path using UnixFS resolver. If the path does not
// exist due to a missing link, it should return an error of type:
// https://pkg.go.dev/github.com/ipfs/go-path@v0.3.0/resolver#ErrNoLink
ResolvePath(context.Context, path.Path) (path.Resolved, error)
}

Expand Down
18 changes: 8 additions & 10 deletions gateway/handler_unixfs_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/ipfs/go-libipfs/gateway/assets"
path "github.com/ipfs/go-path"
"github.com/ipfs/go-path/resolver"
options "github.com/ipfs/interface-go-ipfs-core/options"
ipath "github.com/ipfs/interface-go-ipfs-core/path"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -61,9 +60,15 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *

// Check if directory has index.html, if so, serveFile
idxPath := ipath.Join(contentPath, "index.html")
idx, err := i.api.GetUnixFsNode(ctx, idxPath)
idxResolvedPath, err := i.api.ResolvePath(ctx, idxPath)
switch err.(type) {
case nil:
idx, err := i.api.GetUnixFsNode(ctx, idxResolvedPath)
if err != nil {
internalWebError(w, err)
return
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the signature of the getter for UnixFS files:

- GetUnixFsNode(context.Context, path.Path) (files.Node, error)
+ GetUnixFsNode(context.Context, path.Resolved) (files.Node, error)

We already have ResolvePath at our disposal. Let's simplify. The change in this code is to reflect that.

f, ok := idx.(files.File)
if !ok {
internalWebError(w, files.ErrNotReader)
Expand Down Expand Up @@ -104,14 +109,7 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *
return
}

// Optimization: use Unixfs.Ls without resolving children, but using the
// cumulative DAG size as the file size. This allows for a fast listing
// while keeping a good enough Size field.
results, err := i.api.LsUnixFsDir(ctx,
resolvedPath,
options.Unixfs.ResolveChildren(false),
options.Unixfs.UseCumulativeSize(true),
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this optimization to Kubo's codebase. Each gateway implementation should use whatever they want, some might be faster, some slower. It's the implementers choice. This also allowed me to simplify the API signature to remove the options.

- LsUnixFsDir(context.Context, path.Path, ...options.UnixfsLsOption) (<-chan iface.DirEntry, error)
+ LsUnixFsDir(context.Context, path.Resolved) (<-chan iface.DirEntry, error)

results, err := i.api.LsUnixFsDir(ctx, resolvedPath)
if err != nil {
internalWebError(w, err)
return
Expand Down