-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (49 loc) · 1.31 KB
/
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
60
61
62
63
64
65
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"strconv"
"time"
"github.com/jnst/event-driven-architecture/pubsub"
)
func main() {
const EnvKeyAwsProfile = "AWS_PROFILE"
profile, ok := os.LookupEnv(EnvKeyAwsProfile)
if !ok {
panic("missing env: " + EnvKeyAwsProfile)
}
var butler *pubsub.Butler
if profile == "dummy" {
butler = pubsub.NewButlerWithLocalstack()
} else {
butler = pubsub.NewButlerWithProfile(profile)
}
broker := butler.Prepare("es-topic", "es-queue")
fmt.Println("========== Prepare done ==========")
// Subscriber polling topic every second.
// When the subscriber polling and receives a message, it takes action according to the type of the event.
subscriber := pubsub.NewSubscriber(butler.Sqs)
ctx := context.Background()
go subscriber.Subscribe(ctx, broker.QueueURL)
// Publisher sends message to topic every 5 seconds.
publisher := pubsub.NewPublisher(butler.Sns)
for i := 1; i < 5; i++ {
userID := strconv.Itoa(i)
msg := pubsub.UserEvent{
UserID: userID,
Status: "user.created",
Time: time.Now().Unix(),
}
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
_, _ = publisher.Publish(broker.TopicARN, string(b))
time.Sleep(5 * time.Second)
}
fmt.Println("========== Sample code done ==========")
ctx.Done()
butler.Destroy()
}