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.
89 lines
2.1 KiB
89 lines
2.1 KiB
package authPostman
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"strconv"
|
|
|
|
"git.slaventius.ru/test3k/authPostman/internal/config"
|
|
kafka "git.slaventius.ru/test3k/authPostman/internal/transport/kafka"
|
|
"git.slaventius.ru/test3k/authPostman/internal/transport/telegram"
|
|
)
|
|
|
|
const (
|
|
chatID int64 = 150697696
|
|
token string = "5944264850:AAHaikh8r8NYciybnOpqEvFQpSYgr6JHDZc"
|
|
topicRegistrations string = "registrations" // Топик для регистраций
|
|
)
|
|
|
|
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) *AuthPostmanServer {
|
|
address := []string{net.JoinHostPort(config.Kafka.Host, strconv.Itoa(config.Kafka.Port))}
|
|
|
|
return &AuthPostmanServer{
|
|
ctx: ctx,
|
|
kafkaReader: kafka.NewReader(ctx, config, topicRegistrations, 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)
|
|
|
|
//
|
|
postman := telegram.NewService(token)
|
|
message := postman.NewMessage(chatID, fmt.Sprintf("Confirmation code %v", amsg.Code))
|
|
ers := postman.SendMessage(message)
|
|
if ers != nil {
|
|
log.Print(ers)
|
|
} else {
|
|
log.Printf("send code %s to %s completed", amsg.Code, amsg.Email)
|
|
}
|
|
|
|
// //
|
|
// 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)
|
|
// }
|
|
}
|
|
}
|
|
|