11package communication
22
33import (
4+ "github.com/bastean/codexgo/v4/internal/pkg/service/communication/event"
45 "github.com/bastean/codexgo/v4/internal/pkg/service/communication/rabbitmq"
56 "github.com/bastean/codexgo/v4/internal/pkg/service/env"
67 "github.com/bastean/codexgo/v4/internal/pkg/service/errors"
78 "github.com/bastean/codexgo/v4/internal/pkg/service/module/notification"
9+ "github.com/bastean/codexgo/v4/internal/pkg/service/module/user"
810 "github.com/bastean/codexgo/v4/internal/pkg/service/record/log"
9- "github.com/bastean/codexgo/v4/pkg/context/shared/domain/events"
10- "github.com/bastean/codexgo/v4/pkg/context/shared/domain/events/user"
1111)
1212
1313var Service = & struct {
@@ -20,44 +20,65 @@ var Service = &struct {
2020}
2121
2222var (
23- err error
24- RabbitMQ * rabbitmq. RabbitMQ
23+ err error
24+ Bus event. Bus
2525)
2626
2727func Up () error {
28- log .EstablishingConnectionWith (Service .RabbitMQ )
29-
30- RabbitMQ , err = rabbitmq .Open (
31- env .BrokerRabbitMQURI ,
32- env .BrokerRabbitMQName ,
33- rabbitmq .Queues ,
34- rabbitmq.Events {
35- user .CreatedSucceededKey : []events.Consumer {
28+ switch {
29+ case env .HasBroker ():
30+ log .EstablishingConnectionWith (Service .RabbitMQ )
31+
32+ Bus , err = rabbitmq .Open (
33+ env .BrokerRabbitMQURI ,
34+ env .BrokerRabbitMQName ,
35+ rabbitmq .Queues ,
36+ rabbitmq.Events {
37+ user .CreatedSucceededKey : []event.Consumer {
38+ notification .Confirmation ,
39+ },
40+ },
41+ log .Log ,
42+ )
43+
44+ if err != nil {
45+ log .ConnectionFailedWith (Service .RabbitMQ )
46+ return errors .BubbleUp (err , "Up" )
47+ }
48+
49+ log .ConnectionEstablishedWith (Service .RabbitMQ )
50+ default :
51+ log .Starting (Service .EventBus )
52+
53+ Bus , err = event .NewBus (event.Mapper {
54+ user .CreatedSucceededKey : []event.Consumer {
3655 notification .Confirmation ,
3756 },
38- },
39- log .Log ,
40- )
57+ })
4158
42- if err != nil {
43- log .ConnectionFailedWith (Service .RabbitMQ )
44- return errors .BubbleUp (err , "Up" )
45- }
59+ if err != nil {
60+ log .CannotBeStarted (Service .EventBus )
61+ return errors .BubbleUp (err , "Up" )
62+ }
4663
47- log .ConnectionEstablishedWith (Service .RabbitMQ )
64+ log .Started (Service .EventBus )
65+ }
4866
4967 return nil
5068}
5169
5270func Down () error {
53- log .ClosingConnectionWith (Service .RabbitMQ )
71+ switch {
72+ case env .HasBroker ():
73+ log .ClosingConnectionWith (Service .RabbitMQ )
5474
55- if err = rabbitmq .Close (RabbitMQ ); err != nil {
56- log .DisconnectionFailedWith (Service .RabbitMQ )
57- return errors .BubbleUp (err , "Down" )
58- }
75+ if err = rabbitmq .Close (Bus .( * rabbitmq. RabbitMQ ) ); err != nil {
76+ log .DisconnectionFailedWith (Service .RabbitMQ )
77+ return errors .BubbleUp (err , "Down" )
78+ }
5979
60- log .ConnectionClosedWith (Service .RabbitMQ )
80+ log .ConnectionClosedWith (Service .RabbitMQ )
81+ }
6182
6283 return nil
6384}
0 commit comments