Skip to content

Commit

Permalink
whoop whoop
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Mar 25, 2024
1 parent afca373 commit 4f4e412
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
15 changes: 2 additions & 13 deletions examples/gateway/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"net/http"
"strconv"

"github.com/ipfs/boxo/blockservice"
"github.com/ipfs/boxo/examples/gateway/common"
"github.com/ipfs/boxo/exchange/offline"
"github.com/ipfs/boxo/gateway"
)

Expand All @@ -29,20 +27,11 @@ func main() {
}
defer (func() { _ = tp.Shutdown(ctx) })()

// Sets up a blockstore to hold the blocks we request from the gateway
// Note: in a production environment you would likely want to choose a more efficient datastore implementation
// as well as one that has a way of pruning storage so as not to hold data in memory indefinitely.
blockStore, err := gateway.NewProxyBlockstore([]string{*gatewayUrlPtr}, nil)
if err != nil {
log.Fatal(err)
}
blockService := blockservice.New(blockStore, offline.Exchange(blockStore))

// Sets up the routing system, which will proxy the IPNS routing requests to the given gateway.
routing := newProxyRouting(*gatewayUrlPtr, nil)

// Creates the gateway with the block service and the routing.
backend, err := gateway.NewBlocksBackend(blockService, gateway.WithValueStore(routing))
// Creates the gateway with the remote block store backend.
backend, err := gateway.NewRemoteBlocksBackend([]string{*gatewayUrlPtr}, nil, gateway.WithValueStore(routing))

Check warning on line 34 in examples/gateway/proxy/main.go

View check run for this annotation

Codecov / codecov/patch

examples/gateway/proxy/main.go#L33-L34

Added lines #L33 - L34 were not covered by tests
if err != nil {
log.Fatal(err)
}
Expand Down
7 changes: 1 addition & 6 deletions examples/gateway/proxy/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
"strings"
"testing"

"github.com/ipfs/boxo/blockservice"
"github.com/ipfs/boxo/examples/gateway/common"
"github.com/ipfs/boxo/exchange/offline"
"github.com/ipfs/boxo/gateway"
blocks "github.com/ipfs/go-block-format"
"github.com/stretchr/testify/assert"
Expand All @@ -23,11 +21,8 @@ const (
)

func newProxyGateway(t *testing.T, rs *httptest.Server) *httptest.Server {
blockStore, err := gateway.NewProxyBlockstore([]string{rs.URL}, nil)
require.NoError(t, err)
blockService := blockservice.New(blockStore, offline.Exchange(blockStore))
routing := newProxyRouting(rs.URL, nil)
backend, err := gateway.NewBlocksBackend(blockService, gateway.WithValueStore(routing))
backend, err := gateway.NewRemoteBlocksBackend([]string{rs.URL}, nil, gateway.WithValueStore(routing))
require.NoError(t, err)
handler := common.NewHandler(backend)
ts := httptest.NewServer(handler)
Expand Down
12 changes: 12 additions & 0 deletions gateway/blocks_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/ipfs/boxo/blockservice"
blockstore "github.com/ipfs/boxo/blockstore"
"github.com/ipfs/boxo/exchange/offline"
"github.com/ipfs/boxo/fetcher"
bsfetcher "github.com/ipfs/boxo/fetcher/impl/blockservice"
"github.com/ipfs/boxo/files"
Expand Down Expand Up @@ -97,6 +98,17 @@ func WithResolver(r resolver.Resolver) BlocksBackendOption {

type BlocksBackendOption func(options *blocksBackendOptions) error

// NewRemoteBlocksBackend creates a new [BlocksBackend] instance backed by one
// or more gateways. See [NewProxyBlockstore] for more details.
func NewRemoteBlocksBackend(gatewayURL []string, cdns *CachedDNS, opts ...BlocksBackendOption) (*BlocksBackend, error) {
blockStore, err := NewProxyBlockstore(gatewayURL, cdns)
if err != nil {
return nil, err
}
blockService := blockservice.New(blockStore, offline.Exchange(blockStore))
return NewBlocksBackend(blockService, opts...)

Check warning on line 109 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L103-L109

Added lines #L103 - L109 were not covered by tests
}

func NewBlocksBackend(blockService blockservice.BlockService, opts ...BlocksBackendOption) (*BlocksBackend, error) {
var compiledOptions blocksBackendOptions
for _, o := range opts {
Expand Down
3 changes: 2 additions & 1 deletion gateway/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ type proxyBlockstore struct {
var _ blockstore.Blockstore = (*proxyBlockstore)(nil)

// NewProxyBlockstore creates a new [blockstore.Blockstore] that is backed by one
// or more gateways that follow the [Trustless Gateway] specification.
// or more gateways that support RAW block requests. See the [Trustless Gateway]
// specification for more details.
//
// [Trustless Gateway]: https://specs.ipfs.tech/http-gateways/trustless-gateway/
func NewProxyBlockstore(gatewayURL []string, cdns *CachedDNS) (blockstore.Blockstore, error) {
Expand Down

0 comments on commit 4f4e412

Please sign in to comment.