Skip to content

Commit 1656e91

Browse files
committed
feat(service): add default event bus
1 parent 4e072b7 commit 1656e91

12 files changed

Lines changed: 82 additions & 50 deletions

File tree

internal/app/server/router/api/api.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ type API struct {
1111
*gin.RouterGroup
1212
}
1313

14-
func (a *API) Public() {
15-
public := a.Group("/")
14+
func (api *API) Public() {
15+
public := api.Group("/")
1616

1717
account := public.Group("/account")
1818

@@ -22,8 +22,8 @@ func (a *API) Public() {
2222
account.GET("/verify/:id", user.Verify)
2323
}
2424

25-
func (a *API) Private() {
26-
private := a.Group("/", middleware.Authentication)
25+
func (api *API) Private() {
26+
private := api.Group("/", middleware.Authentication)
2727

2828
account := private.Group("/account")
2929

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package communication
22

33
import (
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

1313
var Service = &struct {
@@ -20,44 +20,65 @@ var Service = &struct {
2020
}
2121

2222
var (
23-
err error
24-
RabbitMQ *rabbitmq.RabbitMQ
23+
err error
24+
Bus event.Bus
2525
)
2626

2727
func 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

5270
func 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
}

internal/pkg/service/communication/event/event.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ import (
55
"github.com/bastean/codexgo/v4/pkg/context/shared/infrastructure/communications/memory"
66
)
77

8-
var (
9-
Bus events.Bus
10-
)
11-
128
var (
139
NewBus = memory.NewEventBus
1410
)
1511

1612
type (
17-
Mapper = memory.EventMapper
1813
Consumer = events.Consumer
14+
Bus = events.Bus
15+
Mapper = memory.EventMapper
1916
)

internal/pkg/service/communication/rabbitmq/rabbitmq.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"reflect"
55

66
"github.com/bastean/codexgo/v4/internal/pkg/service/errors"
7-
"github.com/bastean/codexgo/v4/pkg/context/shared/domain/events/user"
7+
"github.com/bastean/codexgo/v4/internal/pkg/service/module/user"
88
"github.com/bastean/codexgo/v4/pkg/context/shared/domain/loggers"
99
"github.com/bastean/codexgo/v4/pkg/context/shared/domain/messages"
1010
"github.com/bastean/codexgo/v4/pkg/context/shared/infrastructure/communications/rabbitmq"

internal/pkg/service/env/env.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ const (
4848
)
4949

5050
var ENV = map[key]required{
51-
BROKER_RABBITMQ_URI: true,
52-
BROKER_RABBITMQ_NAME: true,
51+
BROKER_RABBITMQ_URI: false,
52+
BROKER_RABBITMQ_NAME: false,
5353

5454
DATABASE_MONGODB_URI: true,
5555
DATABASE_MONGODB_NAME: true,

internal/pkg/service/module/module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func Start() error {
3333

3434
user.Start(
3535
collection,
36-
communication.RabbitMQ,
36+
communication.Bus,
3737
bcrypt.Bcrypt,
3838
)
3939

internal/pkg/service/module/notification/notification.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package notification
22

33
import (
4+
"github.com/bastean/codexgo/v4/internal/pkg/service/module/user"
45
"github.com/bastean/codexgo/v4/pkg/context/notification/application/confirmation"
5-
"github.com/bastean/codexgo/v4/pkg/context/shared/domain/events/user"
66
"github.com/bastean/codexgo/v4/pkg/context/shared/domain/transfers"
77
)
88

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package user
2+
3+
import (
4+
"github.com/bastean/codexgo/v4/pkg/context/shared/domain/events/user"
5+
)
6+
7+
var (
8+
CreatedSucceededKey = user.CreatedSucceededKey
9+
)
10+
11+
type (
12+
CreatedSucceededAttributes = user.CreatedSucceededAttributes
13+
CreatedSucceededMeta = user.CreatedSucceededMeta
14+
)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package user
22

33
import (
4-
"github.com/bastean/codexgo/v4/pkg/context/user/infrastructure/persistence/mongodb"
4+
"github.com/bastean/codexgo/v4/pkg/context/user/infrastructure/persistence/mongodb/collection"
55
)
66

77
const (
88
CollectionName = "users"
99
)
1010

1111
var (
12-
OpenCollection = mongodb.OpenCollection
12+
OpenCollection = collection.Open
1313
)

internal/pkg/service/module/user/user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package user
22

33
import (
4-
"github.com/bastean/codexgo/v4/pkg/context/shared/domain/events"
4+
"github.com/bastean/codexgo/v4/internal/pkg/service/communication/event"
55
"github.com/bastean/codexgo/v4/pkg/context/shared/domain/hashes"
66
"github.com/bastean/codexgo/v4/pkg/context/user/application/create"
77
"github.com/bastean/codexgo/v4/pkg/context/user/application/delete"
@@ -12,7 +12,7 @@ import (
1212
"github.com/bastean/codexgo/v4/pkg/context/user/domain/repository"
1313
)
1414

15-
func Start(repository repository.Repository, bus events.Bus, hashing hashes.Hashing) {
15+
func Start(repository repository.Repository, bus event.Bus, hashing hashes.Hashing) {
1616
CreateHandler = &create.Handler{
1717
Create: &create.Case{
1818
Repository: repository,

0 commit comments

Comments
 (0)