Skip to content

Commit

Permalink
fix: use publisher adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
WildEgor committed May 7, 2024
1 parent 4b957d2 commit 01159b3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 39 deletions.
25 changes: 25 additions & 0 deletions internal/adapters/publisher/adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package publisher

type EventPublisherAdapter struct {
Publisher IEventPublisher
}

func NewEventPublisherAdapter(cfg IPublisherConfigFactory) *EventPublisherAdapter {

config := cfg.Config()
adapter := &EventPublisherAdapter{}

switch config.Type {
case PublisherTypeRabbitMQ:
pub, err := NewRabbitPublisher(cfg)
if err != nil {
return nil
}

adapter.Publisher = pub

return adapter
default:
return nil
}
}
18 changes: 0 additions & 18 deletions internal/adapters/publisher/factory.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/adapters/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ var AdaptersSet = wire.NewSet(
telegram.NewTelegramBotAdapter,
telegram.NewTelegramListener,
publisher.NewRabbitPublisher,
wire.Bind(new(publisher.IEventPublisher), new(*publisher.RabbitPublisher)),
publisher.NewEventPublisherAdapter,
)
20 changes: 10 additions & 10 deletions internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ var AppSet = wire.NewSet(

// Server represents the main server configuration.
type Server struct {
App *fiber.App
Bot *telegram.TelegramListener
Publisher publisher.IEventPublisher
AppConfig *configs.AppConfig
App *fiber.App
Bot *telegram.TelegramListener
PubAdapter *publisher.EventPublisherAdapter
AppConfig *configs.AppConfig
}

func (srv *Server) Run(ctx context.Context) {
Expand All @@ -58,7 +58,7 @@ func (srv *Server) Shutdown() {
}

slog.Debug("shutdown publisher")
err := srv.Publisher.Close()
err := srv.PubAdapter.Publisher.Close()
if err != nil {
slog.Error("unable to shutdown publisher.", models.LogEntryAttr(&models.LogEntry{
Err: err,
Expand All @@ -74,7 +74,7 @@ func NewApp(
br *router.BotRouter,
sr *router.SwaggerRouter,
bot *telegram.TelegramListener,
ps *publisher.RabbitPublisher,
ps *publisher.EventPublisherAdapter,
pc *configs.PostgresConfig,
) *Server {
logger := slogger.NewLogger(
Expand Down Expand Up @@ -115,9 +115,9 @@ func NewApp(
}

return &Server{
App: app,
Bot: bot,
Publisher: ps,
AppConfig: ac,
App: app,
Bot: bot,
PubAdapter: ps,
AppConfig: ac,
}
}
8 changes: 4 additions & 4 deletions internal/repositories/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ type ITopicsRepository interface {
type TopicRepository struct {
redis *redis.RedisConnection
postgres *postgres.PostgresConnection
publisher publisher.IEventPublisher
pubAdapter *publisher.EventPublisherAdapter
publisherConfig *configs.PublisherConfig
}

func NewTopicsRepository(
redis *redis.RedisConnection,
postgres *postgres.PostgresConnection,
publisher publisher.IEventPublisher,
pubAdapter *publisher.EventPublisherAdapter,
publisherConfig *configs.PublisherConfig,
) *TopicRepository {
return &TopicRepository{
redis,
postgres,
publisher,
pubAdapter,
publisherConfig,
}
}
Expand Down Expand Up @@ -239,7 +239,7 @@ func (r *TopicRepository) LeaveFeedback(feedback *models.CreateTopicFeedbackAttr
return nil, err
}

err = r.publisher.Publish(context.TODO(), r.publisherConfig.Topic, &publisher.Event{
err = r.pubAdapter.Publisher.Publish(context.TODO(), r.publisherConfig.Topic, &publisher.Event{
Data: &publisher.TopicFeedbackEvent{
Pattern: "topic_feedbacks",
Data: publisher.TopicFeedbackEventData{
Expand Down
9 changes: 3 additions & 6 deletions internal/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 01159b3

Please sign in to comment.