events
2015-07-15
events/stdout.go
Create Publisher interface and stdout publisher. Create a Publisher interface that allows us to use different implementations and switch between them easily. The NSQ publisher already implements the interface, so it was really just copying that. Create an implmentation of the Publisher interface that just logs the data to stdout.
| paddy@3 | 1 package events |
| paddy@3 | 2 |
| paddy@3 | 3 import ( |
| paddy@3 | 4 "encoding/json" |
| paddy@3 | 5 "os" |
| paddy@3 | 6 ) |
| paddy@3 | 7 |
| paddy@3 | 8 type StdoutPublisher struct{} |
| paddy@3 | 9 |
| paddy@3 | 10 func NewStdoutPublisher() StdoutPublisher { |
| paddy@3 | 11 return StdoutPublisher{} |
| paddy@3 | 12 } |
| paddy@3 | 13 |
| paddy@3 | 14 func (p StdoutPublisher) Publish(topic string, e Event) error { |
| paddy@3 | 15 os.Stdout.Write([]byte(topic + ": ")) |
| paddy@3 | 16 enc := json.NewEncoder(os.Stdout) |
| paddy@3 | 17 return enc.Encode(e) |
| paddy@3 | 18 } |