Skip to content

Commit

Permalink
gateway/BlocksBackend: add option for custom Resolver (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsanjuan authored Oct 12, 2023
1 parent 25ae0bd commit 6602207
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ The following emojis are used to highlight certain changes:

### Added

* `boxo/gateway`:
* A new `WithResolver(...)` option can be used with `NewBlocksBackend(...)` allowing the user to pass their custom `Resolver` implementation.

### Changed

* `boxo/gateway`
Expand Down
18 changes: 16 additions & 2 deletions gateway/blocks_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var _ IPFSBackend = (*BlocksBackend)(nil)
type blocksBackendOptions struct {
ns namesys.NameSystem
vs routing.ValueStore
r resolver.Resolver
}

// WithNameSystem sets the name system to use with the [BlocksBackend]. If not set
Expand All @@ -87,6 +88,14 @@ func WithValueStore(vs routing.ValueStore) BlocksBackendOption {
}
}

// WithResolver sets the [resolver.Resolver] to use with the [BlocksBackend].
func WithResolver(r resolver.Resolver) BlocksBackendOption {
return func(opts *blocksBackendOptions) error {
opts.r = r
return nil
}
}

type BlocksBackendOption func(options *blocksBackendOptions) error

func NewBlocksBackend(blockService blockservice.BlockService, opts ...BlocksBackendOption) (*BlocksBackend, error) {
Expand All @@ -108,13 +117,12 @@ func NewBlocksBackend(blockService blockservice.BlockService, opts ...BlocksBack
}
return basicnode.Prototype.Any, nil
})
fetcher := fetcherConfig.WithReifier(unixfsnode.Reify)
r := resolver.NewBasicResolver(fetcher)

// Setup a name system so that we are able to resolve /ipns links.
var (
ns namesys.NameSystem
vs routing.ValueStore
r resolver.Resolver
)

vs = compiledOptions.vs
Expand All @@ -135,6 +143,12 @@ func NewBlocksBackend(blockService blockservice.BlockService, opts ...BlocksBack
}
}

r = compiledOptions.r
if r == nil {
fetcher := fetcherConfig.WithReifier(unixfsnode.Reify)
r = resolver.NewBasicResolver(fetcher)
}

return &BlocksBackend{
blockStore: blockService.Blockstore(),
blockService: blockService,
Expand Down

0 comments on commit 6602207

Please sign in to comment.