mirror of
https://github.com/3ybactuk/marketplace-go-service-project.git
synced 2025-10-30 14:03:45 +03:00
[hw-6] add notifier service, kafka
This commit is contained in:
51
notifier/internal/infra/config/config.go
Normal file
51
notifier/internal/infra/config/config.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Service struct {
|
||||
LogLevel string `yaml:"log_level"`
|
||||
} `yaml:"service"`
|
||||
|
||||
Kafka struct {
|
||||
Host string `yaml:"host"`
|
||||
Port string `yaml:"port"`
|
||||
OrderTopic string `yaml:"order_topic"`
|
||||
ConsumerGroupID string `yaml:"consumer_group_id"`
|
||||
Brokers string `yaml:"brokers"`
|
||||
} `yaml:"kafka"`
|
||||
}
|
||||
|
||||
func LoadConfig(filename string) (*Config, error) {
|
||||
workDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfgRoot := filepath.Join(workDir, "configs")
|
||||
absCfgRoot, _ := filepath.Abs(cfgRoot)
|
||||
|
||||
filePath := filepath.Join(absCfgRoot, filepath.Clean(filename))
|
||||
if !strings.HasPrefix(filePath, absCfgRoot) {
|
||||
return nil, fmt.Errorf("invalid path")
|
||||
}
|
||||
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
config := &Config{}
|
||||
if err := yaml.NewDecoder(f).Decode(config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
48
notifier/internal/infra/messaging/kafka/consumer.go
Normal file
48
notifier/internal/infra/messaging/kafka/consumer.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package kafka
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/IBM/sarama"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type StatusConsumer struct {
|
||||
group sarama.ConsumerGroup
|
||||
topic string
|
||||
}
|
||||
|
||||
func NewStatusConsumer(topic string, consumerGroup sarama.ConsumerGroup) (*StatusConsumer, error) {
|
||||
return &StatusConsumer{
|
||||
group: consumerGroup,
|
||||
topic: topic,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *StatusConsumer) FetchEvents(ctx context.Context) error {
|
||||
h := &statusHandler{}
|
||||
|
||||
return c.group.Consume(ctx, []string{c.topic}, h)
|
||||
}
|
||||
|
||||
type statusHandler struct{}
|
||||
|
||||
func (h *statusHandler) Setup(sess sarama.ConsumerGroupSession) error {
|
||||
log.Info().Msgf("[notifier] assigned %v", sess.Claims())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *statusHandler) Cleanup(_ sarama.ConsumerGroupSession) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *statusHandler) ConsumeClaim(sess sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
|
||||
for msg := range claim.Messages() {
|
||||
log.Info().Msgf("[order=%s] p=%d off=%d %s\n", string(msg.Key), msg.Partition, msg.Offset, string(msg.Value))
|
||||
|
||||
sess.MarkMessage(msg, "")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user