Skip to content

Instantly share code, notes, and snippets.

@keks
Created November 18, 2016 12:35
Show Gist options
  • Save keks/0f2412fa616129cfc1af3ff011f67d7c to your computer and use it in GitHub Desktop.
Save keks/0f2412fa616129cfc1af3ff011f67d7c to your computer and use it in GitHub Desktop.
package commands
// ...
func (cmd *Command) Call(r Request, w io.Writer) error {
// ...
res := NewResponse(r, w) // pass the writer
cmd.Run(...)
// ...
}
package commands
// ...
type Request struct {
// ...
Mode() RequestMode // json, text, whatevs
}
package commands
var PubsubSubCmd := &Command {
// ...
Run: func(req cmds.Request, res cmds.Response) {
m := FloodsubMarshaller(req.Mode(), res) // res is an io.Writer
sub := floodsub.Subscribe(req.GetArg("topic"))
f, isFlusher := res.(http.Flusher)
go func() {
if isFlusher {
f.Flush()
}
for {
msg := sub.Next()
m.Write(msg)
if isFlusher {
f.Flush()
}
}
}()
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment