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.
75 lines
1.7 KiB
75 lines
1.7 KiB
package authPostman
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"log"
|
|
"net"
|
|
"strconv"
|
|
|
|
"git.slaventius.ru/test3k/authPostman/internal/config"
|
|
kafka "git.slaventius.ru/test3k/authPostman/internal/transport/kafka"
|
|
smtp "git.slaventius.ru/test3k/authPostman/internal/transport/smtp"
|
|
)
|
|
|
|
type msg struct {
|
|
Code string
|
|
Email string
|
|
}
|
|
|
|
type AuthPostmanServer struct {
|
|
ctx context.Context
|
|
kafkaReader *kafka.KafkaReader
|
|
config *config.Config
|
|
}
|
|
|
|
func NewServer(ctx context.Context, config *config.Config, topic string) *AuthPostmanServer {
|
|
address := []string{net.JoinHostPort(config.Kafka.Host, strconv.Itoa(config.Kafka.Port))}
|
|
|
|
return &AuthPostmanServer{
|
|
ctx: ctx,
|
|
kafkaReader: kafka.NewReader(ctx, config, topic, address...),
|
|
config: config,
|
|
}
|
|
}
|
|
|
|
func (s *AuthPostmanServer) GracefulStop() error {
|
|
return s.kafkaReader.Close()
|
|
}
|
|
|
|
func (s *AuthPostmanServer) ReadMessage(offset int64) error {
|
|
// ...
|
|
// s.kafkaReader.SetOffset(offset)
|
|
|
|
//
|
|
for {
|
|
m, err := s.kafkaReader.ReadMessage()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Декодируем сообщение
|
|
amsg := msg{}
|
|
erk := json.Unmarshal(m.Value, &amsg)
|
|
if erk != nil {
|
|
return erk
|
|
}
|
|
|
|
//
|
|
log.Printf("send code %s to %s ...", amsg.Code, amsg.Email)
|
|
// log.Printf("message at offset %d: %s = %s\n", m.Offset, string(m.Key), string(m.Value))
|
|
|
|
//
|
|
message := smtp.NewMessage("Confirmation code", amsg.Code)
|
|
message.AppendRecipient(amsg.Email)
|
|
|
|
//
|
|
smtpSender := smtp.NewService(s.config.Smtp.Host, s.config.Smtp.Port, s.config.Smtp.Sender, s.config.Smtp.Password)
|
|
ers := smtpSender.Send(message)
|
|
if ers != nil {
|
|
log.Print(ers)
|
|
}
|
|
|
|
log.Printf("send code %s to %s completed", amsg.Code, amsg.Email)
|
|
}
|
|
}
|
|
|