generated from WildEgor/e-shop-fiber-microservice-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: README.md, add rabbitmq publisher for feedbacks
- Loading branch information
Showing
34 changed files
with
348 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ dist | |
vendor | ||
docker-compose.yml | ||
.air.toml | ||
.env | ||
.env | ||
.env.docker | ||
.env.example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,7 @@ TELEGRAM_BOT_TOKEN= | |
TELEGRAM_BOT_PREFIX=eshop_support_bot | ||
|
||
DEFAULT_LOCALE=ru-RU | ||
|
||
PUBLISHER_TOPIC=collector | ||
PUBLISHER_ADDR=amqp://guest:[email protected]:5672/ | ||
PUBLISHER_TYPE=rabbitmq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ tmp | |
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
.env | ||
.env | ||
.env.docker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package publisher | ||
|
||
func NewPublisher(cfg IPublisherConfigFactory) IEventPublisher { | ||
|
||
config := cfg.Config() | ||
|
||
switch config.Type { | ||
case PublisherTypeRabbitMQ: | ||
pub, err := NewRabbitPublisher(cfg) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
return pub | ||
default: | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package publisher | ||
|
||
import ( | ||
"fmt" | ||
"github.com/wagslane/go-rabbitmq" | ||
"log/slog" | ||
) | ||
|
||
var _ rabbitmq.Logger = (*PublisherLogger)(nil) | ||
|
||
type PublisherLogger struct { | ||
} | ||
|
||
func (t PublisherLogger) Fatalf(s string, i ...interface{}) { | ||
slog.Error(fmt.Sprintf(s, i)) | ||
Check failure on line 15 in internal/adapters/publisher/logger.go GitHub Actions / lint
|
||
panic(fmt.Sprintf(s, i)) | ||
Check failure on line 16 in internal/adapters/publisher/logger.go GitHub Actions / lint
|
||
} | ||
|
||
func (t PublisherLogger) Errorf(s string, i ...interface{}) { | ||
slog.Error(fmt.Sprintf(s, i)) | ||
} | ||
|
||
func (t PublisherLogger) Warnf(s string, i ...interface{}) { | ||
slog.Warn(fmt.Sprintf(s, i)) | ||
} | ||
|
||
func (t PublisherLogger) Infof(s string, i ...interface{}) { | ||
slog.Info(fmt.Sprintf(s, i)) | ||
} | ||
|
||
func (t PublisherLogger) Debugf(s string, i ...interface{}) { | ||
slog.Debug(fmt.Sprintf(s, i)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package publisher | ||
|
||
import "context" | ||
|
||
type PublisherType string | ||
|
||
const ( | ||
PublisherTypeRabbitMQ PublisherType = "rabbitmq" | ||
) | ||
|
||
type PublisherConfig struct { | ||
Type PublisherType | ||
Topic string | ||
Addr string | ||
} | ||
|
||
type IPublisherConfigFactory interface { | ||
Config() PublisherConfig | ||
} | ||
|
||
type Event struct { | ||
Data any | ||
} | ||
|
||
type IEventPublisher interface { | ||
Publish(context.Context, string, *Event) error | ||
Close() error | ||
} | ||
|
||
type TopicFeedbackEvent struct { | ||
Pattern string `json:"pattern"` | ||
Data TopicFeedbackEventData `json:"data"` | ||
} | ||
|
||
type TopicFeedbackEventData struct { | ||
ID string `json:"id"` | ||
TopicID int64 `json:"t_id"` | ||
SupportTelegramID int64 `json:"s_tid"` | ||
SupportTelegramUsername string `json:"s_tun"` | ||
SupportUserID string `json:"s_uid"` | ||
CreatorTelegramID int64 `json:"u_tid"` | ||
CreatorTelegramUsername string `json:"u_tun"` | ||
Rating uint8 `json:"rating"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package publisher | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/wagslane/go-rabbitmq" | ||
) | ||
|
||
var _ IEventPublisher = (*RabbitPublisher)(nil) | ||
|
||
// RabbitPublisher represent event publisher for RabbitMQ. | ||
type RabbitPublisher struct { | ||
pt string `wire:"-"` | ||
conn *rabbitmq.Conn `wire:"-"` | ||
publisher *rabbitmq.Publisher `wire:"-"` | ||
} | ||
|
||
// NewRabbitPublisher create new RabbitPublisher instance. | ||
func NewRabbitPublisher(cfg IPublisherConfigFactory) (*RabbitPublisher, error) { | ||
conn, err := rabbitmq.NewConn(cfg.Config().Addr) | ||
if err != nil { | ||
return nil, fmt.Errorf("new conn: %w", err) | ||
} | ||
|
||
publisher, err := rabbitmq.NewPublisher( | ||
conn, | ||
rabbitmq.WithPublisherOptionsLogger(&PublisherLogger{}), | ||
rabbitmq.WithPublisherOptionsLogging, | ||
rabbitmq.WithPublisherOptionsExchangeName(cfg.Config().Topic), | ||
rabbitmq.WithPublisherOptionsExchangeDeclare, | ||
rabbitmq.WithPublisherOptionsExchangeKind("topic"), | ||
rabbitmq.WithPublisherOptionsExchangeDurable, | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("publisher: %w", err) | ||
} | ||
|
||
return &RabbitPublisher{ | ||
cfg.Config().Topic, | ||
conn, | ||
publisher, | ||
}, nil | ||
} | ||
|
||
// Publish send events, implements eventPublisher. | ||
func (p *RabbitPublisher) Publish(ctx context.Context, topic string, event *Event) error { | ||
body, err := json.Marshal(event.Data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return p.publisher.PublishWithContext( | ||
ctx, | ||
body, | ||
[]string{""}, | ||
rabbitmq.WithPublishOptionsContentEncoding("utf-8"), | ||
rabbitmq.WithPublishOptionsExchange(topic), | ||
rabbitmq.WithPublishOptionsContentType("application/json"), | ||
rabbitmq.WithPublishOptionsExchange(p.pt), | ||
) | ||
} | ||
|
||
// Close represent finalization for RabbitMQ publisher. | ||
func (p *RabbitPublisher) Close() error { | ||
if err := p.conn.Close(); err != nil { | ||
return fmt.Errorf("connection close: %w", err) | ||
} | ||
|
||
p.publisher.Close() | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
package adapters | ||
|
||
import ( | ||
"github.com/WildEgor/e-shop-support-bot/internal/adapters/publisher" | ||
"github.com/WildEgor/e-shop-support-bot/internal/adapters/telegram" | ||
"github.com/google/wire" | ||
) | ||
|
||
var AdaptersSet = wire.NewSet( | ||
telegram.NewTelegramBotAdapter, | ||
telegram.NewTelegramListener, | ||
publisher.NewRabbitPublisher, | ||
wire.Bind(new(publisher.IEventPublisher), new(*publisher.RabbitPublisher)), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.