forked from aarondl/natscli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (49 loc) · 976 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"flag"
"log"
"sync"
"time"
"github.com/apcera/nats"
)
var (
flagNats = flag.String("nats", "nats://127.0.0.1:4222/", "Nats to connect to")
)
func main() {
flag.Parse()
siphonNats()
}
func siphonNats() {
log.Println("connecting to:", *flagNats)
conn, err := nats.Connect(*flagNats)
if err != nil {
log.Println("could not connect to nats:", err)
return
}
defer conn.Close()
subs := []string{">"}
if len(flag.Args()) > 0 {
subs = flag.Args()
}
wg := &sync.WaitGroup{}
for _, s := range subs {
log.Println("subscribing to:", s)
sub, err := conn.SubscribeSync(s)
if err != nil {
log.Println("failed to subscribe to nats:", err)
return
}
wg.Add(1)
go func(n *nats.Subscription) {
for {
if msg, err := n.NextMsg(1 * time.Hour); err != nil {
log.Println("error getting nats msg:", err)
} else {
log.Printf("[%s] %s", msg.Subject, msg.Data)
}
}
wg.Done()
}(sub)
}
wg.Wait()
}