Skip to content

Commit

Permalink
Add readonly api to gateway
Browse files Browse the repository at this point in the history
Based on #1389

License: MIT
Signed-off-by: rht <rhtbot@gmail.com>
  • Loading branch information
rht committed Aug 15, 2015
1 parent da75e92 commit dd99a70
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ func serveHTTPGateway(req cmds.Request) (error, <-chan error) {
}

var opts = []corehttp.ServeOption{
corehttp.CommandsROOption(*req.InvocContext()),
corehttp.VersionOption(),
corehttp.IPNSHostnameOption(),
corehttp.GatewayOption(writable),
Expand Down
30 changes: 30 additions & 0 deletions core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,39 @@ var rootSubcommands = map[string]*cmds.Command{
"version": VersionCmd,
"bitswap": BitswapCmd,
}
var rootROSubcommands = map[string]*cmds.Command{
"block": &cmds.Command{
Subcommands: map[string]*cmds.Command{
"stat": blockStatCmd,
"get": blockGetCmd,
},
},
"cat": CatCmd,
"commands": CommandsDaemonCmd,
"ls": LsCmd,
"name": &cmds.Command{
Subcommands: map[string]*cmds.Command{
"resolve": IpnsCmd,
},
},
"object": &cmds.Command{
Subcommands: map[string]*cmds.Command{
"data": objectDataCmd,
"links": objectLinksCmd,
"get": objectGetCmd,
"stat": objectStatCmd,
},
},
"refs": RefsCmd,
//"resolve": ResolveCmd,
}

var RootRO = &cmds.Command{}

func init() {
*RootRO = *Root
Root.Subcommands = rootSubcommands
RootRO.Subcommands = rootROSubcommands
}

type MessageOutput struct {
Expand Down
12 changes: 10 additions & 2 deletions core/corehttp/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func patchCORSVars(c *cmdsHttp.ServerConfig, addr net.Addr) {
}
}

func CommandsOption(cctx commands.Context) ServeOption {
func commandsOption(cctx commands.Context, command *commands.Command) ServeOption {
return func(n *core.IpfsNode, l net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {

cfg := &cmdsHttp.ServerConfig{
Expand All @@ -113,8 +113,16 @@ func CommandsOption(cctx commands.Context) ServeOption {
addCORSDefaults(cfg)
patchCORSVars(cfg, l.Addr())

cmdHandler := cmdsHttp.NewHandler(cctx, corecommands.Root, cfg)
cmdHandler := cmdsHttp.NewHandler(cctx, command, cfg)
mux.Handle(cmdsHttp.ApiPath+"/", cmdHandler)
return mux, nil
}
}

func CommandsOption(cctx commands.Context) ServeOption {
return commandsOption(cctx, corecommands.Root)
}

func CommandsROOption(cctx commands.Context) ServeOption {
return commandsOption(cctx, corecommands.RootRO)
}
13 changes: 13 additions & 0 deletions test/sharness/t0110-gateway.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ test_expect_success "log output looks good" '
grep "log API client connected" log_out
'

# test ipfs readonly api
test_expect_success "get IPFS directory file through readonly API succeeds" '
curl -sfo actual "http://127.0.0.1:$port/api/v0/cat?arg=$HASH2/test"
'

test_expect_success "get IPFS directory file through readonly API output looks good" '
test_cmp dir/test actual
'

test_expect_success "refs IPFS directory file through readonly API succeeds" '
curl -sfo actual "http://127.0.0.1:$port/api/v0/refs?arg=$HASH2/test"
'

test_kill_ipfs_daemon

test_done

0 comments on commit dd99a70

Please sign in to comment.