Add readonly api to gateway
Based on https://github.com/ipfs/go-ipfs/pull/1389 License: MIT Signed-off-by: rht <rhtbot@gmail.com>
rht committed
Aug 15, 2015 at 11:10 UTC
dd99a70a7dda920a0f7dad5ad14e980202fae3ce
4 files changed
+54
-2
cmd/ipfs/daemon.go
+1
@@ -401,6 +401,7 @@ func serveHTTPGateway(req cmds.Request) (error, <-chan error) {
401
}
402
403
var opts = []corehttp.ServeOption{
404
+ corehttp.CommandsROOption(*req.InvocContext()),
405
corehttp.VersionOption(),
406
corehttp.IPNSHostnameOption(),
407
corehttp.GatewayOption(writable),
core/commands/root.go
+30
@@ -109,9 +109,39 @@ var rootSubcommands = map[string]*cmds.Command{
109
"version": VersionCmd,
110
"bitswap": BitswapCmd,
111
}
112
+var rootROSubcommands = map[string]*cmds.Command{
113
+ "block": &cmds.Command{
114
+ Subcommands: map[string]*cmds.Command{
115
+ "stat": blockStatCmd,
116
+ "get": blockGetCmd,
117
+ },
118
+ },
119
+ "cat": CatCmd,
120
+ "commands": CommandsDaemonCmd,
121
+ "ls": LsCmd,
122
+ "name": &cmds.Command{
123
+ Subcommands: map[string]*cmds.Command{
124
+ "resolve": IpnsCmd,
125
+ },
126
+ },
127
+ "object": &cmds.Command{
128
+ Subcommands: map[string]*cmds.Command{
129
+ "data": objectDataCmd,
130
+ "links": objectLinksCmd,
131
+ "get": objectGetCmd,
132
+ "stat": objectStatCmd,
133
+ },
134
+ },
135
+ "refs": RefsCmd,
136
+ //"resolve": ResolveCmd,
137
+}
138
+
139
+var RootRO = &cmds.Command{}
140
141
func init() {
142
+ *RootRO = *Root
143
Root.Subcommands = rootSubcommands
144
+ RootRO.Subcommands = rootROSubcommands
145
}
146
147
type MessageOutput struct {
core/corehttp/commands.go
+10
-2
@@ -99,7 +99,7 @@ func patchCORSVars(c *cmdsHttp.ServerConfig, addr net.Addr) {
99
}
100
}
101
102
-func CommandsOption(cctx commands.Context) ServeOption {
102
+func commandsOption(cctx commands.Context, command *commands.Command) ServeOption {
103
return func(n *core.IpfsNode, l net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
104
105
cfg := &cmdsHttp.ServerConfig{
@@ -113,8 +113,16 @@ func CommandsOption(cctx commands.Context) ServeOption {
113
addCORSDefaults(cfg)
114
patchCORSVars(cfg, l.Addr())
115
116
- cmdHandler := cmdsHttp.NewHandler(cctx, corecommands.Root, cfg)
116
+ cmdHandler := cmdsHttp.NewHandler(cctx, command, cfg)
117
mux.Handle(cmdsHttp.ApiPath+"/", cmdHandler)
118
return mux, nil
119
}
120
}
121
+
122
+func CommandsOption(cctx commands.Context) ServeOption {
123
+ return commandsOption(cctx, corecommands.Root)
124
+}
125
+
126
+func CommandsROOption(cctx commands.Context) ServeOption {
127
+ return commandsOption(cctx, corecommands.RootRO)
128
+}
test/sharness/t0110-gateway.sh
+13
@@ -91,6 +91,19 @@ test_expect_success "log output looks good" '
91
grep "log API client connected" log_out
92
'
93
94
+# test ipfs readonly api
95
+test_expect_success "get IPFS directory file through readonly API succeeds" '
96
+ curl -sfo actual "http://127.0.0.1:$port/api/v0/cat?arg=$HASH2/test"
97
+'
98
+
99
+test_expect_success "get IPFS directory file through readonly API output looks good" '
100
+ test_cmp dir/test actual
101
+'
102
+
103
+test_expect_success "refs IPFS directory file through readonly API succeeds" '
104
+ curl -sfo actual "http://127.0.0.1:$port/api/v0/refs?arg=$HASH2/test"
105
+'
106
+
107
test_kill_ipfs_daemon
108
109
test_done