From 1033eaf6eec9c34c4bd5067969b3690474477031 Mon Sep 17 00:00:00 2001 From: slaventius Date: Wed, 15 Mar 2023 11:14:21 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=83=D0=BD=D0=B8=D0=BA=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=B8=D0=B4=D0=B5=D0=BD=D1=82=D0=B8=D1=84=D0=B8=D0=BA=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=20=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/authDB.go | 29 +++++++++++++++++++++++------ internal/customer/customer.go | 11 +++++------ internal/customer/repository.go | 7 ++----- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/internal/authDB.go b/internal/authDB.go index 3924e17..27f8858 100644 --- a/internal/authDB.go +++ b/internal/authDB.go @@ -32,6 +32,7 @@ type AuthDBServer struct { repo *repo.CustomerRepository api.UnimplementedAuthDBServer api.UnimplementedHealthServer + counter int64 } func NewServer(ctx context.Context, config *config.Config) *AuthDBServer { @@ -54,15 +55,30 @@ func NewServer(ctx context.Context, config *config.Config) *AuthDBServer { kafkaWriter: kafka.NewWriter(ctx, logger, apiKafka.TopicRegistrations, net.JoinHostPort(config.Kafka.Host, strconv.Itoa(config.Kafka.Port))), config: config, logger: logger, + + // С каждым стартом сервиса значение счетчика повторяться не будет + counter: time.Now().UnixNano(), } } -func (r *AuthDBServer) getMD5Hash(text string) string { +func (s *AuthDBServer) getMD5Hash(text string) string { hash := md5.Sum([]byte(text)) return hex.EncodeToString(hash[:]) } +func (s *AuthDBServer) genMessage(customer repo.Customer) apiKafka.MessageRegistration { + // Увеличим счетчик сообщений на случай повторной отправки пакета продьюсером + // На стороне получателя уникальность этого значение будет контролироваться + s.counter = s.counter + 1 + + return apiKafka.MessageRegistration{ + ID: s.counter, + Code: customer.Code, + Email: customer.Email, + } +} + func (s *AuthDBServer) GracefulStop() error { return s.kafkaWriter.Close() } @@ -122,13 +138,14 @@ func (s *AuthDBServer) Registration(ctx context.Context, req *api.RegistrationRe } // TODO - value, era := json.Marshal(customer.MessageRegistration) + msg := s.genMessage(customer) + value, era := json.Marshal(msg) if era != nil { return nil, era } // - s.logger.Printf("publication code %s to %s ...", customer.MessageRegistration.Code, customer.MessageRegistration.Email) + s.logger.Printf("publication code %s to %s (message #%d) ...", msg.Code, msg.Email, msg.ID) // Отправим уведомление о необходимости подтверждения err := s.kafkaWriter.WriteMessage([]byte(customer.Login), value) @@ -139,11 +156,11 @@ func (s *AuthDBServer) Registration(ctx context.Context, req *api.RegistrationRe } // - s.logger.Printf("publication code %s to %s completed", customer.MessageRegistration.Code, customer.MessageRegistration.Email) + s.logger.Printf("publication code %s to %s (message #%d) completed", msg.Code, msg.Email, msg.ID) return &api.RegistrationResponse{ - Code: customer.MessageRegistration.Code, - Email: customer.MessageRegistration.Email, + Code: msg.Code, + Email: msg.Email, }, nil } diff --git a/internal/customer/customer.go b/internal/customer/customer.go index 6c1e787..99718ca 100644 --- a/internal/customer/customer.go +++ b/internal/customer/customer.go @@ -3,17 +3,16 @@ package customer import ( "strconv" "time" - - apiKafka "git.slaventius.ru/test3k/umate/pkg/kafka" ) // Покупатель type Customer struct { SimpleRow - Login string `json:"login"` - Password string `json:"password"` - Confirmed bool `json:"confirmed"` - apiKafka.MessageRegistration + Code string `json:"code"` + Email string `json:"email"` + Login string `json:"login"` + Password string `json:"password"` + Confirmed bool `json:"confirmed"` TimeReregistration time.Time `json:"reregistration_at"` // Время начиная с которого допустима повторная регистрация } diff --git a/internal/customer/repository.go b/internal/customer/repository.go index 444a977..2f4eca2 100644 --- a/internal/customer/repository.go +++ b/internal/customer/repository.go @@ -6,7 +6,6 @@ import ( "time" arango_db "git.slaventius.ru/test3k/authDB/internal/transport/arango" - "git.slaventius.ru/test3k/umate/pkg/kafka" "github.com/arangodb/go-driver" ) @@ -38,13 +37,11 @@ func NewCustomerRepository(ctx context.Context, database *arango_db.DataBase) *C func (r *CustomerRepository) NewCustomer(login string, password string, email string) Customer { return Customer{ SimpleRow: *NewSimpleRow(r.ctx, r.database, r.collection), + Code: time.Now().Format(StampTemplate), + Email: email, Login: login, Password: password, TimeReregistration: time.Now(), - MessageRegistration: kafka.MessageRegistration{ - Email: email, - Code: time.Now().Format(StampTemplate), - }, } }