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.

82 lines
1.8 KiB

2 years ago
package postman
import (
"context"
2 years ago
"encoding/json"
2 years ago
"log"
"net"
"strconv"
2 years ago
"test3k/authPostman/internal/config"
2 years ago
2 years ago
// smtp "test3k/authPostman/internal/smtp"
2 years ago
2 years ago
"github.com/segmentio/kafka-go"
2 years ago
)
2 years ago
type msg struct {
Code string
Email string
}
2 years ago
type AuthPostmanServer struct {
2 years ago
ctx context.Context
2 years ago
config *config.Config
2 years ago
kafkaReader *kafka.Reader
2 years ago
}
2 years ago
func NewServer(ctx context.Context, config *config.Config, topic string) *AuthPostmanServer {
return &AuthPostmanServer{
2 years ago
ctx: ctx,
config: config,
2 years ago
kafkaReader: kafka.NewReader(kafka.ReaderConfig{
2 years ago
Topic: topic,
Brokers: []string{net.JoinHostPort(config.Kafka.Host, strconv.Itoa(config.Kafka.Port))},
// GroupID: fmt.Sprintf("consumer-group-%d", config.Kafka.Partition),
Partition: config.Kafka.Partition,
2 years ago
MinBytes: 10e3, // 10KB
MaxBytes: 10e6, // 10MB
}),
}
2 years ago
}
func (s *AuthPostmanServer) GracefulStop() error {
2 years ago
return s.kafkaReader.Close()
}
func (s *AuthPostmanServer) ReadMessage(offset int64) error {
// ...
s.kafkaReader.SetOffset(offset)
//
for {
m, err := s.kafkaReader.ReadMessage(s.ctx)
if err != nil {
return err
}
2 years ago
// Декодируем сообщение
amsg := msg{}
erk := json.Unmarshal(m.Value, &amsg)
if erk != nil {
return erk
}
2 years ago
//
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))
2 years ago
// //
// message := smtp.NewMessage("Confirmation code", amsg.Code)
// message.AppendRecipient(amsg.Email)
2 years ago
2 years ago
// //
// 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)
// }
2 years ago
2 years ago
log.Printf("send code %s to %s completed", amsg.Code, amsg.Email)
2 years ago
}
2 years ago
}