You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.0 KiB

2 years ago
package authPostman
2 years ago
import (
"context"
2 years ago
"encoding/json"
2 years ago
"fmt"
2 years ago
"net"
"strconv"
2 years ago
"time"
2 years ago
2 years ago
"git.slaventius.ru/test3k/authPostman/internal/config"
kafka "git.slaventius.ru/test3k/authPostman/internal/transport/kafka"
2 years ago
// telegram "git.slaventius.ru/test3k/authPostman/internal/transport/telegram"
smtp "git.slaventius.ru/test3k/authPostman/internal/transport/smtp"
2 years ago
api "git.slaventius.ru/test3k/umate/pkg/kafka"
2 years ago
logger "git.slaventius.ru/test3k/umate/pkg/logger"
2 years ago
)
2 years ago
type AuthPostmanServer struct {
2 years ago
ctx context.Context
2 years ago
kafkaReader *kafka.KafkaReader
2 years ago
logger *logger.Logger
2 years ago
config *config.Config
2 years ago
msgs map[int64]time.Time
age time.Duration
2 years ago
}
2 years ago
func NewServer(ctx context.Context, config *config.Config) *AuthPostmanServer {
2 years ago
logger := logger.NewLogger("test3k:authPostmanService", config.Sentry.DSN)
2 years ago
address := []string{net.JoinHostPort(config.Kafka.Host, strconv.Itoa(config.Kafka.Port))}
2 years ago
return &AuthPostmanServer{
2 years ago
ctx: ctx,
2 years ago
kafkaReader: kafka.NewReader(ctx, config, api.TopicRegistrations, address...),
2 years ago
logger: logger,
2 years ago
config: config,
2 years ago
msgs: make(map[int64]time.Time),
age: time.Minute * 5,
2 years ago
}
2 years ago
}
func (s *AuthPostmanServer) GracefulStop() error {
2 years ago
return s.kafkaReader.Close()
}
func (s *AuthPostmanServer) ReadMessage(offset int64) error {
// ...
2 years ago
// s.kafkaReader.SetOffset(offset)
2 years ago
//
for {
// m, err := s.kafkaReader.ReadMessage()
m, err := s.kafkaReader.FetchMessage()
2 years ago
if err != nil {
s.logger.Error(err)
continue
2 years ago
}
2 years ago
// Декодируем сообщение
2 years ago
msg := api.MessageRegistration{}
erk := json.Unmarshal(m.Value, &msg)
2 years ago
if erk != nil {
s.logger.Error(erk)
continue
2 years ago
}
2 years ago
// Проверим пришедшее сообщение, не является ли оно повтором
if _, ok := s.msgs[msg.ID]; ok {
s.logger.Warnf("the message #%d is a duplicate", msg.ID)
2 years ago
continue
} else {
2 years ago
s.msgs[msg.ID] = time.Now()
2 years ago
}
2 years ago
//
2 years ago
s.logger.Printf("send code %s to %s (message #%d) ...", msg.Code, msg.Email, msg.ID)
2 years ago
2 years ago
//
2 years ago
text := fmt.Sprintf("Confirmation code %v", msg.Code)
2 years ago
// postman := telegram.NewService(s.config.Telegram.ChatToken)
// message := postman.NewMessage(s.config.Telegram.ChatID, text)
// ers := postman.SendMessage(message)
2 years ago
postman := smtp.NewService(s.config.Smtp.Host, s.config.Smtp.Port, s.config.Smtp.Sender, s.config.Smtp.Password)
2 years ago
message := smtp.NewMessage("Confirmation code", text)
2 years ago
message.AppendRecipient(msg.Email)
2 years ago
ers := postman.Send(message)
2 years ago
if ers != nil {
s.logger.Error(ers)
continue
2 years ago
} else {
2 years ago
s.logger.Printf("send code %s to %s (message #%d) completed", msg.Code, msg.Email, msg.ID)
2 years ago
}
// Подтвердим сообщение после успешной обработки
erf := s.kafkaReader.CommitMessage(m)
if erf != nil {
s.logger.Error(erf)
}
2 years ago
2 years ago
// Удалим идентификаторы устаревших сообщений
2 years ago
no := time.Now()
for k, v := range s.msgs {
if s.age < no.Sub(v) {
delete(s.msgs, k)
}
}
2 years ago
}
2 years ago
}