From c027c5b851590063b5e523f998d428dbccb9578c Mon Sep 17 00:00:00 2001 From: xeeetu Date: Fri, 16 Aug 2024 15:17:48 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D1=81=D0=B5=D1=80=D0=B2=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/chat_server/main.go | 74 ++++++- go.sum | 9 + pkg/chat_v1/chat.pb.go | 378 ++++++++++++++++++++++++++++++++++++ pkg/chat_v1/chat_grpc.pb.go | 178 +++++++++++++++++ 4 files changed, 637 insertions(+), 2 deletions(-) create mode 100644 pkg/chat_v1/chat.pb.go create mode 100644 pkg/chat_v1/chat_grpc.pb.go diff --git a/cmd/chat_server/main.go b/cmd/chat_server/main.go index 91e7378..479c000 100644 --- a/cmd/chat_server/main.go +++ b/cmd/chat_server/main.go @@ -1,7 +1,77 @@ package main -import "fmt" +import ( + "context" + "fmt" + "log" + "net" + + "github.com/brianvoe/gofakeit" + desc "github.com/xeeetu/chat-server/pkg/chat_v1" + "google.golang.org/grpc" + "google.golang.org/grpc/reflection" + "google.golang.org/protobuf/types/known/emptypb" +) + +const ( + address = "localhost:50051" +) + +type server struct { + desc.UnimplementedChatV1Server +} + +func (s *server) Create(ctx context.Context, in *desc.CreateRequest) (*desc.CreateResponse, error) { + select { + case <-ctx.Done(): + // Если контекст отменён, возвращаем ошибку + return nil, ctx.Err() + default: + // Продолжаем выполнение + } + + fmt.Printf("Create chat with users: %v", in.Usernames) + return &desc.CreateResponse{Id: gofakeit.Int64()}, nil +} + +func (s *server) Delete(ctx context.Context, in *desc.DeleteRequest) (*emptypb.Empty, error) { + select { + case <-ctx.Done(): + // Если контекст отменён, возвращаем ошибку + return nil, ctx.Err() + default: + // Продолжаем выполнение + } + + fmt.Printf("Delete chat with id: %d", in.Id) + return &emptypb.Empty{}, nil +} + +func (s *server) SendMessage(ctx context.Context, in *desc.SendMessageRequest) (*emptypb.Empty, error) { + select { + case <-ctx.Done(): + // Если контекст отменён, возвращаем ошибку + return nil, ctx.Err() + default: + // Продолжаем выполнение + } + + fmt.Printf("Send message: %v", in) + return &emptypb.Empty{}, nil +} func main() { - fmt.Println("Hello World") + lis, err := net.Listen("tcp", address) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + + s := grpc.NewServer() + reflection.Register(s) + + desc.RegisterChatV1Server(s, &server{}) + + if err = s.Serve(lis); err != nil { + log.Fatalf("failed to serve: %v", err) + } } diff --git a/go.sum b/go.sum index 299cfa4..9fe329b 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,16 @@ +github.com/brianvoe/gofakeit v3.18.0+incompatible h1:wDOmHc9DLG4nRjUVVaxA+CEglKOW72Y5+4WNxUIkjM8= github.com/brianvoe/gofakeit v3.18.0+incompatible/go.mod h1:kfwdRA90vvNhPutZWfH7WPaDzUjz+CZFqG+rPkOjGOc= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= +golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 h1:Zy9XzmMEflZ/MAaA7vNcoebnRAld7FsPW1EeBB7V0m8= google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0= +google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc= google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= diff --git a/pkg/chat_v1/chat.pb.go b/pkg/chat_v1/chat.pb.go new file mode 100644 index 0000000..0aaa4e0 --- /dev/null +++ b/pkg/chat_v1/chat.pb.go @@ -0,0 +1,378 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.1 +// protoc v3.21.12 +// source: chat.proto + +package chat_v1 + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + emptypb "google.golang.org/protobuf/types/known/emptypb" + timestamppb "google.golang.org/protobuf/types/known/timestamppb" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type CreateRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Usernames []string `protobuf:"bytes,1,rep,name=usernames,proto3" json:"usernames,omitempty"` // имя пользователей в чате +} + +func (x *CreateRequest) Reset() { + *x = CreateRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_chat_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *CreateRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*CreateRequest) ProtoMessage() {} + +func (x *CreateRequest) ProtoReflect() protoreflect.Message { + mi := &file_chat_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use CreateRequest.ProtoReflect.Descriptor instead. +func (*CreateRequest) Descriptor() ([]byte, []int) { + return file_chat_proto_rawDescGZIP(), []int{0} +} + +func (x *CreateRequest) GetUsernames() []string { + if x != nil { + return x.Usernames + } + return nil +} + +type CreateResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` // id создаваемого чата +} + +func (x *CreateResponse) Reset() { + *x = CreateResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_chat_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *CreateResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*CreateResponse) ProtoMessage() {} + +func (x *CreateResponse) ProtoReflect() protoreflect.Message { + mi := &file_chat_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use CreateResponse.ProtoReflect.Descriptor instead. +func (*CreateResponse) Descriptor() ([]byte, []int) { + return file_chat_proto_rawDescGZIP(), []int{1} +} + +func (x *CreateResponse) GetId() int64 { + if x != nil { + return x.Id + } + return 0 +} + +type DeleteRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` // id удаляемого чата +} + +func (x *DeleteRequest) Reset() { + *x = DeleteRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_chat_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *DeleteRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*DeleteRequest) ProtoMessage() {} + +func (x *DeleteRequest) ProtoReflect() protoreflect.Message { + mi := &file_chat_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use DeleteRequest.ProtoReflect.Descriptor instead. +func (*DeleteRequest) Descriptor() ([]byte, []int) { + return file_chat_proto_rawDescGZIP(), []int{2} +} + +func (x *DeleteRequest) GetId() int64 { + if x != nil { + return x.Id + } + return 0 +} + +type SendMessageRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + From string `protobuf:"bytes,1,opt,name=from,proto3" json:"from,omitempty"` // от кого отправляется сообщение + Text string `protobuf:"bytes,2,opt,name=text,proto3" json:"text,omitempty"` // содержание сообщения + Timestamp *timestamppb.Timestamp `protobuf:"bytes,3,opt,name=timestamp,proto3" json:"timestamp,omitempty"` // время отправки сообщения +} + +func (x *SendMessageRequest) Reset() { + *x = SendMessageRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_chat_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *SendMessageRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SendMessageRequest) ProtoMessage() {} + +func (x *SendMessageRequest) ProtoReflect() protoreflect.Message { + mi := &file_chat_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use SendMessageRequest.ProtoReflect.Descriptor instead. +func (*SendMessageRequest) Descriptor() ([]byte, []int) { + return file_chat_proto_rawDescGZIP(), []int{3} +} + +func (x *SendMessageRequest) GetFrom() string { + if x != nil { + return x.From + } + return "" +} + +func (x *SendMessageRequest) GetText() string { + if x != nil { + return x.Text + } + return "" +} + +func (x *SendMessageRequest) GetTimestamp() *timestamppb.Timestamp { + if x != nil { + return x.Timestamp + } + return nil +} + +var File_chat_proto protoreflect.FileDescriptor + +var file_chat_proto_rawDesc = []byte{ + 0x0a, 0x0a, 0x63, 0x68, 0x61, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x63, 0x68, + 0x61, 0x74, 0x1a, 0x1b, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x62, 0x75, 0x66, 0x2f, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, + 0x1f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, + 0x2f, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x22, 0x2d, 0x0a, 0x0d, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x12, 0x1c, 0x0a, 0x09, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x18, 0x01, + 0x20, 0x03, 0x28, 0x09, 0x52, 0x09, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x22, + 0x20, 0x0a, 0x0e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, + 0x64, 0x22, 0x1f, 0x0a, 0x0d, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, + 0x69, 0x64, 0x22, 0x76, 0x0a, 0x12, 0x53, 0x65, 0x6e, 0x64, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, + 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x66, 0x72, 0x6f, 0x6d, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x12, 0x12, 0x0a, 0x04, + 0x74, 0x65, 0x78, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, + 0x12, 0x38, 0x0a, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x18, 0x03, 0x20, + 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, + 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x32, 0xb5, 0x01, 0x0a, 0x06, 0x43, + 0x68, 0x61, 0x74, 0x56, 0x31, 0x12, 0x33, 0x0a, 0x06, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x12, + 0x13, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x2e, 0x43, 0x72, 0x65, 0x61, + 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x35, 0x0a, 0x06, 0x44, 0x65, + 0x6c, 0x65, 0x74, 0x65, 0x12, 0x13, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x2e, 0x44, 0x65, 0x6c, 0x65, + 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, + 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, + 0x79, 0x12, 0x3f, 0x0a, 0x0b, 0x53, 0x65, 0x6e, 0x64, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, + 0x12, 0x18, 0x2e, 0x63, 0x68, 0x61, 0x74, 0x2e, 0x53, 0x65, 0x6e, 0x64, 0x4d, 0x65, 0x73, 0x73, + 0x61, 0x67, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, + 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, + 0x74, 0x79, 0x42, 0x33, 0x5a, 0x31, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, + 0x2f, 0x78, 0x65, 0x65, 0x65, 0x74, 0x75, 0x2f, 0x63, 0x68, 0x61, 0x74, 0x2d, 0x73, 0x65, 0x72, + 0x76, 0x65, 0x72, 0x2f, 0x70, 0x6b, 0x67, 0x2f, 0x63, 0x68, 0x61, 0x74, 0x5f, 0x76, 0x31, 0x3b, + 0x63, 0x68, 0x61, 0x74, 0x5f, 0x76, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_chat_proto_rawDescOnce sync.Once + file_chat_proto_rawDescData = file_chat_proto_rawDesc +) + +func file_chat_proto_rawDescGZIP() []byte { + file_chat_proto_rawDescOnce.Do(func() { + file_chat_proto_rawDescData = protoimpl.X.CompressGZIP(file_chat_proto_rawDescData) + }) + return file_chat_proto_rawDescData +} + +var file_chat_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_chat_proto_goTypes = []interface{}{ + (*CreateRequest)(nil), // 0: chat.CreateRequest + (*CreateResponse)(nil), // 1: chat.CreateResponse + (*DeleteRequest)(nil), // 2: chat.DeleteRequest + (*SendMessageRequest)(nil), // 3: chat.SendMessageRequest + (*timestamppb.Timestamp)(nil), // 4: google.protobuf.Timestamp + (*emptypb.Empty)(nil), // 5: google.protobuf.Empty +} +var file_chat_proto_depIdxs = []int32{ + 4, // 0: chat.SendMessageRequest.timestamp:type_name -> google.protobuf.Timestamp + 0, // 1: chat.ChatV1.Create:input_type -> chat.CreateRequest + 2, // 2: chat.ChatV1.Delete:input_type -> chat.DeleteRequest + 3, // 3: chat.ChatV1.SendMessage:input_type -> chat.SendMessageRequest + 1, // 4: chat.ChatV1.Create:output_type -> chat.CreateResponse + 5, // 5: chat.ChatV1.Delete:output_type -> google.protobuf.Empty + 5, // 6: chat.ChatV1.SendMessage:output_type -> google.protobuf.Empty + 4, // [4:7] is the sub-list for method output_type + 1, // [1:4] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_chat_proto_init() } +func file_chat_proto_init() { + if File_chat_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_chat_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*CreateRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_chat_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*CreateResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_chat_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*DeleteRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_chat_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*SendMessageRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_chat_proto_rawDesc, + NumEnums: 0, + NumMessages: 4, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_chat_proto_goTypes, + DependencyIndexes: file_chat_proto_depIdxs, + MessageInfos: file_chat_proto_msgTypes, + }.Build() + File_chat_proto = out.File + file_chat_proto_rawDesc = nil + file_chat_proto_goTypes = nil + file_chat_proto_depIdxs = nil +} diff --git a/pkg/chat_v1/chat_grpc.pb.go b/pkg/chat_v1/chat_grpc.pb.go new file mode 100644 index 0000000..f1c6baa --- /dev/null +++ b/pkg/chat_v1/chat_grpc.pb.go @@ -0,0 +1,178 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v3.21.12 +// source: chat.proto + +package chat_v1 + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + emptypb "google.golang.org/protobuf/types/known/emptypb" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// ChatV1Client is the client API for ChatV1 service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type ChatV1Client interface { + Create(ctx context.Context, in *CreateRequest, opts ...grpc.CallOption) (*CreateResponse, error) + Delete(ctx context.Context, in *DeleteRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) + SendMessage(ctx context.Context, in *SendMessageRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) +} + +type chatV1Client struct { + cc grpc.ClientConnInterface +} + +func NewChatV1Client(cc grpc.ClientConnInterface) ChatV1Client { + return &chatV1Client{cc} +} + +func (c *chatV1Client) Create(ctx context.Context, in *CreateRequest, opts ...grpc.CallOption) (*CreateResponse, error) { + out := new(CreateResponse) + err := c.cc.Invoke(ctx, "/chat.ChatV1/Create", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *chatV1Client) Delete(ctx context.Context, in *DeleteRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { + out := new(emptypb.Empty) + err := c.cc.Invoke(ctx, "/chat.ChatV1/Delete", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *chatV1Client) SendMessage(ctx context.Context, in *SendMessageRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { + out := new(emptypb.Empty) + err := c.cc.Invoke(ctx, "/chat.ChatV1/SendMessage", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// ChatV1Server is the server API for ChatV1 service. +// All implementations must embed UnimplementedChatV1Server +// for forward compatibility +type ChatV1Server interface { + Create(context.Context, *CreateRequest) (*CreateResponse, error) + Delete(context.Context, *DeleteRequest) (*emptypb.Empty, error) + SendMessage(context.Context, *SendMessageRequest) (*emptypb.Empty, error) + mustEmbedUnimplementedChatV1Server() +} + +// UnimplementedChatV1Server must be embedded to have forward compatible implementations. +type UnimplementedChatV1Server struct { +} + +func (UnimplementedChatV1Server) Create(context.Context, *CreateRequest) (*CreateResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Create not implemented") +} +func (UnimplementedChatV1Server) Delete(context.Context, *DeleteRequest) (*emptypb.Empty, error) { + return nil, status.Errorf(codes.Unimplemented, "method Delete not implemented") +} +func (UnimplementedChatV1Server) SendMessage(context.Context, *SendMessageRequest) (*emptypb.Empty, error) { + return nil, status.Errorf(codes.Unimplemented, "method SendMessage not implemented") +} +func (UnimplementedChatV1Server) mustEmbedUnimplementedChatV1Server() {} + +// UnsafeChatV1Server may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to ChatV1Server will +// result in compilation errors. +type UnsafeChatV1Server interface { + mustEmbedUnimplementedChatV1Server() +} + +func RegisterChatV1Server(s grpc.ServiceRegistrar, srv ChatV1Server) { + s.RegisterService(&ChatV1_ServiceDesc, srv) +} + +func _ChatV1_Create_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(CreateRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ChatV1Server).Create(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/chat.ChatV1/Create", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ChatV1Server).Create(ctx, req.(*CreateRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _ChatV1_Delete_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(DeleteRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ChatV1Server).Delete(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/chat.ChatV1/Delete", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ChatV1Server).Delete(ctx, req.(*DeleteRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _ChatV1_SendMessage_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(SendMessageRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ChatV1Server).SendMessage(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/chat.ChatV1/SendMessage", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ChatV1Server).SendMessage(ctx, req.(*SendMessageRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// ChatV1_ServiceDesc is the grpc.ServiceDesc for ChatV1 service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var ChatV1_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "chat.ChatV1", + HandlerType: (*ChatV1Server)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Create", + Handler: _ChatV1_Create_Handler, + }, + { + MethodName: "Delete", + Handler: _ChatV1_Delete_Handler, + }, + { + MethodName: "SendMessage", + Handler: _ChatV1_SendMessage_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "chat.proto", +} From 58a4c5f8aaae5605148af49964e0fa9dd965a006 Mon Sep 17 00:00:00 2001 From: xeeetu Date: Fri, 16 Aug 2024 15:22:14 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D0=B8=20=D0=BA=20=D1=80=D1=83=D1=87=D0=BA?= =?UTF-8?q?=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/chat_server/main.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/chat_server/main.go b/cmd/chat_server/main.go index 479c000..3ff3997 100644 --- a/cmd/chat_server/main.go +++ b/cmd/chat_server/main.go @@ -21,6 +21,7 @@ type server struct { desc.UnimplementedChatV1Server } +// Create создаёт чат, получает массив пользователей func (s *server) Create(ctx context.Context, in *desc.CreateRequest) (*desc.CreateResponse, error) { select { case <-ctx.Done(): @@ -34,6 +35,7 @@ func (s *server) Create(ctx context.Context, in *desc.CreateRequest) (*desc.Crea return &desc.CreateResponse{Id: gofakeit.Int64()}, nil } +// Delete удаляет чат по id func (s *server) Delete(ctx context.Context, in *desc.DeleteRequest) (*emptypb.Empty, error) { select { case <-ctx.Done(): @@ -47,6 +49,7 @@ func (s *server) Delete(ctx context.Context, in *desc.DeleteRequest) (*emptypb.E return &emptypb.Empty{}, nil } +// SendMessage отправляет сообщение на сервер func (s *server) SendMessage(ctx context.Context, in *desc.SendMessageRequest) (*emptypb.Empty, error) { select { case <-ctx.Done():