-
Notifications
You must be signed in to change notification settings - Fork 413
/
events.go
185 lines (165 loc) · 4.38 KB
/
events.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
package main
import (
"context"
"fmt"
"time"
"cloud.google.com/go/pubsub"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill-googlecloud/pkg/googlecloud"
"github.com/ThreeDotsLabs/watermill-http/v2/pkg/http"
"github.com/ThreeDotsLabs/watermill/components/cqrs"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/ThreeDotsLabs/watermill/message/router/middleware"
)
type PostViewed struct {
PostID int `json:"post_id"`
}
type PostReactionAdded struct {
PostID int `json:"post_id"`
ReactionID string `json:"reaction_id"`
}
type PostStatsUpdated struct {
PostID int `json:"post_id"`
Views int `json:"views"`
ViewsUpdated bool `json:"views_updated"`
Reactions map[string]int `json:"reactions"`
ReactionUpdated *string `json:"reaction_updated"`
}
type Routers struct {
EventsRouter *message.Router
SSERouter http.SSERouter
EventBus *cqrs.EventBus
}
func NewRouters(cfg config, repo *Repository) (Routers, error) {
logger := watermill.NewStdLogger(false, false)
publisher, err := googlecloud.NewPublisher(
googlecloud.PublisherConfig{
ProjectID: cfg.PubSubProjectID,
},
logger,
)
if err != nil {
return Routers{}, err
}
eventBus, err := cqrs.NewEventBusWithConfig(
publisher,
cqrs.EventBusConfig{
GeneratePublishTopic: func(params cqrs.GenerateEventPublishTopicParams) (string, error) {
return params.EventName, nil
},
Marshaler: cqrs.JSONMarshaler{},
Logger: logger,
},
)
if err != nil {
return Routers{}, err
}
eventsRouter, err := message.NewRouter(message.RouterConfig{}, logger)
if err != nil {
return Routers{}, err
}
eventsRouter.AddMiddleware(middleware.Recoverer)
eventProcessor, err := cqrs.NewEventProcessorWithConfig(
eventsRouter,
cqrs.EventProcessorConfig{
GenerateSubscribeTopic: func(params cqrs.EventProcessorGenerateSubscribeTopicParams) (string, error) {
return params.EventName, nil
},
SubscriberConstructor: func(params cqrs.EventProcessorSubscriberConstructorParams) (message.Subscriber, error) {
return googlecloud.NewSubscriber(
googlecloud.SubscriberConfig{
ProjectID: cfg.PubSubProjectID,
GenerateSubscriptionName: func(topic string) string {
return fmt.Sprintf("%v_%v", topic, params.HandlerName)
},
},
logger,
)
},
Marshaler: cqrs.JSONMarshaler{},
Logger: logger,
},
)
if err != nil {
return Routers{}, err
}
err = eventProcessor.AddHandlers(
cqrs.NewEventHandler(
"UpdateViews",
func(ctx context.Context, event *PostViewed) error {
var views int
var reactions map[string]int
err = repo.UpdatePost(ctx, event.PostID, func(post *Post) {
post.Views++
views = post.Views
reactions = post.Reactions
})
if err != nil {
return err
}
statsUpdated := PostStatsUpdated{
PostID: event.PostID,
ViewsUpdated: true,
Views: views,
Reactions: reactions,
}
return eventBus.Publish(ctx, statsUpdated)
},
),
cqrs.NewEventHandler(
"UpdateReactions",
func(ctx context.Context, event *PostReactionAdded) error {
var views int
var reactions map[string]int
err := repo.UpdatePost(ctx, event.PostID, func(post *Post) {
post.Reactions[event.ReactionID]++
views = post.Views
reactions = post.Reactions
})
if err != nil {
return err
}
statsUpdated := PostStatsUpdated{
PostID: event.PostID,
Views: views,
ReactionUpdated: &event.ReactionID,
Reactions: reactions,
}
return eventBus.Publish(ctx, statsUpdated)
},
),
)
if err != nil {
return Routers{}, err
}
sseSubscriber, err := googlecloud.NewSubscriber(
googlecloud.SubscriberConfig{
ProjectID: cfg.PubSubProjectID,
GenerateSubscriptionName: func(topic string) string {
return fmt.Sprintf("%v_%v", topic, watermill.NewShortUUID())
},
SubscriptionConfig: pubsub.SubscriptionConfig{
ExpirationPolicy: time.Hour * 24,
},
},
logger,
)
if err != nil {
return Routers{}, err
}
sseRouter, err := http.NewSSERouter(
http.SSERouterConfig{
UpstreamSubscriber: sseSubscriber,
Marshaler: http.StringSSEMarshaler{},
},
logger,
)
if err != nil {
return Routers{}, err
}
return Routers{
EventsRouter: eventsRouter,
SSERouter: sseRouter,
EventBus: eventBus,
}, nil
}