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.

80 lines
2.1 KiB

3 years ago
package authPostman
3 years ago
import (
"context"
3 years ago
"encoding/json"
3 years ago
"fmt"
3 years ago
"net"
"strconv"
3 years ago
"git.slaventius.ru/test3k/authPostman/internal/config"
kafka "git.slaventius.ru/test3k/authPostman/internal/transport/kafka"
3 years ago
// telegram "git.slaventius.ru/test3k/authPostman/internal/transport/telegram"
smtp "git.slaventius.ru/test3k/authPostman/internal/transport/smtp"
3 years ago
api "git.slaventius.ru/test3k/umate/pkg/kafka"
3 years ago
logger "git.slaventius.ru/test3k/umate/pkg/logger"
3 years ago
)
3 years ago
type AuthPostmanServer struct {
3 years ago
ctx context.Context
3 years ago
kafkaReader *kafka.KafkaReader
3 years ago
logger *logger.Logger
3 years ago
config *config.Config
3 years ago
}
3 years ago
func NewServer(ctx context.Context, config *config.Config) *AuthPostmanServer {
3 years ago
logger := logger.NewLogger("test3k:authPostmanService", config.Sentry.DSN)
3 years ago
address := []string{net.JoinHostPort(config.Kafka.Host, strconv.Itoa(config.Kafka.Port))}
3 years ago
return &AuthPostmanServer{
3 years ago
ctx: ctx,
3 years ago
kafkaReader: kafka.NewReader(ctx, config, api.TopicRegistrations, address...),
3 years ago
logger: logger,
3 years ago
config: config,
3 years ago
}
3 years ago
}
func (s *AuthPostmanServer) GracefulStop() error {
3 years ago
return s.kafkaReader.Close()
}
func (s *AuthPostmanServer) ReadMessage(offset int64) error {
// ...
3 years ago
// s.kafkaReader.SetOffset(offset)
3 years ago
//
for {
3 years ago
m, err := s.kafkaReader.ReadMessage()
3 years ago
if err != nil {
return err
}
3 years ago
// Декодируем сообщение
3 years ago
msg := api.MessageRegistration{}
erk := json.Unmarshal(m.Value, &msg)
3 years ago
if erk != nil {
return erk
}
3 years ago
//
3 years ago
s.logger.Printf("send code %s to %s ...", msg.Code, msg.Email)
3 years ago
3 years ago
//
3 years ago
text := fmt.Sprintf("Confirmation code %v", msg.Code)
3 years ago
// postman := telegram.NewService(s.config.Telegram.ChatToken)
// message := postman.NewMessage(s.config.Telegram.ChatID, text)
// ers := postman.SendMessage(message)
3 years ago
postman := smtp.NewService(s.config.Smtp.Host, s.config.Smtp.Port, s.config.Smtp.Sender, s.config.Smtp.Password)
3 years ago
message := smtp.NewMessage("Confirmation code", text)
3 years ago
message.AppendRecipient(msg.Email)
3 years ago
ers := postman.Send(message)
3 years ago
if ers != nil {
3 years ago
s.logger.Print(ers)
3 years ago
} else {
3 years ago
s.logger.Printf("send code %s to %s completed", msg.Code, msg.Email)
3 years ago
}
3 years ago
}
3 years ago
}