mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-24 14:08:13 +08:00
Add readonly api to gateway
Based on https://github.com/ipfs/go-ipfs/pull/1389 License: MIT Signed-off-by: rht <rhtbot@gmail.com>
This commit is contained in:
@ -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),
|
||||
|
@ -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 {
|
||||
|
@ -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{
|
||||
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user