From c26d9140b228d4f833581567ffeffc6d39823c7c Mon Sep 17 00:00:00 2001 From: slaventius Date: Wed, 1 Feb 2023 11:09:59 +0300 Subject: [PATCH] * --- .gitignore | 1 + cmd/main.go | 3 +- internal/transport/grpc/grpc.go | 142 ++++++++++++++++++++++++-------- 3 files changed, 111 insertions(+), 35 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b71e2aa --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +authDBService \ No newline at end of file diff --git a/cmd/main.go b/cmd/main.go index b510e38..5052b4c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -13,7 +13,7 @@ import ( func main() { s := grpc.NewServer() - srv := server.NewGRPCServer() + srv := server.NewServer() config := config.NewConfig() // @@ -27,6 +27,7 @@ func main() { } // + fmt.Printf("authDBService up in %d port\n", config.App.Port) eru := s.Serve(lis) if eru != nil { log.Fatal(eru) diff --git a/internal/transport/grpc/grpc.go b/internal/transport/grpc/grpc.go index d7972e6..4d6d0f4 100644 --- a/internal/transport/grpc/grpc.go +++ b/internal/transport/grpc/grpc.go @@ -2,64 +2,138 @@ package grpc import ( "context" - "fmt" - "log" + "errors" + "strconv" "sync" api "test3k/authDB/pkg/api" - - kafka "github.com/segmentio/kafka-go" + "time" + // kafka "github.com/segmentio/kafka-go" ) type user struct { - Code string - Login string - Password string + ID int32 + Code string + Login string + Password string + Email string + Confirmed bool } -type GRPCServer struct { +type gRPCServer struct { mu sync.Mutex - users map[string]user + users map[string]*user api.UnimplementedAuthDBServer + id int32 } -func NewGRPCServer() *GRPCServer { - return &GRPCServer{ +func NewServer() *gRPCServer { + return &gRPCServer{ mu: sync.Mutex{}, - users: make(map[string]user), + users: make(map[string]*user), + id: 0, } } -func (s *GRPCServer) Login(context.Context, *api.LoginRequest) (*api.LoginResponse, error) { - return nil, nil +func (s *gRPCServer) Login(ctx context.Context, req *api.LoginRequest) (*api.LoginResponse, error) { + s.mu.Lock() + defer s.mu.Unlock() + + // + user, ok := s.users[req.GetLogin()] + if !ok { + return nil, errors.New("login unknown") + } + + // + if !user.Confirmed { + return nil, errors.New("login unconfirmed") + } + + // + if user.Password != req.Password { + return nil, errors.New("password incorrect") + } + + return &api.LoginResponse{ + ID: user.ID, + }, nil } -func (s *GRPCServer) Registration(context.Context, *api.RegistrationRequest) (*api.RegistrationResponse, error) { - ctx := context.Background() - consumer := kafka.NewReader(kafka.ReaderConfig{ - Brokers: []string{"localhost:9092"}, - Topic: "topic-A", - Partition: 0, - MinBytes: 10e3, // 10KB - MaxBytes: 10e6, // 10MB - }) - defer consumer.Close() +func (s *gRPCServer) Registration(ctx context.Context, req *api.RegistrationRequest) (*api.RegistrationResponse, error) { + s.mu.Lock() + defer s.mu.Unlock() // - // consumer.SetOffset(42) + _, ok := s.users[req.GetLogin()] + if ok { + return nil, errors.New("login already registered") + } // - for { - m, err := consumer.ReadMessage(ctx) - if err != nil { - log.Fatal(err) - } + s.id = s.id + 1 - fmt.Printf("message at offset %d: %s = %s\n", m.Offset, string(m.Key), string(m.Value)) + // + id := time.Now().Nanosecond() + code := strconv.Itoa(id / 2) + + // + s.users[req.Login] = &user{ + ID: s.id, + Code: code, + Login: req.Login, + Password: strconv.Itoa(id), + Email: req.Email, + Confirmed: false, } - return nil, nil + // // + // consumer := kafka.NewReader(kafka.ReaderConfig{ + // Brokers: []string{"localhost:9092"}, + // Topic: "topic-A", + // Partition: 0, + // MinBytes: 10e3, // 10KB + // MaxBytes: 10e6, // 10MB + // }) + // defer consumer.Close() + + // // + // // consumer.SetOffset(42) + + // // + // for { + // m, err := consumer.ReadMessage(ctx) + // if err != nil { + // log.Fatal(err) + // } + + // fmt.Printf("message at offset %d: %s = %s\n", m.Offset, string(m.Key), string(m.Value)) + // } + + return &api.RegistrationResponse{ + Code: code, + Email: req.Email, + }, nil } -func (s *GRPCServer) Confirmation(context.Context, *api.ConfirmationRequest) (*api.ConfirmationResponse, error) { - return nil, nil +func (s *gRPCServer) Confirmation(ctx context.Context, req *api.ConfirmationRequest) (*api.ConfirmationResponse, error) { + s.mu.Lock() + defer s.mu.Unlock() + + // + for _, x := range s.users { + if x.Code == req.GetCode() { + if x.Confirmed { + return nil, errors.New("already confirmed") + } + + // + x.Confirmed = true + + return &api.ConfirmationResponse{ + ID: x.ID, + }, nil + } + } + + return nil, errors.New("code unknown") }