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 Jan 19, 2018
1 parent a444271 commit bffa0e0
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 @@ -15,6 +15,7 @@ import (
type GatewayConfig struct {
Headers map[string][]string
Writable bool
FetchBlocks bool
PathPrefixes []string
}

Expand All @@ -26,13 +27,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 @@ -229,6 +229,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 @@ -51,6 +51,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 @@ -195,4 +195,30 @@ test_expect_success "GET compact blocks succeeds" '

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 bffa0e0

Please sign in to comment.