/
pubsub.go
196 lines (177 loc) · 5.14 KB
/
pubsub.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package cmd
import (
"context"
"errors"
"github.com/google/uuid"
"github.com/kubemq-io/kubemq-go"
"github.com/kubemq-io/kubetools/transport/option"
"github.com/spf13/cobra"
"log"
)
var pubsubTransport string
var pubsubRecGroup string
var pubsubSendTimeout int
var pubsubCmd = &cobra.Command{
Use: "pubsub",
Aliases: []string{"p", "ps"},
Short: "send and receive pub/sub real-time and persistent events",
}
var pubsubRecCmd = &cobra.Command{
Use: "receive",
Aliases: []string{"rec", "r"},
Short: "receive pub/sub real-time and persistent events",
}
var pubsubSendCmd = &cobra.Command{
Use: "send",
Aliases: []string{"s"},
Short: "send pub/sub real-time and persistent events",
}
var pubsubRecEventsCmd = &cobra.Command{
Use: "events",
Aliases: []string{"e"},
Short: "subscribe to receive real-time events from a channel",
Run: func(cmd *cobra.Command, args []string) {
runPubSub(args, "sub_events")
},
}
var pubsubRecEventsStoreCmd = &cobra.Command{
Use: "events_store",
Aliases: []string{"es"},
Short: "subscribe to receive persistent events from channel",
Run: func(cmd *cobra.Command, args []string) {
runPubSub(args, "sub_events_store")
},
}
var pubsubSendEventsCmd = &cobra.Command{
Use: "events",
Aliases: []string{"e"},
Short: "send real-time event to a channel",
Run: func(cmd *cobra.Command, args []string) {
runPubSub(args, "send_event")
},
}
var pubsubSendEventsStoreCmd = &cobra.Command{
Use: "events_store",
Aliases: []string{"es"},
Short: "send persistent event to a channel",
Run: func(cmd *cobra.Command, args []string) {
runPubSub(args, "send_event_store")
},
}
func runPubSub(args []string, kind string) {
ctx, cancel := context.WithCancel(context.Background())
defer func() {
cancel()
}()
client, err := getpubsubClient(ctx)
if err != nil {
log.Fatalf("error on create send client: %s", err.Error())
return
}
errCh := make(chan error, 1)
switch kind {
case "send_event":
if len(args) != 2 {
log.Fatal("invalid args, should be a channel name and message body")
return
}
err := client.E().SetChannel(args[0]).SetBody([]byte(args[1])).Send(ctx)
if err != nil {
log.Printf("error sending event: %s", err.Error())
}
case "send_event_store":
if len(args) != 2 {
log.Fatal("invalid args, should be a channel name and message body")
return
}
res, err := client.ES().SetChannel(args[0]).SetBody([]byte(args[1])).Send(ctx)
if err != nil {
log.Printf("error sending event_store: %s", err.Error())
}
if res.Err != nil {
log.Printf("error sending event_store: %s", res.Err)
}
case "sub_events":
if len(args) != 1 {
log.Fatal("invalid args, should be a channel name")
return
}
log.Printf("subscribe to %s channel\n", args[0])
eventsCh, err := client.SubscribeToEvents(ctx, args[0], pubsubRecGroup, errCh)
if err != nil {
log.Printf("Error:\n\t%s\n", err.Error())
return
}
for {
select {
case event, more := <-eventsCh:
if !more {
return
}
log.Printf("Message:\n\t%s\n", event.Body)
case err := <-errCh:
log.Printf("Error:\n\t%s\n", err.Error())
case <-ctx.Done():
return
}
}
case "sub_events_store":
if len(args) != 1 {
log.Fatal("invalid args, should be a channel name")
return
}
log.Printf("subscribe to %s channel\n", args[0])
eventsStoreCh, err := client.SubscribeToEventsStore(ctx, args[0], pubsubRecGroup, errCh, kubemq.StartFromNewEvents())
if err != nil {
log.Printf("Error:\n\t%s\n", err.Error())
return
}
for {
select {
case eventStore, more := <-eventsStoreCh:
if !more {
return
}
log.Printf("Message:\n\t%s\n", eventStore.Body)
case err := <-errCh:
log.Printf("Error:\n\t%s\n", err.Error())
case <-ctx.Done():
return
}
}
}
}
func init() {
rootCmd.AddCommand(pubsubCmd)
pubsubCmd.AddCommand(pubsubSendCmd, pubsubRecCmd)
pubsubSendCmd.AddCommand(pubsubSendEventsCmd, pubsubSendEventsStoreCmd)
pubsubRecCmd.AddCommand(pubsubRecEventsCmd, pubsubRecEventsStoreCmd)
pubsubCmd.PersistentFlags().StringVarP(&pubsubTransport, "pubsubTransport", "t", "grpc", "set transport type, grpc or rest")
pubsubRecEventsCmd.PersistentFlags().StringVarP(&pubsubRecGroup, "pubsubRecGroup", "g", "", "set optional group for a channel")
pubsubRecEventsStoreCmd.PersistentFlags().StringVarP(&pubsubRecGroup, "pubsubRecGroup", "g", "", "set optional group for a channel")
}
func getpubsubClient(ctx context.Context) (*kubemq.Client, error) {
switch pubsubTransport {
case "grpc":
for _, conn := range cfg.Connections {
if conn.Kind == option.ConnectionTypeGrpc {
client, err := kubemq.NewClient(ctx,
kubemq.WithAddress(conn.Host, conn.Port),
kubemq.WithClientId(uuid.New().String()),
kubemq.WithTransportType(kubemq.TransportTypeGRPC))
return client, err
}
}
case "rest":
for _, conn := range cfg.Connections {
if conn.Kind == option.ConnectionTypeRest {
client, err := kubemq.NewClient(ctx,
kubemq.WithUri(conn.Uri()),
kubemq.WithClientId(uuid.New().String()),
kubemq.WithTransportType(kubemq.TransportTypeRest))
return client, err
}
}
}
return nil, errors.New("invalid transport type")
}