Skip to content

Commit

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

Add --offline-gateway option to `ipfs daemon`

License: MIT
Signed-off-by: Iaroslav Gridin <voker57@gmail.com>
  • Loading branch information
Voker57 committed Aug 28, 2017
1 parent a781213 commit 30ac2b7
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
13 changes: 10 additions & 3 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
writableKwd = "writable"
enableFloodSubKwd = "enable-pubsub-experiment"
enableMultiplexKwd = "enable-mplex-experiment"
offlineGatewayKwd = "offline-gateway"
// apiAddrKwd = "address-api"
// swarmAddrKwd = "address-swarm"
)
Expand Down Expand Up @@ -148,6 +149,7 @@ Headers.
cmds.BoolOption(initOptionKwd, "Initialize ipfs with default settings if not already initialized").Default(false),
cmds.StringOption(routingOptionKwd, "Overrides the routing option").Default("dht"),
cmds.BoolOption(mountKwd, "Mounts IPFS to the filesystem").Default(false),
cmds.BoolOption(offlineGatewayKwd, "Do not fetch blocks for gateway requests").Default(false),
cmds.BoolOption(writableKwd, "Enable writing objects (with POST, PUT and DELETE)").Default(false),
cmds.StringOption(ipfsMountKwd, "Path to the mountpoint for IPFS (if using --mount). Defaults to config setting."),
cmds.StringOption(ipnsMountKwd, "Path to the mountpoint for IPNS (if using --mount). Defaults to config setting."),
Expand Down Expand Up @@ -453,9 +455,12 @@ func serveHTTPApi(req cmds.Request) (error, <-chan error) {
if err != nil {
return fmt.Errorf("serveHTTPApi: Option(%s) failed: %s", unrestrictedApiAccessKwd, err), nil
}
gatewayOpt := corehttp.GatewayOption(false, corehttp.WebUIPaths...)

offline, _, err := req.Option(offlineGatewayKwd).Bool()

gatewayOpt := corehttp.GatewayOption(false, offline, corehttp.WebUIPaths...)
if unrestricted {
gatewayOpt = corehttp.GatewayOption(true, "/ipfs", "/ipns")
gatewayOpt = corehttp.GatewayOption(true, offline, "/ipfs", "/ipns")
}

var opts = []corehttp.ServeOption{
Expand Down Expand Up @@ -542,6 +547,8 @@ func serveHTTPGateway(req cmds.Request) (error, <-chan error) {
writable = cfg.Gateway.Writable
}

offline, _, err := req.Option(offlineGatewayKwd).Bool()

gwLis, err := manet.Listen(gatewayMaddr)
if err != nil {
return fmt.Errorf("serveHTTPGateway: manet.Listen(%s) failed: %s", gatewayMaddr, err), nil
Expand All @@ -560,7 +567,7 @@ func serveHTTPGateway(req cmds.Request) (error, <-chan error) {
corehttp.CommandsROOption(*req.InvocContext()),
corehttp.VersionOption(),
corehttp.IPNSHostnameOption(),
corehttp.GatewayOption(writable, "/ipfs", "/ipns"),
corehttp.GatewayOption(writable, offline, "/ipfs", "/ipns"),
}

if len(cfg.Gateway.RootRedirect) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/ipfswatch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func run(ipfsPath, watchPath string) error {
if *http {
addr := "/ip4/127.0.0.1/tcp/5001"
var opts = []corehttp.ServeOption{
corehttp.GatewayOption(true, "/ipfs", "/ipns"),
corehttp.GatewayOption(true, false, "/ipfs", "/ipns"),
corehttp.WebUIOption,
corehttp.CommandsOption(cmdCtx(node, ipfsPath)),
}
Expand Down
4 changes: 2 additions & 2 deletions core/corehttp/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type GatewayConfig struct {
PathPrefixes []string
}

func GatewayOption(writable bool, paths ...string) ServeOption {
func GatewayOption(writable bool, offline bool, paths ...string) ServeOption {
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
cfg, err := n.Repo.Config()
if err != nil {
Expand All @@ -28,7 +28,7 @@ func GatewayOption(writable bool, paths ...string) ServeOption {
Headers: cfg.Gateway.HTTPHeaders,
Writable: writable,
PathPrefixes: cfg.Gateway.PathPrefixes,
}, coreapi.NewCoreAPI(n))
}, coreapi.NewCoreAPI(n, offline))

for _, p := range paths {
mux.Handle(p+"/", gateway)
Expand Down
2 changes: 1 addition & 1 deletion core/corehttp/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func newTestServerAndNode(t *testing.T, ns mockNamesys) (*httptest.Server, *core
ts.Listener,
VersionOption(),
IPNSHostnameOption(),
GatewayOption(false, "/ipfs", "/ipns"),
GatewayOption(false, false, "/ipfs", "/ipns"),
)
if err != nil {
t.Fatal(err)
Expand Down
18 changes: 18 additions & 0 deletions test/sharness/t0110-gateway.sh
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,22 @@ test_expect_success "try fetching it from gateway" '

test_kill_ipfs_daemon

test_launch_ipfs_daemon --offline-gateway

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_done
2 changes: 1 addition & 1 deletion test/supernode_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func run() error {

opts := []corehttp.ServeOption{
corehttp.CommandsOption(cmdCtx(node, repoPath)),
corehttp.GatewayOption(false),
corehttp.GatewayOption(false, false),
}

if *cat {
Expand Down

0 comments on commit 30ac2b7

Please sign in to comment.