Skip to content

Commit

Permalink
Add 'FetchBlocks' argument to Gateway constructor
Browse files Browse the repository at this point in the history
If FetchBlocks=false, don't fetch blocks which are not
already present in storage.

Add Gateway.FetchBlocks key to config.

License: MIT
Signed-off-by: Iaroslav Gridin <voker57@gmail.com>
  • Loading branch information
Voker57 committed Oct 20, 2017
1 parent 2e6f730 commit 8cc6abc
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
4 changes: 3 additions & 1 deletion core/corehttp/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type GatewayConfig struct {
Headers map[string][]string
Writable bool
FetchBlocks bool
PathPrefixes []string
}

Expand All @@ -25,13 +26,14 @@ func ConfigFromNode(n *core.IpfsNode) (*GatewayConfig, error) {
gc := &GatewayConfig{
Headers: cfg.Gateway.HTTPHeaders,
PathPrefixes: cfg.Gateway.PathPrefixes,
FetchBlocks: cfg.Gateway.FetchBlocks,
}
return gc, nil
}

func GatewayOption(gc GatewayConfig, paths ...string) ServeOption {
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
gateway := newGatewayHandler(n, gc, coreapi.NewCoreAPI(n))
gateway := newGatewayHandler(n, gc, coreapi.NewCoreAPI(n, !gc.FetchBlocks))

for _, p := range paths {
mux.Handle(p+"/", gateway)
Expand Down
5 changes: 5 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ A boolean to configure whether the gateway is writeable or not.

Default: `false`

- `FetchBlocks`
A boolean to configure whether the gateway should fetch blocks from network to satisfy requests.

Default: `true`

- `PathPrefixes`
TODO

Expand Down
1 change: 1 addition & 0 deletions repo/config/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type Gateway struct {
RootRedirect string
Writable bool
PathPrefixes []string
FetchBlocks bool // Fetch blocks for requests
}
1 change: 1 addition & 0 deletions repo/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
Gateway: Gateway{
RootRedirect: "",
Writable: false,
FetchBlocks: true,
PathPrefixes: []string{},
HTTPHeaders: map[string][]string{
"Access-Control-Allow-Origin": []string{"*"},
Expand Down
26 changes: 26 additions & 0 deletions test/sharness/t0110-gateway.sh
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,30 @@ test_expect_success "try fetching it from gateway" '

test_kill_ipfs_daemon

test_expect_success "set FetchBlocks to false in config" '
ipfs config --bool=false Gateway.FetchBlocks
'

test_launch_ipfs_daemon

port=$GWAY_PORT
apiport=$API_PORT

test_expect_success "try fetching not present key from offline gateway" '
echo "hi" | ipfs add --hash-only -Q > hi.hash
test_expect_code 22 curl -f "http://127.0.0.1:$port/ipfs/$(cat hi.hash)"
'

test_expect_success "try fetching present key from offline gateway" '
echo "hi" | ipfs add -Q > hi.hash &&
echo "http://127.0.0.1:$port/ipfs/$(cat hi.hash)" &&
curl -f "http://127.0.0.1:$port/ipfs/$(cat hi.hash)"
'

test_kill_ipfs_daemon

test_expect_success "set FetchBlocks to true in config" '
ipfs config --bool=true Gateway.FetchBlocks
'

test_done

0 comments on commit 8cc6abc

Please sign in to comment.