mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 10:27:46 +08:00
commands: Use a flag to enable streaming channel output
This commit is contained in:
parent
abd390b892
commit
981f793df9
@ -42,6 +42,9 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
|
||||
// override with json to send to server
|
||||
req.SetOption(cmds.EncShort, cmds.JSON)
|
||||
|
||||
// stream channel output
|
||||
req.SetOption(cmds.ChanOpt, "true")
|
||||
|
||||
query, err := getQuery(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@ -84,13 +84,6 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set(contentTypeHeader, mime)
|
||||
}
|
||||
|
||||
// if the res output is a channel, set a custom header for it
|
||||
isChan := false
|
||||
if _, ok := res.Output().(chan interface{}); ok {
|
||||
w.Header().Set(channelHeader, "1")
|
||||
isChan = true
|
||||
}
|
||||
|
||||
// if response contains an error, write an HTTP error status code
|
||||
if e := res.Error(); e != nil {
|
||||
if e.Code == cmds.ErrClient {
|
||||
@ -108,11 +101,14 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if isChan {
|
||||
// if output is a channel and user requested streaming channels,
|
||||
// use chunk copier for the output
|
||||
_, isChan := res.Output().(chan interface{})
|
||||
streamChans, _, _ := req.Option("stream-channels").Bool()
|
||||
if isChan && streamChans {
|
||||
err = copyChunks(w, out)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
fmt.Println(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -158,15 +158,18 @@ const (
|
||||
EncLong = "encoding"
|
||||
RecShort = "r"
|
||||
RecLong = "recursive"
|
||||
ChanOpt = "stream-channels"
|
||||
)
|
||||
|
||||
// options that are used by this package
|
||||
var OptionEncodingType = StringOption(EncShort, EncLong, "The encoding type the output should be encoded with (json, xml, or text)")
|
||||
var OptionRecursivePath = BoolOption(RecShort, RecLong, "Add directory paths recursively")
|
||||
var OptionStreamChannels = BoolOption(ChanOpt, "Stream channel output")
|
||||
|
||||
// global options, added to every command
|
||||
var globalOptions = []Option{
|
||||
OptionEncodingType,
|
||||
OptionStreamChannels,
|
||||
}
|
||||
|
||||
// the above array of Options, wrapped in a Command
|
||||
|
||||
Loading…
Reference in New Issue
Block a user