Skip to content

Commit

Permalink
cleanups and fix example
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Apr 10, 2024
1 parent 29a122d commit 0d2d80a
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 372 deletions.
4 changes: 2 additions & 2 deletions examples/gateway/graph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ func main() {
}
defer (func() { _ = tp.Shutdown(ctx) })()

Check warning on line 28 in examples/gateway/graph/main.go

View check run for this annotation

Codecov / codecov/patch

examples/gateway/graph/main.go#L14-L28

Added lines #L14 - L28 were not covered by tests

carFetcher, err := gateway.NewRemoteCarFetcher([]string{*gatewayUrlPtr}, nil)
carFetcher, err := gateway.NewRemoteCarFetcher([]string{*gatewayUrlPtr})
if err != nil {
log.Fatal(err)
}

Check warning on line 33 in examples/gateway/graph/main.go

View check run for this annotation

Codecov / codecov/patch

examples/gateway/graph/main.go#L30-L33

Added lines #L30 - L33 were not covered by tests

// Creates the gateway with the remote graph backend.
backend, err := gateway.NewGraphGatewayBackend(carFetcher)
backend, err := gateway.NewGraphBackend(carFetcher)
if err != nil {
log.Fatal(err)
}

Check warning on line 39 in examples/gateway/graph/main.go

View check run for this annotation

Codecov / codecov/patch

examples/gateway/graph/main.go#L36-L39

Added lines #L36 - L39 were not covered by tests
Expand Down
45 changes: 45 additions & 0 deletions gateway/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,56 @@ import (
"github.com/ipfs/boxo/ipns"
"github.com/ipfs/boxo/namesys"
"github.com/ipfs/boxo/path"
"github.com/ipfs/boxo/path/resolver"
"github.com/ipfs/go-cid"
routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/prometheus/client_golang/prometheus"
)

type backendOptions struct {
ns namesys.NameSystem
vs routing.ValueStore
r resolver.Resolver
promRegistry prometheus.Registerer
}

// WithNameSystem sets the name system to use with the different backends. If not set
// it will use the default DNSLink resolver generated by [NewDNSResolver] along
// with any configured [routing.ValueStore].
func WithNameSystem(ns namesys.NameSystem) BackendOption {
return func(opts *backendOptions) error {
opts.ns = ns
return nil
}
}

// WithValueStore sets the [routing.ValueStore] to use with the different backends.
func WithValueStore(vs routing.ValueStore) BackendOption {
return func(opts *backendOptions) error {
opts.vs = vs
return nil
}

Check warning on line 42 in gateway/backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/backend.go#L38-L42

Added lines #L38 - L42 were not covered by tests
}

// WithResolver sets the [resolver.Resolver] to use with the different backends.
func WithResolver(r resolver.Resolver) BackendOption {
return func(opts *backendOptions) error {
opts.r = r
return nil
}

Check warning on line 50 in gateway/backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/backend.go#L46-L50

Added lines #L46 - L50 were not covered by tests
}

// WithPrometheusRegistry sets the registry to use with [GraphBackend].
func WithPrometheusRegistry(reg prometheus.Registerer) BackendOption {
return func(opts *backendOptions) error {
opts.promRegistry = reg
return nil
}

Check warning on line 58 in gateway/backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/backend.go#L54-L58

Added lines #L54 - L58 were not covered by tests
}

type BackendOption func(options *backendOptions) error

// baseBackend contains some common backend functionalities that are shared by
// different backend implementations.
type baseBackend struct {
Expand Down
50 changes: 2 additions & 48 deletions gateway/backend_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/ipfs/boxo/ipld/merkledag"
ufile "github.com/ipfs/boxo/ipld/unixfs/file"
uio "github.com/ipfs/boxo/ipld/unixfs/io"
"github.com/ipfs/boxo/namesys"
"github.com/ipfs/boxo/path"
"github.com/ipfs/boxo/path/resolver"
blocks "github.com/ipfs/go-block-format"
Expand All @@ -35,9 +34,7 @@ import (
"github.com/ipld/go-ipld-prime/traversal"
"github.com/ipld/go-ipld-prime/traversal/selector"
selectorparse "github.com/ipld/go-ipld-prime/traversal/selector/parse"
"github.com/libp2p/go-libp2p/core/routing"
mc "github.com/multiformats/go-multicodec"
"github.com/prometheus/client_golang/prometheus"

// Ensure basic codecs are registered.
_ "github.com/ipld/go-ipld-prime/codec/cbor"
Expand All @@ -57,51 +54,8 @@ type BlocksBackend struct {

var _ IPFSBackend = (*BlocksBackend)(nil)

type blocksBackendOptions struct {
ns namesys.NameSystem
vs routing.ValueStore
r resolver.Resolver
promRegistry prometheus.Registerer
}

// WithNameSystem sets the name system to use with the [BlocksBackend]. If not set
// it will use the default DNSLink resolver generated by [NewDNSResolver] along
// with any configured [routing.ValueStore].
func WithNameSystem(ns namesys.NameSystem) BlocksBackendOption {
return func(opts *blocksBackendOptions) error {
opts.ns = ns
return nil
}
}

// WithValueStore sets the [routing.ValueStore] to use with the [BlocksBackend].
func WithValueStore(vs routing.ValueStore) BlocksBackendOption {
return func(opts *blocksBackendOptions) error {
opts.vs = vs
return nil
}
}

// 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
}
}

// WithPrometheusRegistry sets the registry to use for metrics collection.
func WithPrometheusRegistry(reg prometheus.Registerer) BlocksBackendOption {
return func(opts *blocksBackendOptions) error {
opts.promRegistry = reg
return nil
}
}

type BlocksBackendOption func(options *blocksBackendOptions) error

func NewBlocksBackend(blockService blockservice.BlockService, opts ...BlocksBackendOption) (*BlocksBackend, error) {
var compiledOptions blocksBackendOptions
func NewBlocksBackend(blockService blockservice.BlockService, opts ...BackendOption) (*BlocksBackend, error) {
var compiledOptions backendOptions
for _, o := range opts {
if err := o(&compiledOptions); err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 0d2d80a

Please sign in to comment.