From e5f19ac95084d67d2f96f51b2feb01f33818863c Mon Sep 17 00:00:00 2001 From: viletyy Date: Thu, 17 Jun 2021 00:45:37 +0800 Subject: [PATCH 1/6] add: grpc server --- go.mod | 3 + initialize/grpc.go | 35 ++++ internal/service/auth.go | 2 +- internal/service/service.go | 6 +- internal/service/vendor.go | 14 +- main.go | 3 +- proto/basic/vendor.pb.go | 338 ++++++++++++++++++++++++++++++++++ proto/basic/vendor.proto | 27 +++ proto/basic/vendor_grpc.pb.go | 101 ++++++++++ proto/common.pb.go | 161 ++++++++++++++++ proto/common.proto | 9 + server/basic/vendor.go | 54 ++++++ 12 files changed, 741 insertions(+), 12 deletions(-) create mode 100644 initialize/grpc.go create mode 100644 proto/basic/vendor.pb.go create mode 100644 proto/basic/vendor.proto create mode 100644 proto/basic/vendor_grpc.pb.go create mode 100644 proto/common.pb.go create mode 100644 proto/common.proto create mode 100644 server/basic/vendor.go diff --git a/go.mod b/go.mod index 8899ad4..1cababf 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/onsi/ginkgo v1.16.4 // indirect github.com/onsi/gomega v1.13.0 // indirect github.com/opentracing/opentracing-go v1.2.0 + github.com/smacker/opentracing-gorm v0.0.0-20181207094635-cd4974441042 // indirect github.com/spf13/viper v1.7.1 github.com/swaggo/gin-swagger v1.3.0 github.com/swaggo/swag v1.7.0 @@ -28,6 +29,8 @@ require ( github.com/viletyy/yolk v1.0.1 go.uber.org/atomic v1.8.0 // indirect go.uber.org/zap v1.17.0 + google.golang.org/grpc v1.38.0 + google.golang.org/protobuf v1.26.0 gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df ) diff --git a/initialize/grpc.go b/initialize/grpc.go new file mode 100644 index 0000000..930d27f --- /dev/null +++ b/initialize/grpc.go @@ -0,0 +1,35 @@ +/* + * @Date: 2021-06-17 00:19:32 + * @LastEditors: viletyy + * @LastEditTime: 2021-06-17 00:33:38 + * @FilePath: /potato/initialize/grpc.go + */ +package initialize + +import ( + "net" + + "github.com/viletyy/potato/global" + pb "github.com/viletyy/potato/proto/basic" + "github.com/viletyy/potato/server/basic" + "google.golang.org/grpc" + "google.golang.org/grpc/reflection" +) + +func RunGrpc() { + server := grpc.NewServer() + pb.RegisterVendorServiceServer(server, basic.NewVendorServer()) + reflection.Register(server) + + go func() { + listen, err := net.Listen("tcp", ":10002") + if err != nil { + global.GO_LOG.Sugar().Fatalf("net.Listen err: %v", err) + } + + err = server.Serve(listen) + if err != nil { + global.GO_LOG.Sugar().Fatalf("server.Serve err: %v", err) + } + }() +} diff --git a/internal/service/auth.go b/internal/service/auth.go index 8c4213b..142e78f 100644 --- a/internal/service/auth.go +++ b/internal/service/auth.go @@ -14,5 +14,5 @@ type AuthRequest struct { } func (svc *Service) CheckAuth(param *AuthRequest) (model.Auth, error) { - return svc.dao.GetAuth(param.AppKey, param.AppSecret) + return svc.Dao.GetAuth(param.AppKey, param.AppSecret) } diff --git a/internal/service/service.go b/internal/service/service.go index e842463..7a5698e 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -1,7 +1,7 @@ /* * @Date: 2021-06-10 18:51:48 * @LastEditors: viletyy - * @LastEditTime: 2021-06-14 21:13:01 + * @LastEditTime: 2021-06-16 23:58:59 * @FilePath: /potato/internal/service/service.go */ package service @@ -16,12 +16,12 @@ import ( type Service struct { Ctx context.Context - dao *dao.Dao + Dao *dao.Dao } func New(ctx context.Context) Service { svc := Service{Ctx: ctx} - svc.dao = dao.New(otgorm.WithContext(svc.Ctx, global.GO_DB)) + svc.Dao = dao.New(otgorm.WithContext(svc.Ctx, global.GO_DB)) return svc } diff --git a/internal/service/vendor.go b/internal/service/vendor.go index 94a9ad2..562571a 100644 --- a/internal/service/vendor.go +++ b/internal/service/vendor.go @@ -1,7 +1,7 @@ /* * @Date: 2021-06-10 17:57:48 * @LastEditors: viletyy - * @LastEditTime: 2021-06-14 23:19:35 + * @LastEditTime: 2021-06-16 23:59:12 * @FilePath: /potato/internal/service/vendor.go */ package service @@ -41,25 +41,25 @@ type DeleteVendorRequest struct { } func (svc *Service) CountVendor(param *CountVendorRequest) (int, error) { - return svc.dao.CountVendor(param.Name, param.Uuid) + return svc.Dao.CountVendor(param.Name, param.Uuid) } func (svc *Service) GetVendorList(param *VendorListRequest, pager *app.Pager) ([]basic.Vendor, error) { - return svc.dao.GetVendorList(param.Name, param.Uuid, pager.Page, pager.PageSize) + return svc.Dao.GetVendorList(param.Name, param.Uuid, pager.Page, pager.PageSize) } func (svc *Service) GetVendor(param *VendorRequest) (basic.Vendor, error) { - return svc.dao.GetVendor(param.ID) + return svc.Dao.GetVendor(param.ID) } func (svc *Service) CreateVendor(param *CreateVendorRequest) (basic.Vendor, error) { - return svc.dao.CreateVendor(param.Name, param.Uuid) + return svc.Dao.CreateVendor(param.Name, param.Uuid) } func (svc *Service) UpdateVendor(param *UpdateVendorRequest) (basic.Vendor, error) { - return svc.dao.UpdateVendor(param.ID, param.Name, param.Uuid) + return svc.Dao.UpdateVendor(param.ID, param.Name, param.Uuid) } func (svc *Service) DeleteVendor(param *DeleteVendorRequest) (basic.Vendor, error) { - return svc.dao.DeleteVendor(param.ID) + return svc.Dao.DeleteVendor(param.ID) } diff --git a/main.go b/main.go index edfa04a..f464e5a 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ /* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy - * @LastEditTime: 2021-06-13 23:32:06 + * @LastEditTime: 2021-06-17 00:29:41 * @FilePath: /potato/main.go */ package main @@ -26,6 +26,7 @@ func main() { defer global.GO_DB.Close() defer global.GO_REDIS.Close() + initialize.RunGrpc() initialize.RunServer() } diff --git a/proto/basic/vendor.pb.go b/proto/basic/vendor.pb.go new file mode 100644 index 0000000..1dda5ae --- /dev/null +++ b/proto/basic/vendor.pb.go @@ -0,0 +1,338 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.17.1 +// source: proto/basic/vendor.proto + +package basic + +import ( + proto "github.com/viletyy/potato/proto" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + 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 GetVendorListRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + Uuid int64 `protobuf:"varint,2,opt,name=uuid,proto3" json:"uuid,omitempty"` + Page int64 `protobuf:"varint,3,opt,name=page,proto3" json:"page,omitempty"` + PageSize int64 `protobuf:"varint,4,opt,name=page_size,json=pageSize,proto3" json:"page_size,omitempty"` +} + +func (x *GetVendorListRequest) Reset() { + *x = GetVendorListRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_basic_vendor_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetVendorListRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetVendorListRequest) ProtoMessage() {} + +func (x *GetVendorListRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_basic_vendor_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 GetVendorListRequest.ProtoReflect.Descriptor instead. +func (*GetVendorListRequest) Descriptor() ([]byte, []int) { + return file_proto_basic_vendor_proto_rawDescGZIP(), []int{0} +} + +func (x *GetVendorListRequest) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *GetVendorListRequest) GetUuid() int64 { + if x != nil { + return x.Uuid + } + return 0 +} + +func (x *GetVendorListRequest) GetPage() int64 { + if x != nil { + return x.Page + } + return 0 +} + +func (x *GetVendorListRequest) GetPageSize() int64 { + if x != nil { + return x.PageSize + } + return 0 +} + +type Vendor struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` + Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` + Uuid int64 `protobuf:"varint,3,opt,name=uuid,proto3" json:"uuid,omitempty"` +} + +func (x *Vendor) Reset() { + *x = Vendor{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_basic_vendor_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Vendor) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Vendor) ProtoMessage() {} + +func (x *Vendor) ProtoReflect() protoreflect.Message { + mi := &file_proto_basic_vendor_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 Vendor.ProtoReflect.Descriptor instead. +func (*Vendor) Descriptor() ([]byte, []int) { + return file_proto_basic_vendor_proto_rawDescGZIP(), []int{1} +} + +func (x *Vendor) GetId() int64 { + if x != nil { + return x.Id + } + return 0 +} + +func (x *Vendor) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *Vendor) GetUuid() int64 { + if x != nil { + return x.Uuid + } + return 0 +} + +type GetVendorListReply struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + List []*Vendor `protobuf:"bytes,1,rep,name=list,proto3" json:"list,omitempty"` + Pager *proto.Pager `protobuf:"bytes,2,opt,name=pager,proto3" json:"pager,omitempty"` +} + +func (x *GetVendorListReply) Reset() { + *x = GetVendorListReply{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_basic_vendor_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetVendorListReply) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetVendorListReply) ProtoMessage() {} + +func (x *GetVendorListReply) ProtoReflect() protoreflect.Message { + mi := &file_proto_basic_vendor_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 GetVendorListReply.ProtoReflect.Descriptor instead. +func (*GetVendorListReply) Descriptor() ([]byte, []int) { + return file_proto_basic_vendor_proto_rawDescGZIP(), []int{2} +} + +func (x *GetVendorListReply) GetList() []*Vendor { + if x != nil { + return x.List + } + return nil +} + +func (x *GetVendorListReply) GetPager() *proto.Pager { + if x != nil { + return x.Pager + } + return nil +} + +var File_proto_basic_vendor_proto protoreflect.FileDescriptor + +var file_proto_basic_vendor_proto_rawDesc = []byte{ + 0x0a, 0x18, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x62, 0x61, 0x73, 0x69, 0x63, 0x2f, 0x76, 0x65, + 0x6e, 0x64, 0x6f, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x12, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x6f, + 0x0a, 0x14, 0x47, 0x65, 0x74, 0x56, 0x65, 0x6e, 0x64, 0x6f, 0x72, 0x4c, 0x69, 0x73, 0x74, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x75, 0x75, + 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x75, 0x75, 0x69, 0x64, 0x12, 0x12, + 0x0a, 0x04, 0x70, 0x61, 0x67, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x70, 0x61, + 0x67, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x70, 0x61, 0x67, 0x65, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x18, + 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x70, 0x61, 0x67, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x22, + 0x40, 0x0a, 0x06, 0x56, 0x65, 0x6e, 0x64, 0x6f, 0x72, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, + 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x12, 0x0a, + 0x04, 0x75, 0x75, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x75, 0x75, 0x69, + 0x64, 0x22, 0x4f, 0x0a, 0x12, 0x47, 0x65, 0x74, 0x56, 0x65, 0x6e, 0x64, 0x6f, 0x72, 0x4c, 0x69, + 0x73, 0x74, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x1b, 0x0a, 0x04, 0x6c, 0x69, 0x73, 0x74, 0x18, + 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x07, 0x2e, 0x56, 0x65, 0x6e, 0x64, 0x6f, 0x72, 0x52, 0x04, + 0x6c, 0x69, 0x73, 0x74, 0x12, 0x1c, 0x0a, 0x05, 0x70, 0x61, 0x67, 0x65, 0x72, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x0b, 0x32, 0x06, 0x2e, 0x50, 0x61, 0x67, 0x65, 0x72, 0x52, 0x05, 0x70, 0x61, 0x67, + 0x65, 0x72, 0x32, 0x4e, 0x0a, 0x0d, 0x56, 0x65, 0x6e, 0x64, 0x6f, 0x72, 0x53, 0x65, 0x72, 0x76, + 0x69, 0x63, 0x65, 0x12, 0x3d, 0x0a, 0x0d, 0x47, 0x65, 0x74, 0x56, 0x65, 0x6e, 0x64, 0x6f, 0x72, + 0x4c, 0x69, 0x73, 0x74, 0x12, 0x15, 0x2e, 0x47, 0x65, 0x74, 0x56, 0x65, 0x6e, 0x64, 0x6f, 0x72, + 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x47, 0x65, + 0x74, 0x56, 0x65, 0x6e, 0x64, 0x6f, 0x72, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x70, 0x6c, 0x79, + 0x22, 0x00, 0x42, 0x27, 0x5a, 0x25, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, + 0x2f, 0x76, 0x69, 0x6c, 0x65, 0x74, 0x79, 0x79, 0x2f, 0x70, 0x6f, 0x74, 0x61, 0x74, 0x6f, 0x2f, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x62, 0x61, 0x73, 0x69, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x33, +} + +var ( + file_proto_basic_vendor_proto_rawDescOnce sync.Once + file_proto_basic_vendor_proto_rawDescData = file_proto_basic_vendor_proto_rawDesc +) + +func file_proto_basic_vendor_proto_rawDescGZIP() []byte { + file_proto_basic_vendor_proto_rawDescOnce.Do(func() { + file_proto_basic_vendor_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_basic_vendor_proto_rawDescData) + }) + return file_proto_basic_vendor_proto_rawDescData +} + +var file_proto_basic_vendor_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_proto_basic_vendor_proto_goTypes = []interface{}{ + (*GetVendorListRequest)(nil), // 0: GetVendorListRequest + (*Vendor)(nil), // 1: Vendor + (*GetVendorListReply)(nil), // 2: GetVendorListReply + (*proto.Pager)(nil), // 3: Pager +} +var file_proto_basic_vendor_proto_depIdxs = []int32{ + 1, // 0: GetVendorListReply.list:type_name -> Vendor + 3, // 1: GetVendorListReply.pager:type_name -> Pager + 0, // 2: VendorService.GetVendorList:input_type -> GetVendorListRequest + 2, // 3: VendorService.GetVendorList:output_type -> GetVendorListReply + 3, // [3:4] is the sub-list for method output_type + 2, // [2:3] is the sub-list for method input_type + 2, // [2:2] is the sub-list for extension type_name + 2, // [2:2] is the sub-list for extension extendee + 0, // [0:2] is the sub-list for field type_name +} + +func init() { file_proto_basic_vendor_proto_init() } +func file_proto_basic_vendor_proto_init() { + if File_proto_basic_vendor_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_basic_vendor_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetVendorListRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_basic_vendor_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Vendor); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_basic_vendor_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetVendorListReply); 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_proto_basic_vendor_proto_rawDesc, + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_basic_vendor_proto_goTypes, + DependencyIndexes: file_proto_basic_vendor_proto_depIdxs, + MessageInfos: file_proto_basic_vendor_proto_msgTypes, + }.Build() + File_proto_basic_vendor_proto = out.File + file_proto_basic_vendor_proto_rawDesc = nil + file_proto_basic_vendor_proto_goTypes = nil + file_proto_basic_vendor_proto_depIdxs = nil +} diff --git a/proto/basic/vendor.proto b/proto/basic/vendor.proto new file mode 100644 index 0000000..9c6efb7 --- /dev/null +++ b/proto/basic/vendor.proto @@ -0,0 +1,27 @@ +syntax = "proto3"; + +option go_package = "github.com/viletyy/potato/proto/basic"; + +import "proto/common.proto"; + +service VendorService { + rpc GetVendorList(GetVendorListRequest) returns (GetVendorListReply) {} +} + +message GetVendorListRequest { + string name = 1; + int64 uuid = 2; + int64 page = 3; + int64 page_size = 4; +} + +message Vendor { + int64 id = 1; + string name = 2; + int64 uuid = 3; +} + +message GetVendorListReply { + repeated Vendor list = 1; + Pager pager = 2; +} \ No newline at end of file diff --git a/proto/basic/vendor_grpc.pb.go b/proto/basic/vendor_grpc.pb.go new file mode 100644 index 0000000..f6dfe61 --- /dev/null +++ b/proto/basic/vendor_grpc.pb.go @@ -0,0 +1,101 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. + +package basic + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// 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 + +// VendorServiceClient is the client API for VendorService 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 VendorServiceClient interface { + GetVendorList(ctx context.Context, in *GetVendorListRequest, opts ...grpc.CallOption) (*GetVendorListReply, error) +} + +type vendorServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewVendorServiceClient(cc grpc.ClientConnInterface) VendorServiceClient { + return &vendorServiceClient{cc} +} + +func (c *vendorServiceClient) GetVendorList(ctx context.Context, in *GetVendorListRequest, opts ...grpc.CallOption) (*GetVendorListReply, error) { + out := new(GetVendorListReply) + err := c.cc.Invoke(ctx, "/VendorService/GetVendorList", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// VendorServiceServer is the server API for VendorService service. +// All implementations must embed UnimplementedVendorServiceServer +// for forward compatibility +type VendorServiceServer interface { + GetVendorList(context.Context, *GetVendorListRequest) (*GetVendorListReply, error) + mustEmbedUnimplementedVendorServiceServer() +} + +// UnimplementedVendorServiceServer must be embedded to have forward compatible implementations. +type UnimplementedVendorServiceServer struct { +} + +func (UnimplementedVendorServiceServer) GetVendorList(context.Context, *GetVendorListRequest) (*GetVendorListReply, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetVendorList not implemented") +} +func (UnimplementedVendorServiceServer) mustEmbedUnimplementedVendorServiceServer() {} + +// UnsafeVendorServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to VendorServiceServer will +// result in compilation errors. +type UnsafeVendorServiceServer interface { + mustEmbedUnimplementedVendorServiceServer() +} + +func RegisterVendorServiceServer(s grpc.ServiceRegistrar, srv VendorServiceServer) { + s.RegisterService(&VendorService_ServiceDesc, srv) +} + +func _VendorService_GetVendorList_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(GetVendorListRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(VendorServiceServer).GetVendorList(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/VendorService/GetVendorList", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(VendorServiceServer).GetVendorList(ctx, req.(*GetVendorListRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// VendorService_ServiceDesc is the grpc.ServiceDesc for VendorService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var VendorService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "VendorService", + HandlerType: (*VendorServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "GetVendorList", + Handler: _VendorService_GetVendorList_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "proto/basic/vendor.proto", +} diff --git a/proto/common.pb.go b/proto/common.pb.go new file mode 100644 index 0000000..d20407d --- /dev/null +++ b/proto/common.pb.go @@ -0,0 +1,161 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.17.1 +// source: proto/common.proto + +package proto + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + 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 Pager struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Page int64 `protobuf:"varint,1,opt,name=page,proto3" json:"page,omitempty"` + PageSize int64 `protobuf:"varint,2,opt,name=page_size,json=pageSize,proto3" json:"page_size,omitempty"` + Total int64 `protobuf:"varint,3,opt,name=total,proto3" json:"total,omitempty"` +} + +func (x *Pager) Reset() { + *x = Pager{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_common_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Pager) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Pager) ProtoMessage() {} + +func (x *Pager) ProtoReflect() protoreflect.Message { + mi := &file_proto_common_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 Pager.ProtoReflect.Descriptor instead. +func (*Pager) Descriptor() ([]byte, []int) { + return file_proto_common_proto_rawDescGZIP(), []int{0} +} + +func (x *Pager) GetPage() int64 { + if x != nil { + return x.Page + } + return 0 +} + +func (x *Pager) GetPageSize() int64 { + if x != nil { + return x.PageSize + } + return 0 +} + +func (x *Pager) GetTotal() int64 { + if x != nil { + return x.Total + } + return 0 +} + +var File_proto_common_proto protoreflect.FileDescriptor + +var file_proto_common_proto_rawDesc = []byte{ + 0x0a, 0x12, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x4e, 0x0a, 0x05, 0x50, 0x61, 0x67, 0x65, 0x72, 0x12, 0x12, 0x0a, + 0x04, 0x70, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x70, 0x61, 0x67, + 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x70, 0x61, 0x67, 0x65, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x70, 0x61, 0x67, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x12, 0x14, + 0x0a, 0x05, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x74, + 0x6f, 0x74, 0x61, 0x6c, 0x42, 0x21, 0x5a, 0x1f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, + 0x6f, 0x6d, 0x2f, 0x76, 0x69, 0x6c, 0x65, 0x74, 0x79, 0x79, 0x2f, 0x70, 0x6f, 0x74, 0x61, 0x74, + 0x6f, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_proto_common_proto_rawDescOnce sync.Once + file_proto_common_proto_rawDescData = file_proto_common_proto_rawDesc +) + +func file_proto_common_proto_rawDescGZIP() []byte { + file_proto_common_proto_rawDescOnce.Do(func() { + file_proto_common_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_common_proto_rawDescData) + }) + return file_proto_common_proto_rawDescData +} + +var file_proto_common_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_proto_common_proto_goTypes = []interface{}{ + (*Pager)(nil), // 0: Pager +} +var file_proto_common_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_proto_common_proto_init() } +func file_proto_common_proto_init() { + if File_proto_common_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_common_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Pager); 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_proto_common_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_proto_common_proto_goTypes, + DependencyIndexes: file_proto_common_proto_depIdxs, + MessageInfos: file_proto_common_proto_msgTypes, + }.Build() + File_proto_common_proto = out.File + file_proto_common_proto_rawDesc = nil + file_proto_common_proto_goTypes = nil + file_proto_common_proto_depIdxs = nil +} diff --git a/proto/common.proto b/proto/common.proto new file mode 100644 index 0000000..6e90536 --- /dev/null +++ b/proto/common.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; + +option go_package= "github.com/viletyy/potato/proto"; + +message Pager { + int64 page = 1; + int64 page_size = 2; + int64 total = 3; +} \ No newline at end of file diff --git a/server/basic/vendor.go b/server/basic/vendor.go new file mode 100644 index 0000000..e473a01 --- /dev/null +++ b/server/basic/vendor.go @@ -0,0 +1,54 @@ +/* + * @Date: 2021-06-16 23:49:02 + * @LastEditors: viletyy + * @LastEditTime: 2021-06-17 00:18:47 + * @FilePath: /potato/server/basic/vendor.go + */ +package basic + +import ( + "context" + "encoding/json" + + "github.com/viletyy/potato/internal/service" + "github.com/viletyy/potato/pkg/app" + pb "github.com/viletyy/potato/proto/basic" +) + +type VendorServer struct { + pb.UnimplementedVendorServiceServer +} + +func NewVendorServer() *VendorServer { + return &VendorServer{} +} + +func (t *VendorServer) GetVendorList(ctx context.Context, r *pb.GetVendorListRequest) (*pb.GetVendorListReply, error) { + svc := service.New(ctx) + dbVendorList, err := svc.Dao.GetVendorList(r.GetName(), int(r.GetUuid()), int(r.GetPage()), int(r.GetPageSize())) + if err != nil { + return nil, err + } + total, err := svc.Dao.CountVendor(r.GetName(), int(r.GetUuid())) + if err != nil { + return nil, err + } + data := map[string]interface{}{ + "list": dbVendorList, + "pager": app.Pager{ + Page: int(r.GetPage()), + PageSize: int(r.GetPageSize()), + Total: total, + }, + } + byteData, err := json.Marshal(data) + if err != nil { + return nil, err + } + vendorList := pb.GetVendorListReply{} + err = json.Unmarshal(byteData, &vendorList) + if err != nil { + return nil, err + } + return &vendorList, nil +} From f44beb99005d349c4f45a0b17ec2adce36da8ed1 Mon Sep 17 00:00:00 2001 From: viletyy Date: Fri, 18 Jun 2021 22:40:33 +0800 Subject: [PATCH 2/6] add: grpc errcode --- pkg/errcode/errcode.go | 2 +- pkg/errcode/rpc/rpc_common_code.go | 20 +++++ pkg/errcode/rpc/rpc_errcode.go | 81 +++++++++++++++++++ pkg/errcode/rpc/rpc_module_error.go | 16 ++++ proto/common.pb.go | 119 ++++++++++++++++++++++++---- proto/common.proto | 8 ++ server/basic/vendor.go | 11 +-- 7 files changed, 235 insertions(+), 22 deletions(-) create mode 100644 pkg/errcode/rpc/rpc_common_code.go create mode 100644 pkg/errcode/rpc/rpc_errcode.go create mode 100644 pkg/errcode/rpc/rpc_module_error.go diff --git a/pkg/errcode/errcode.go b/pkg/errcode/errcode.go index aa8d201..9eed983 100644 --- a/pkg/errcode/errcode.go +++ b/pkg/errcode/errcode.go @@ -1,7 +1,7 @@ /* * @Date: 2021-06-10 16:30:10 * @LastEditors: viletyy - * @LastEditTime: 2021-06-11 15:36:11 + * @LastEditTime: 2021-06-18 22:22:55 * @FilePath: /potato/pkg/errcode/errcode.go */ package errcode diff --git a/pkg/errcode/rpc/rpc_common_code.go b/pkg/errcode/rpc/rpc_common_code.go new file mode 100644 index 0000000..6e84dbc --- /dev/null +++ b/pkg/errcode/rpc/rpc_common_code.go @@ -0,0 +1,20 @@ +/* + * @Date: 2021-06-18 21:37:14 + * @LastEditors: viletyy + * @LastEditTime: 2021-06-18 22:21:54 + * @FilePath: /potato/pkg/errcode/rpc/rpc_common_code.go + */ +package rpc + +var ( + RpcSuccess = NewRpcError(0, "成功") + RpcFail = NewRpcError(10000, "内部错误") + RpcInvalidParams = NewRpcError(10001, "无效参数") + RpcUnauthorized = NewRpcError(10002, "认证错误") + RpcNotFound = NewRpcError(10003, "没有找到") + RpcUnknown = NewRpcError(10004, "未知") + RpcDeadlineExceeded = NewRpcError(10005, "超出最后截止期限") + RpcAccessDenied = NewRpcError(10006, "访问被拒绝") + RpcLimitExceed = NewRpcError(10007, "访问限制") + RpcMethodNotAllowed = NewRpcError(10008, "不支持该方法") +) diff --git a/pkg/errcode/rpc/rpc_errcode.go b/pkg/errcode/rpc/rpc_errcode.go new file mode 100644 index 0000000..5e13336 --- /dev/null +++ b/pkg/errcode/rpc/rpc_errcode.go @@ -0,0 +1,81 @@ +/* + * @Date: 2021-06-18 21:42:53 + * @LastEditors: viletyy + * @LastEditTime: 2021-06-18 22:31:20 + * @FilePath: /potato/pkg/errcode/rpc/rpc_errcode.go + */ +package rpc + +import ( + "fmt" + + pb "github.com/viletyy/potato/proto" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +type RpcError struct { + Code int + Msg string +} + +type Status struct { + *status.Status +} + +var rpcCodes = map[int]string{} + +func FromError(err error) *Status { + s, _ := status.FromError(err) + return &Status{s} +} + +func NewRpcError(code int, msg string) *RpcError { + if _, ok := rpcCodes[code]; ok { + panic(fmt.Sprintf("错误码 %d 已经存在,请更换一个", code)) + } + rpcCodes[code] = msg + return &RpcError{Code: code, Msg: msg} +} + +func (e *RpcError) Error() string { + return fmt.Sprintf("错误码:%d,错误信息:%s", e.Code, e.Msg) +} + +func ToRpcError(err *RpcError) error { + pbErr := &pb.Error{Code: int32(err.Code), Message: err.Msg} + s, _ := status.New(ToRpcCode(err.Code), err.Msg).WithDetails(pbErr) + return s.Err() +} + +func ToRpcStatus(code int, msg string) *Status { + pbErr := &pb.Error{Code: int32(code), Message: msg} + s, _ := status.New(ToRpcCode(code), msg).WithDetails(pbErr) + return &Status{s} +} + +func ToRpcCode(code int) codes.Code { + var statusCode codes.Code + switch code { + case RpcFail.Code: + statusCode = codes.Internal + case RpcInvalidParams.Code: + statusCode = codes.InvalidArgument + case RpcUnauthorized.Code: + statusCode = codes.Unauthenticated + case RpcAccessDenied.Code: + statusCode = codes.PermissionDenied + case RpcDeadlineExceeded.Code: + statusCode = codes.DeadlineExceeded + case RpcNotFound.Code: + statusCode = codes.NotFound + case RpcLimitExceed.Code: + statusCode = codes.ResourceExhausted + case RpcMethodNotAllowed.Code: + statusCode = codes.Unimplemented + default: + statusCode = codes.Unknown + } + + return statusCode +} diff --git a/pkg/errcode/rpc/rpc_module_error.go b/pkg/errcode/rpc/rpc_module_error.go new file mode 100644 index 0000000..aadc96d --- /dev/null +++ b/pkg/errcode/rpc/rpc_module_error.go @@ -0,0 +1,16 @@ +/* + * @Date: 2021-06-18 22:32:02 + * @LastEditors: viletyy + * @LastEditTime: 2021-06-18 22:33:40 + * @FilePath: /potato/pkg/errcode/rpc/rpc_module_error.go + */ +package rpc + +var ( + RpcErrorGetVendorListFail = NewRpcError(20101, "获取系统厂商列表失败") + RpcErrorGetVendorFail = NewRpcError(20102, "获取系统厂商失败") + RpcErrorCreateVendorFail = NewRpcError(20103, "创建系统厂商失败") + RpcErrorUpdateVendorFail = NewRpcError(20104, "更新系统厂商失败") + RpcErrorDeleteVendorFail = NewRpcError(20105, "删除系统厂商失败") + RpcErrorCountVendorFail = NewRpcError(20106, "统计系统厂商失败") +) diff --git a/proto/common.pb.go b/proto/common.pb.go index d20407d..852486a 100644 --- a/proto/common.pb.go +++ b/proto/common.pb.go @@ -9,6 +9,7 @@ package proto import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" + anypb "google.golang.org/protobuf/types/known/anypb" reflect "reflect" sync "sync" ) @@ -83,18 +84,89 @@ func (x *Pager) GetTotal() int64 { return 0 } +type Error struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Code int32 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"` + Message string `protobuf:"bytes,2,opt,name=message,proto3" json:"message,omitempty"` + Data *anypb.Any `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"` +} + +func (x *Error) Reset() { + *x = Error{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_common_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Error) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Error) ProtoMessage() {} + +func (x *Error) ProtoReflect() protoreflect.Message { + mi := &file_proto_common_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 Error.ProtoReflect.Descriptor instead. +func (*Error) Descriptor() ([]byte, []int) { + return file_proto_common_proto_rawDescGZIP(), []int{1} +} + +func (x *Error) GetCode() int32 { + if x != nil { + return x.Code + } + return 0 +} + +func (x *Error) GetMessage() string { + if x != nil { + return x.Message + } + return "" +} + +func (x *Error) GetData() *anypb.Any { + if x != nil { + return x.Data + } + return nil +} + var File_proto_common_proto protoreflect.FileDescriptor var file_proto_common_proto_rawDesc = []byte{ 0x0a, 0x12, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x4e, 0x0a, 0x05, 0x50, 0x61, 0x67, 0x65, 0x72, 0x12, 0x12, 0x0a, - 0x04, 0x70, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x70, 0x61, 0x67, - 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x70, 0x61, 0x67, 0x65, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x18, 0x02, - 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x70, 0x61, 0x67, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x12, 0x14, - 0x0a, 0x05, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x74, - 0x6f, 0x74, 0x61, 0x6c, 0x42, 0x21, 0x5a, 0x1f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, - 0x6f, 0x6d, 0x2f, 0x76, 0x69, 0x6c, 0x65, 0x74, 0x79, 0x79, 0x2f, 0x70, 0x6f, 0x74, 0x61, 0x74, - 0x6f, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x19, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x61, 0x6e, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, + 0x4e, 0x0a, 0x05, 0x50, 0x61, 0x67, 0x65, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, 0x67, 0x65, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x70, 0x61, 0x67, 0x65, 0x12, 0x1b, 0x0a, 0x09, + 0x70, 0x61, 0x67, 0x65, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, + 0x08, 0x70, 0x61, 0x67, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x74, + 0x61, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x22, + 0x5f, 0x0a, 0x05, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x18, 0x0a, 0x07, + 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, + 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x28, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x03, + 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x41, 0x6e, 0x79, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, + 0x42, 0x21, 0x5a, 0x1f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x76, + 0x69, 0x6c, 0x65, 0x74, 0x79, 0x79, 0x2f, 0x70, 0x6f, 0x74, 0x61, 0x74, 0x6f, 0x2f, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -109,16 +181,19 @@ func file_proto_common_proto_rawDescGZIP() []byte { return file_proto_common_proto_rawDescData } -var file_proto_common_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_proto_common_proto_msgTypes = make([]protoimpl.MessageInfo, 2) var file_proto_common_proto_goTypes = []interface{}{ - (*Pager)(nil), // 0: Pager + (*Pager)(nil), // 0: Pager + (*Error)(nil), // 1: Error + (*anypb.Any)(nil), // 2: google.protobuf.Any } var file_proto_common_proto_depIdxs = []int32{ - 0, // [0:0] is the sub-list for method output_type - 0, // [0:0] is the sub-list for method input_type - 0, // [0:0] is the sub-list for extension type_name - 0, // [0:0] is the sub-list for extension extendee - 0, // [0:0] is the sub-list for field type_name + 2, // 0: Error.data:type_name -> google.protobuf.Any + 1, // [1:1] is the sub-list for method output_type + 1, // [1:1] 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_proto_common_proto_init() } @@ -139,6 +214,18 @@ func file_proto_common_proto_init() { return nil } } + file_proto_common_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Error); 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{ @@ -146,7 +233,7 @@ func file_proto_common_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_proto_common_proto_rawDesc, NumEnums: 0, - NumMessages: 1, + NumMessages: 2, NumExtensions: 0, NumServices: 0, }, diff --git a/proto/common.proto b/proto/common.proto index 6e90536..6fe1ab9 100644 --- a/proto/common.proto +++ b/proto/common.proto @@ -1,9 +1,17 @@ syntax = "proto3"; +import "google/protobuf/any.proto"; + option go_package= "github.com/viletyy/potato/proto"; message Pager { int64 page = 1; int64 page_size = 2; int64 total = 3; +} + +message Error { + int32 code = 1; + string message = 2; + google.protobuf.Any data = 3; } \ No newline at end of file diff --git a/server/basic/vendor.go b/server/basic/vendor.go index e473a01..f7df94a 100644 --- a/server/basic/vendor.go +++ b/server/basic/vendor.go @@ -1,7 +1,7 @@ /* * @Date: 2021-06-16 23:49:02 * @LastEditors: viletyy - * @LastEditTime: 2021-06-17 00:18:47 + * @LastEditTime: 2021-06-18 22:39:40 * @FilePath: /potato/server/basic/vendor.go */ package basic @@ -12,6 +12,7 @@ import ( "github.com/viletyy/potato/internal/service" "github.com/viletyy/potato/pkg/app" + "github.com/viletyy/potato/pkg/errcode/rpc" pb "github.com/viletyy/potato/proto/basic" ) @@ -27,11 +28,11 @@ func (t *VendorServer) GetVendorList(ctx context.Context, r *pb.GetVendorListReq svc := service.New(ctx) dbVendorList, err := svc.Dao.GetVendorList(r.GetName(), int(r.GetUuid()), int(r.GetPage()), int(r.GetPageSize())) if err != nil { - return nil, err + return nil, rpc.ToRpcError(rpc.RpcErrorGetVendorListFail) } total, err := svc.Dao.CountVendor(r.GetName(), int(r.GetUuid())) if err != nil { - return nil, err + return nil, rpc.ToRpcError(rpc.RpcErrorCountVendorFail) } data := map[string]interface{}{ "list": dbVendorList, @@ -43,12 +44,12 @@ func (t *VendorServer) GetVendorList(ctx context.Context, r *pb.GetVendorListReq } byteData, err := json.Marshal(data) if err != nil { - return nil, err + return nil, rpc.ToRpcError(rpc.RpcInvalidParams) } vendorList := pb.GetVendorListReply{} err = json.Unmarshal(byteData, &vendorList) if err != nil { - return nil, err + return nil, rpc.ToRpcError(rpc.RpcInvalidParams) } return &vendorList, nil } From dd3e27e3d64249f28fb486fcedc198292138fb5e Mon Sep 17 00:00:00 2001 From: viletyy Date: Sat, 19 Jun 2021 22:42:11 +0800 Subject: [PATCH 3/6] add: grpc server port config --- config.yaml.example | 1 + config/server.go | 3 ++- initialize/grpc.go | 5 +++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/config.yaml.example b/config.yaml.example index 5ef4415..55ee942 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -13,6 +13,7 @@ app: upload_image_allow_exts: ['.jpg','.jpeg','.png'] server: http_port: 8000 + rpc_port: 8001 read_timeout: 60 write_timeout: 60 tracer_host_port: '127.0.0.1:6831' diff --git a/config/server.go b/config/server.go index 51589cc..1260df6 100644 --- a/config/server.go +++ b/config/server.go @@ -1,13 +1,14 @@ /* * @Date: 2021-03-22 09:54:07 * @LastEditors: viletyy - * @LastEditTime: 2021-06-14 20:40:49 + * @LastEditTime: 2021-06-19 22:38:44 * @FilePath: /potato/config/server.go */ package config type Server struct { HttpPort int64 `mapstructure:"http_port" json:"http_port" yaml:"http_port"` + RpcPort int64 `mapstructure:"rpc_port" json:"rpc_port" yaml:"rpc_port"` ReadTimeout int64 `mapstructure:"read_timeout" json:"read_timeout" yaml:"read_timeout"` WriteTimeout int64 `mapstructure:"write_timeout" json:"write_timeout" yaml:"write_timeout"` TracerHostPort string `mapstructure:"tracer_host_port" json:"tracer_host_port" yaml:"tracer_host_port"` diff --git a/initialize/grpc.go b/initialize/grpc.go index 930d27f..cb8f314 100644 --- a/initialize/grpc.go +++ b/initialize/grpc.go @@ -1,7 +1,7 @@ /* * @Date: 2021-06-17 00:19:32 * @LastEditors: viletyy - * @LastEditTime: 2021-06-17 00:33:38 + * @LastEditTime: 2021-06-19 22:41:24 * @FilePath: /potato/initialize/grpc.go */ package initialize @@ -12,6 +12,7 @@ import ( "github.com/viletyy/potato/global" pb "github.com/viletyy/potato/proto/basic" "github.com/viletyy/potato/server/basic" + "github.com/viletyy/yolk/convert" "google.golang.org/grpc" "google.golang.org/grpc/reflection" ) @@ -22,7 +23,7 @@ func RunGrpc() { reflection.Register(server) go func() { - listen, err := net.Listen("tcp", ":10002") + listen, err := net.Listen("tcp", ":"+convert.ToString(global.GO_CONFIG.Server.RpcPort)) if err != nil { global.GO_LOG.Sugar().Fatalf("net.Listen err: %v", err) } From 4df74ff80acec25d42c2c3d2a7a22d27ffa0f96c Mon Sep 17 00:00:00 2001 From: viletyy Date: Sun, 20 Jun 2021 19:29:27 +0800 Subject: [PATCH 4/6] add: more proto insist --- go.mod | 2 ++ initialize/grpc.go | 36 ---------------------------------- initialize/grpc_server.go | 33 +++++++++++++++++++++++++++++++ initialize/server.go | 41 ++++++++++++++++++--------------------- main.go | 40 +++++++++++++++++++++++++++++++++++--- 5 files changed, 91 insertions(+), 61 deletions(-) delete mode 100644 initialize/grpc.go create mode 100644 initialize/grpc_server.go diff --git a/go.mod b/go.mod index 1cababf..11c2c62 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/onsi/gomega v1.13.0 // indirect github.com/opentracing/opentracing-go v1.2.0 github.com/smacker/opentracing-gorm v0.0.0-20181207094635-cd4974441042 // indirect + github.com/soheilhy/cmux v0.1.5 github.com/spf13/viper v1.7.1 github.com/swaggo/gin-swagger v1.3.0 github.com/swaggo/swag v1.7.0 @@ -29,6 +30,7 @@ require ( github.com/viletyy/yolk v1.0.1 go.uber.org/atomic v1.8.0 // indirect go.uber.org/zap v1.17.0 + golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect google.golang.org/grpc v1.38.0 google.golang.org/protobuf v1.26.0 gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect diff --git a/initialize/grpc.go b/initialize/grpc.go deleted file mode 100644 index cb8f314..0000000 --- a/initialize/grpc.go +++ /dev/null @@ -1,36 +0,0 @@ -/* - * @Date: 2021-06-17 00:19:32 - * @LastEditors: viletyy - * @LastEditTime: 2021-06-19 22:41:24 - * @FilePath: /potato/initialize/grpc.go - */ -package initialize - -import ( - "net" - - "github.com/viletyy/potato/global" - pb "github.com/viletyy/potato/proto/basic" - "github.com/viletyy/potato/server/basic" - "github.com/viletyy/yolk/convert" - "google.golang.org/grpc" - "google.golang.org/grpc/reflection" -) - -func RunGrpc() { - server := grpc.NewServer() - pb.RegisterVendorServiceServer(server, basic.NewVendorServer()) - reflection.Register(server) - - go func() { - listen, err := net.Listen("tcp", ":"+convert.ToString(global.GO_CONFIG.Server.RpcPort)) - if err != nil { - global.GO_LOG.Sugar().Fatalf("net.Listen err: %v", err) - } - - err = server.Serve(listen) - if err != nil { - global.GO_LOG.Sugar().Fatalf("server.Serve err: %v", err) - } - }() -} diff --git a/initialize/grpc_server.go b/initialize/grpc_server.go new file mode 100644 index 0000000..173c2fe --- /dev/null +++ b/initialize/grpc_server.go @@ -0,0 +1,33 @@ +/* + * @Date: 2021-06-17 00:19:32 + * @LastEditors: viletyy + * @LastEditTime: 2021-06-20 19:06:15 + * @FilePath: /potato/initialize/grpc_server.go + */ +package initialize + +import ( + pb "github.com/viletyy/potato/proto/basic" + "github.com/viletyy/potato/server/basic" + "google.golang.org/grpc" + "google.golang.org/grpc/reflection" +) + +func RunGrpcServer() *grpc.Server { + server := grpc.NewServer() + pb.RegisterVendorServiceServer(server, basic.NewVendorServer()) + reflection.Register(server) + + // go func() { + // listen, err := net.Listen("tcp", ":"+convert.ToString(global.GO_CONFIG.Server.RpcPort)) + // if err != nil { + // global.GO_LOG.Sugar().Fatalf("net.Listen err: %v", err) + // } + + // err = server.Serve(listen) + // if err != nil { + // global.GO_LOG.Sugar().Fatalf("server.Serve err: %v", err) + // } + // }() + return server +} diff --git a/initialize/server.go b/initialize/server.go index 2d1986c..a2178d6 100644 --- a/initialize/server.go +++ b/initialize/server.go @@ -1,50 +1,47 @@ /* * @Date: 2021-03-22 17:03:27 * @LastEditors: viletyy - * @LastEditTime: 2021-06-10 22:24:39 + * @LastEditTime: 2021-06-20 19:18:10 * @FilePath: /potato/initialize/server.go */ package initialize import ( - "context" - "fmt" "net/http" - "os" - "os/signal" "time" "github.com/viletyy/potato/global" "github.com/viletyy/potato/internal/routers" ) -func RunServer() { +func RunServer(port string) *http.Server { router := routers.InitRouter() server := &http.Server{ - Addr: fmt.Sprintf(":%d", global.GO_CONFIG.Server.HttpPort), + Addr: port, Handler: router, ReadTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second, WriteTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second, MaxHeaderBytes: 1 << 20, } - go func() { - if err := server.ListenAndServe(); err != nil { - global.GO_LOG.Info(fmt.Sprintf("Listen: %s\n", err)) - } - }() + // go func() { + // if err := server.ListenAndServe(); err != nil { + // global.GO_LOG.Info(fmt.Sprintf("Listen: %s\n", err)) + // } + // }() - quit := make(chan os.Signal) - signal.Notify(quit, os.Interrupt) - <-quit + // quit := make(chan os.Signal) + // signal.Notify(quit, os.Interrupt) + // <-quit - global.GO_LOG.Info("Shutdown Server") + // global.GO_LOG.Info("Shutdown Server") - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - if err := server.Shutdown(ctx); err != nil { - global.GO_LOG.Fatal(fmt.Sprintf("Server Shutdown: %v", err)) - } + // ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + // defer cancel() + // if err := server.Shutdown(ctx); err != nil { + // global.GO_LOG.Fatal(fmt.Sprintf("Server Shutdown: %v", err)) + // } - global.GO_LOG.Info("Server exiting") + // global.GO_LOG.Info("Server exiting") + return server } diff --git a/main.go b/main.go index f464e5a..28e4663 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,19 @@ /* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy - * @LastEditTime: 2021-06-17 00:29:41 + * @LastEditTime: 2021-06-20 19:24:16 * @FilePath: /potato/main.go */ package main import ( + "flag" + "net" + + "github.com/soheilhy/cmux" "github.com/viletyy/potato/global" "github.com/viletyy/potato/initialize" + "github.com/viletyy/yolk/convert" ) // @title Potato Api @@ -16,6 +21,12 @@ import ( // @description This is a potato use golang // @BasePath /api +var port string + +func runTcpServer(port string) (net.Listener, error) { + return net.Listen("tcp", ":"+port) +} + func main() { global.GO_VP = initialize.Viper() global.GO_LOG = initialize.Zap() @@ -26,7 +37,30 @@ func main() { defer global.GO_DB.Close() defer global.GO_REDIS.Close() - initialize.RunGrpc() - initialize.RunServer() + flag.StringVar(&port, "port", convert.ToString(global.GO_CONFIG.Server.HttpPort), "启动端口号") + flag.Parse() + + l, err := runTcpServer(port) + if err != nil { + global.GO_LOG.Sugar().Fatalf("Run Tcp Server err: %v", err) + } + m := cmux.New(l) + grpcL := m.MatchWithWriters( + cmux.HTTP2MatchHeaderFieldSendSettings( + "content-type", + "application/grpc", + ), + ) + httpL := m.Match(cmux.HTTP1Fast()) + + grpcS := initialize.RunGrpcServer() + httpS := initialize.RunServer(port) + go grpcS.Serve(grpcL) + go httpS.Serve(httpL) + + err = m.Serve() + if err != nil { + global.GO_LOG.Sugar().Fatalf("Run Server err: %v", err) + } } From f317b9ecb70c2ace96c1089803125cbbe5470a41 Mon Sep 17 00:00:00 2001 From: viletyy Date: Sun, 20 Jun 2021 19:47:08 +0800 Subject: [PATCH 5/6] fix: change config and annotation code --- config.yaml.example | 3 +-- config/server.go | 5 ++--- initialize/grpc_server.go | 13 +------------ initialize/server.go | 19 ------------------- main.go | 4 ++-- 5 files changed, 6 insertions(+), 38 deletions(-) diff --git a/config.yaml.example b/config.yaml.example index 55ee942..3071086 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -12,8 +12,7 @@ app: upload_image_max_size: 5 # MB upload_image_allow_exts: ['.jpg','.jpeg','.png'] server: - http_port: 8000 - rpc_port: 8001 + port: 8000 read_timeout: 60 write_timeout: 60 tracer_host_port: '127.0.0.1:6831' diff --git a/config/server.go b/config/server.go index 1260df6..e9b1110 100644 --- a/config/server.go +++ b/config/server.go @@ -1,14 +1,13 @@ /* * @Date: 2021-03-22 09:54:07 * @LastEditors: viletyy - * @LastEditTime: 2021-06-19 22:38:44 + * @LastEditTime: 2021-06-20 19:45:31 * @FilePath: /potato/config/server.go */ package config type Server struct { - HttpPort int64 `mapstructure:"http_port" json:"http_port" yaml:"http_port"` - RpcPort int64 `mapstructure:"rpc_port" json:"rpc_port" yaml:"rpc_port"` + Port int64 `mapstructure:"port" json:"port" yaml:"port"` ReadTimeout int64 `mapstructure:"read_timeout" json:"read_timeout" yaml:"read_timeout"` WriteTimeout int64 `mapstructure:"write_timeout" json:"write_timeout" yaml:"write_timeout"` TracerHostPort string `mapstructure:"tracer_host_port" json:"tracer_host_port" yaml:"tracer_host_port"` diff --git a/initialize/grpc_server.go b/initialize/grpc_server.go index 173c2fe..a936199 100644 --- a/initialize/grpc_server.go +++ b/initialize/grpc_server.go @@ -1,7 +1,7 @@ /* * @Date: 2021-06-17 00:19:32 * @LastEditors: viletyy - * @LastEditTime: 2021-06-20 19:06:15 + * @LastEditTime: 2021-06-20 19:46:17 * @FilePath: /potato/initialize/grpc_server.go */ package initialize @@ -18,16 +18,5 @@ func RunGrpcServer() *grpc.Server { pb.RegisterVendorServiceServer(server, basic.NewVendorServer()) reflection.Register(server) - // go func() { - // listen, err := net.Listen("tcp", ":"+convert.ToString(global.GO_CONFIG.Server.RpcPort)) - // if err != nil { - // global.GO_LOG.Sugar().Fatalf("net.Listen err: %v", err) - // } - - // err = server.Serve(listen) - // if err != nil { - // global.GO_LOG.Sugar().Fatalf("server.Serve err: %v", err) - // } - // }() return server } diff --git a/initialize/server.go b/initialize/server.go index a2178d6..0f44540 100644 --- a/initialize/server.go +++ b/initialize/server.go @@ -24,24 +24,5 @@ func RunServer(port string) *http.Server { MaxHeaderBytes: 1 << 20, } - // go func() { - // if err := server.ListenAndServe(); err != nil { - // global.GO_LOG.Info(fmt.Sprintf("Listen: %s\n", err)) - // } - // }() - - // quit := make(chan os.Signal) - // signal.Notify(quit, os.Interrupt) - // <-quit - - // global.GO_LOG.Info("Shutdown Server") - - // ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - // defer cancel() - // if err := server.Shutdown(ctx); err != nil { - // global.GO_LOG.Fatal(fmt.Sprintf("Server Shutdown: %v", err)) - // } - - // global.GO_LOG.Info("Server exiting") return server } diff --git a/main.go b/main.go index 28e4663..9323dbd 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ /* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy - * @LastEditTime: 2021-06-20 19:24:16 + * @LastEditTime: 2021-06-20 19:45:38 * @FilePath: /potato/main.go */ package main @@ -37,7 +37,7 @@ func main() { defer global.GO_DB.Close() defer global.GO_REDIS.Close() - flag.StringVar(&port, "port", convert.ToString(global.GO_CONFIG.Server.HttpPort), "启动端口号") + flag.StringVar(&port, "port", convert.ToString(global.GO_CONFIG.Server.Port), "启动端口号") flag.Parse() l, err := runTcpServer(port) From 3918c6726bf7e68bb7a0eeef2539443448c74cc4 Mon Sep 17 00:00:00 2001 From: viletyy Date: Sun, 20 Jun 2021 22:28:27 +0800 Subject: [PATCH 6/6] add: generate scripts --- initialize/http_server.go | 28 ++++++++++++++++++++++++++++ scripts/gen_basic_proto.sh | 9 +++++++++ scripts/gen_proto.sh | 9 +++++++++ 3 files changed, 46 insertions(+) create mode 100644 initialize/http_server.go create mode 100644 scripts/gen_basic_proto.sh create mode 100644 scripts/gen_proto.sh diff --git a/initialize/http_server.go b/initialize/http_server.go new file mode 100644 index 0000000..d5f3271 --- /dev/null +++ b/initialize/http_server.go @@ -0,0 +1,28 @@ +/* + * @Date: 2021-03-22 17:03:27 + * @LastEditors: viletyy + * @LastEditTime: 2021-06-20 22:22:10 + * @FilePath: /potato/initialize/http_server.go + */ +package initialize + +import ( + "net/http" + "time" + + "github.com/viletyy/potato/global" + "github.com/viletyy/potato/internal/routers" +) + +func RunHttpServer(port string) *http.Server { + router := routers.InitRouter() + server := &http.Server{ + Addr: port, + Handler: router, + ReadTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second, + WriteTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second, + MaxHeaderBytes: 1 << 20, + } + + return server +} diff --git a/scripts/gen_basic_proto.sh b/scripts/gen_basic_proto.sh new file mode 100644 index 0000000..c20e198 --- /dev/null +++ b/scripts/gen_basic_proto.sh @@ -0,0 +1,9 @@ +### + # @Date: 2021-06-20 21:43:20 + # @LastEditors: viletyy + # @LastEditTime: 2021-06-20 22:24:50 + # @FilePath: /potato/scripts/gen_basic_proto.sh +### +protoc --go_out=. --go_opt=paths=source_relative \ +--go-grpc_out=. --go-grpc_opt=paths=source_relative \ +./proto/basic/*.proto \ No newline at end of file diff --git a/scripts/gen_proto.sh b/scripts/gen_proto.sh new file mode 100644 index 0000000..c44aa75 --- /dev/null +++ b/scripts/gen_proto.sh @@ -0,0 +1,9 @@ +### + # @Date: 2021-06-20 21:43:20 + # @LastEditors: viletyy + # @LastEditTime: 2021-06-20 22:23:27 + # @FilePath: /potato/scripts/gen_proto.sh +### +protoc --go_out=. --go_opt=paths=source_relative \ +--go-grpc_out=. --go-grpc_opt=paths=source_relative \ +./proto/*.proto \ No newline at end of file