-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
114 lines (101 loc) · 3.57 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
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
package main
import (
"context"
"fmt"
"math/rand"
"os"
"time"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/components/cqrs"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/ThreeDotsLabs/watermill/message/router/middleware"
"github.com/ThreeDotsLabs/watermill/pubsub/gochannel"
"github.com/brianvoe/gofakeit/v5"
"github.com/czeslavo/process-manager/1_voiding/billing"
"github.com/czeslavo/process-manager/1_voiding/manager"
"github.com/czeslavo/process-manager/1_voiding/messages"
"github.com/czeslavo/process-manager/1_voiding/reports"
"github.com/google/uuid"
)
func main() {
ctx := context.Background()
logger := watermill.NewStdLogger(false, false)
pubsub := gochannel.NewGoChannel(gochannel.Config{}, logger)
router, err := message.NewRouter(message.RouterConfig{}, logger)
if err != nil {
panic(err)
}
billingService := billing.NewService()
reportsService := reports.NewService()
documentVoidingProcessManager := manager.NewDocumentVoidingProcessManager()
cqrsFacade, err := cqrs.NewFacade(cqrs.FacadeConfig{
GenerateCommandsTopic: func(commandName string) string { return commandName },
CommandHandlers: func(cb *cqrs.CommandBus, eb *cqrs.EventBus) []cqrs.CommandHandler {
var commandHandlers []cqrs.CommandHandler
commandHandlers = append(commandHandlers, billingService.CommandHandlers(eb)...)
commandHandlers = append(commandHandlers, reportsService.CommandHandlers(eb)...)
commandHandlers = append(commandHandlers, documentVoidingProcessManager.CommandHandlers()...)
return commandHandlers
},
CommandsPublisher: pubsub,
CommandsSubscriberConstructor: func(handlerName string) (message.Subscriber, error) {
return pubsub, nil
},
GenerateEventsTopic: func(eventName string) string {
return eventName
},
EventHandlers: func(cb *cqrs.CommandBus, eb *cqrs.EventBus) []cqrs.EventHandler {
var eventHandlers []cqrs.EventHandler
eventHandlers = append(eventHandlers, billingService.EventHandlers()...)
eventHandlers = append(eventHandlers, reportsService.EventHandlers()...)
eventHandlers = append(eventHandlers, documentVoidingProcessManager.EventHandlers(cb)...)
return eventHandlers
},
EventsPublisher: pubsub,
EventsSubscriberConstructor: func(handlerName string) (message.Subscriber, error) {
return pubsub, nil
},
Router: router,
CommandEventMarshaler: cqrs.JSONMarshaler{},
Logger: logger,
})
if err != nil {
panic(err)
}
poisonQueueMiddleware, err := middleware.PoisonQueue(pubsub, "/dev/null")
if err != nil {
panic(err)
}
router.AddMiddleware(poisonQueueMiddleware)
router.AddMiddleware(slowDown)
go simulateCommands(ctx, cqrsFacade.CommandBus())
go runHttpServer(ctx, cqrsFacade.CommandBus(), reportsService, billingService, documentVoidingProcessManager)
if err := router.Run(ctx); err != nil {
panic(err)
}
}
func simulateCommands(ctx context.Context, commandBus *cqrs.CommandBus) {
recipients := []string{
uuid.New().String(),
uuid.New().String(),
uuid.New().String(),
}
for range time.Tick(time.Second * 5) {
if err := commandBus.Send(ctx, &messages.IssueDocument{
DocumentID: uuid.New().String(),
RecipientID: recipients[rand.Intn(len(recipients))],
TotalAmount: gofakeit.Price(1, 50),
}); err != nil {
fmt.Println("Failed to send command")
}
}
}
func slowDown(h message.HandlerFunc) message.HandlerFunc {
shouldSlowDown := os.Getenv("SLOW_DOWN") == "1"
return func(message *message.Message) ([]*message.Message, error) {
if shouldSlowDown {
time.Sleep(time.Millisecond * 250)
}
return h(message)
}
}