diff --git a/base.go b/base.go index 0b565ff..5fc4970 100644 --- a/base.go +++ b/base.go @@ -4,13 +4,40 @@ import ( "bytes" "compress/flate" "encoding/gob" + fmt "fmt" "log" "math/rand" "os" + "reflect" "github.com/tecbot/gorocksdb" ) +type KeyKind struct { + Key string + FieldName string +} + +func NewKeyKind(key string, fieldname string) KeyKind { + return KeyKind{Key: key, FieldName: fieldname} +} + +type KeyList struct { + Keys []interface{} +} + +func (kl *KeyList) GetKeys() []interface{} { + return kl.Keys +} + +func (kl *KeyList) AppendKey(key interface{}) { + kl.Keys = append(kl.Keys, key) +} + +func (kl *KeyList) GetLength() int { + return len(kl.Keys) +} + // CheckErrorPanic 查错误 存在就panic操作 func CheckErrorPanic(err error) { if err != nil { @@ -45,43 +72,67 @@ var ropts = gorocksdb.NewDefaultReadOptions() // IKeyList key list interface type IKeyList interface { - AppendKey(key []byte) - GetKeyList() [][]byte + AppendKey(key interface{}) + GetKeys() []interface{} GetLength() int } -// SaveGobFromRocksdb save data from rocksdb keyseek -func SaveGobFromRocksdb(fname string, seekKey string, v IKeyList) { - i := 0 - iter := db.NewIterator(ropts) - iter.Seek([]byte(seekKey)) - for ; iter.Valid(); iter.Next() { - i++ - // if i%1000 == 0 { - // t.Error(string(iter.Key().Data())) - // t.Error(string(iter.Value().Data())) - - // } - v.AppendKey(iter.Value().Data()) - } - log.Println(i) - +// SaveData 存数据 +func SaveData(fname string, v interface{}) { var keybuf = &bytes.Buffer{} enc := gob.NewEncoder(keybuf) enc.Encode(v) - log.Println((len(keybuf.Bytes()))) - f, err := os.OpenFile(fname, os.O_CREATE|os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm) + f, err := os.OpenFile(fname, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm) CheckErrorPanic(err) - gw, err := flate.NewWriter(f, 5) + gw, err := flate.NewWriter(f, 7) CheckErrorPanic(err) defer gw.Close() gw.Write(keybuf.Bytes()) } +func SaveGob(fname string, datatype reflect.Type, kfs ...KeyKind) { + + // cl := &CountryList{} + kl := &KeyList{} + + i := 0 + iter := db.NewIterator(ropts) + bkey := []byte(kfs[0].Key) + iter.Seek(bkey) + + for ; iter.ValidForPrefix(bkey); iter.Next() { + i++ + first := iter.Value().Data() + data := reflect.New(datatype).Elem() + + data.FieldByName(kfs[0].FieldName).Set(reflect.ValueOf(first)) + + // country := &Country{} + // country.Name = name + for n := 1; n < len(kfs); n++ { + v, err := db.Get(ropts, []byte(fmt.Sprintf(kfs[n].Key, first))) + if err != nil { + panic(err) + } + // t.Error(string(iter.Key().Data()), len(pic.Data())) + // country.Pic = pic.Data() + fv := data.FieldByName(kfs[n].FieldName) + fv.Set(reflect.ValueOf(v.Data())) + // cl.KeyList = append(cl.KeyList, country) + + } + kl.AppendKey(data.Interface()) + } + // t.Error(i) + // // SaveData("./data/country.gob", cl) + SaveData(fname, kl) + // t.Error() +} + // LoadGob load gob from file func LoadGob(fname string, v IKeyList) { f, err := os.Open(fname) @@ -92,6 +143,6 @@ func LoadGob(fname string, v IKeyList) { } // GetRandomKey get ikeylist key by random -func GetRandomKey(v IKeyList) []byte { - return v.GetKeyList()[rand.Intn(v.GetLength())] +func GetRandomKey(v IKeyList) interface{} { + return v.GetKeys()[rand.Intn(v.GetLength())] } diff --git a/base.pb.go b/base.pb.go new file mode 100644 index 0000000..9408943 --- /dev/null +++ b/base.pb.go @@ -0,0 +1,200 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.22.0 +// protoc (unknown) +// source: base.proto + +package main + +import ( + proto "github.com/golang/protobuf/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) +) + +// This is a compile-time assertion that a sufficiently up-to-date version +// of the legacy proto package is being used. +const _ = proto.ProtoPackageIsVersion4 + +// The request message containing the user's name. +type Request struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *Request) Reset() { + *x = Request{} + if protoimpl.UnsafeEnabled { + mi := &file_base_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Request) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Request) ProtoMessage() {} + +func (x *Request) ProtoReflect() protoreflect.Message { + mi := &file_base_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 Request.ProtoReflect.Descriptor instead. +func (*Request) Descriptor() ([]byte, []int) { + return file_base_proto_rawDescGZIP(), []int{0} +} + +// The response message containing the greetings +type Reply struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` +} + +func (x *Reply) Reset() { + *x = Reply{} + if protoimpl.UnsafeEnabled { + mi := &file_base_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Reply) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Reply) ProtoMessage() {} + +func (x *Reply) ProtoReflect() protoreflect.Message { + mi := &file_base_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 Reply.ProtoReflect.Descriptor instead. +func (*Reply) Descriptor() ([]byte, []int) { + return file_base_proto_rawDescGZIP(), []int{1} +} + +func (x *Reply) GetMessage() string { + if x != nil { + return x.Message + } + return "" +} + +var File_base_proto protoreflect.FileDescriptor + +var file_base_proto_rawDesc = []byte{ + 0x0a, 0x0a, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x6d, 0x61, + 0x69, 0x6e, 0x22, 0x09, 0x0a, 0x07, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x21, 0x0a, + 0x05, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, + 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, + 0x42, 0x08, 0x5a, 0x06, 0x2e, 0x3b, 0x6d, 0x61, 0x69, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, +} + +var ( + file_base_proto_rawDescOnce sync.Once + file_base_proto_rawDescData = file_base_proto_rawDesc +) + +func file_base_proto_rawDescGZIP() []byte { + file_base_proto_rawDescOnce.Do(func() { + file_base_proto_rawDescData = protoimpl.X.CompressGZIP(file_base_proto_rawDescData) + }) + return file_base_proto_rawDescData +} + +var file_base_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_base_proto_goTypes = []interface{}{ + (*Request)(nil), // 0: main.Request + (*Reply)(nil), // 1: main.Reply +} +var file_base_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_base_proto_init() } +func file_base_proto_init() { + if File_base_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_base_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Request); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_base_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Reply); 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_base_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_base_proto_goTypes, + DependencyIndexes: file_base_proto_depIdxs, + MessageInfos: file_base_proto_msgTypes, + }.Build() + File_base_proto = out.File + file_base_proto_rawDesc = nil + file_base_proto_goTypes = nil + file_base_proto_depIdxs = nil +} diff --git a/case_test.go b/case_test.go index 3052246..7e371c8 100644 --- a/case_test.go +++ b/case_test.go @@ -1,18 +1,47 @@ package main import ( + "encoding/gob" + "os" "testing" ) -func TestCheck(t *testing.T) { - // firstname lastname - fn := &FirstNameList{} - SaveGobFromRocksdb("./data/firstname.gob", "firstname-", fn) +// func TestCheckCountry2(t *testing.T) { +// gob.Register(Country{}) + +// SaveGob("./data/country.gob", reflect.TypeOf(Country{}), NewKeyKind("country-name-", "Name"), NewKeyKind("country-pic-%s", "Pic")) +// } + +// func TestLastName(t *testing.T) { +// gob.Register(Country{}) +// ln := &KeyList{} +// LoadGob("./data/lastname.gob", ln) +// t.Error(ln.GetLength(), string(ln.Keys[0].([]byte))) +// nln := KeyList{} +// for _, buf := range ln.Keys { +// lnn := LastName{} +// lnn.Name = buf.([]byte) +// nln.AppendKey(lnn) +// } +// SaveData("./data/lastname-test.gob", nln) +// } + +func TestS(t *testing.T) { + gob.Register(Country{}) + ln := &KeyList{} + LoadGob("./data/lastname1.gob", ln) + t.Error(len(ln.Keys)) + country := ln.Keys[0].(Country) + f, _ := os.OpenFile("./1.png", os.O_CREATE|os.O_TRUNC|os.O_RDWR, os.ModePerm) + f.Write(country.Pic) } func TestReadData(t *testing.T) { - - ln := &FirstNameList{} - LoadGob("./data/firstname.gob", ln) - t.Error(len(ln.KeyList)) + gob.Register(Country{}) + ln := &KeyList{} + LoadGob("./data/country.gob", ln) + t.Error(len(ln.Keys)) + country := ln.Keys[0].(Country) + f, _ := os.OpenFile("./1.png", os.O_CREATE|os.O_TRUNC|os.O_RDWR, os.ModePerm) + f.Write(country.Pic) } diff --git a/country.go b/country.go new file mode 100644 index 0000000..fddf225 --- /dev/null +++ b/country.go @@ -0,0 +1,36 @@ +package main + +import ( + context "context" + "encoding/gob" +) + +// Country struct +type Country struct { + Pic []byte + Name []byte +} + +var countrylist *KeyList = &KeyList{} + +func init() { + gob.Register(Country{}) + LoadGob("./data/country.gob", countrylist) +} + +type countryserver struct { +} + +func (s *countryserver) Name(cxt context.Context, request *Request) (*Reply, error) { + + reply := &Reply{} + reply.Message = string(GetRandomKey(countrylist).(Country).Name) + return reply, nil +} + +func (s *countryserver) Picture(cxt context.Context, request *Request) (*CountryReply, error) { + + reply := &CountryReply{} + reply.Image = GetRandomKey(countrylist).(Country).Pic + return reply, nil +} diff --git a/country.pb.go b/country.pb.go new file mode 100644 index 0000000..539b3ea --- /dev/null +++ b/country.pb.go @@ -0,0 +1,290 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.22.0 +// protoc (unknown) +// source: country.proto + +package main + +import ( + context "context" + proto "github.com/golang/protobuf/proto" + _ "google.golang.org/genproto/googleapis/api/annotations" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + 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) +) + +// This is a compile-time assertion that a sufficiently up-to-date version +// of the legacy proto package is being used. +const _ = proto.ProtoPackageIsVersion4 + +// The response message containing the greetings +type CountryReply struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Image []byte `protobuf:"bytes,1,opt,name=image,proto3" json:"image,omitempty"` +} + +func (x *CountryReply) Reset() { + *x = CountryReply{} + if protoimpl.UnsafeEnabled { + mi := &file_country_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *CountryReply) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*CountryReply) ProtoMessage() {} + +func (x *CountryReply) ProtoReflect() protoreflect.Message { + mi := &file_country_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 CountryReply.ProtoReflect.Descriptor instead. +func (*CountryReply) Descriptor() ([]byte, []int) { + return file_country_proto_rawDescGZIP(), []int{0} +} + +func (x *CountryReply) GetImage() []byte { + if x != nil { + return x.Image + } + return nil +} + +var File_country_proto protoreflect.FileDescriptor + +var file_country_proto_rawDesc = []byte{ + 0x0a, 0x0d, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, + 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x1a, 0x0a, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x1a, 0x1c, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x61, 0x6e, + 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, + 0x24, 0x0a, 0x0c, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, + 0x14, 0x0a, 0x05, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, + 0x69, 0x6d, 0x61, 0x67, 0x65, 0x32, 0x9d, 0x01, 0x0a, 0x07, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x72, + 0x79, 0x12, 0x3f, 0x0a, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x0d, 0x2e, 0x6d, 0x61, 0x69, 0x6e, + 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0b, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, + 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x1b, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x15, 0x22, 0x10, 0x2f, + 0x76, 0x31, 0x2f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x2f, 0x6e, 0x61, 0x6d, 0x65, 0x3a, + 0x01, 0x2a, 0x12, 0x51, 0x0a, 0x07, 0x50, 0x69, 0x63, 0x74, 0x75, 0x72, 0x65, 0x12, 0x0d, 0x2e, + 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x6d, + 0x61, 0x69, 0x6e, 0x2e, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x65, 0x70, 0x6c, 0x79, + 0x22, 0x23, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x1d, 0x22, 0x18, 0x2f, 0x76, 0x31, 0x2f, 0x63, 0x6f, + 0x75, 0x6e, 0x74, 0x72, 0x79, 0x2f, 0x6e, 0x61, 0x6d, 0x65, 0x2f, 0x70, 0x69, 0x63, 0x74, 0x75, + 0x72, 0x65, 0x3a, 0x01, 0x2a, 0x42, 0x08, 0x5a, 0x06, 0x2e, 0x3b, 0x6d, 0x61, 0x69, 0x6e, 0x62, + 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_country_proto_rawDescOnce sync.Once + file_country_proto_rawDescData = file_country_proto_rawDesc +) + +func file_country_proto_rawDescGZIP() []byte { + file_country_proto_rawDescOnce.Do(func() { + file_country_proto_rawDescData = protoimpl.X.CompressGZIP(file_country_proto_rawDescData) + }) + return file_country_proto_rawDescData +} + +var file_country_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_country_proto_goTypes = []interface{}{ + (*CountryReply)(nil), // 0: main.CountryReply + (*Request)(nil), // 1: main.Request + (*Reply)(nil), // 2: main.Reply +} +var file_country_proto_depIdxs = []int32{ + 1, // 0: main.Country.Name:input_type -> main.Request + 1, // 1: main.Country.Picture:input_type -> main.Request + 2, // 2: main.Country.Name:output_type -> main.Reply + 0, // 3: main.Country.Picture:output_type -> main.CountryReply + 2, // [2:4] is the sub-list for method output_type + 0, // [0:2] 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_country_proto_init() } +func file_country_proto_init() { + if File_country_proto != nil { + return + } + file_base_proto_init() + if !protoimpl.UnsafeEnabled { + file_country_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*CountryReply); 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_country_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_country_proto_goTypes, + DependencyIndexes: file_country_proto_depIdxs, + MessageInfos: file_country_proto_msgTypes, + }.Build() + File_country_proto = out.File + file_country_proto_rawDesc = nil + file_country_proto_goTypes = nil + file_country_proto_depIdxs = nil +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConnInterface + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion6 + +// CountryClient is the client API for Country service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type CountryClient interface { + // Sends a greeting + Name(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error) + Picture(ctx context.Context, in *Request, opts ...grpc.CallOption) (*CountryReply, error) +} + +type countryClient struct { + cc grpc.ClientConnInterface +} + +func NewCountryClient(cc grpc.ClientConnInterface) CountryClient { + return &countryClient{cc} +} + +func (c *countryClient) Name(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error) { + out := new(Reply) + err := c.cc.Invoke(ctx, "/main.Country/Name", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *countryClient) Picture(ctx context.Context, in *Request, opts ...grpc.CallOption) (*CountryReply, error) { + out := new(CountryReply) + err := c.cc.Invoke(ctx, "/main.Country/Picture", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// CountryServer is the server API for Country service. +type CountryServer interface { + // Sends a greeting + Name(context.Context, *Request) (*Reply, error) + Picture(context.Context, *Request) (*CountryReply, error) +} + +// UnimplementedCountryServer can be embedded to have forward compatible implementations. +type UnimplementedCountryServer struct { +} + +func (*UnimplementedCountryServer) Name(context.Context, *Request) (*Reply, error) { + return nil, status.Errorf(codes.Unimplemented, "method Name not implemented") +} +func (*UnimplementedCountryServer) Picture(context.Context, *Request) (*CountryReply, error) { + return nil, status.Errorf(codes.Unimplemented, "method Picture not implemented") +} + +func RegisterCountryServer(s *grpc.Server, srv CountryServer) { + s.RegisterService(&_Country_serviceDesc, srv) +} + +func _Country_Name_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(Request) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(CountryServer).Name(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/main.Country/Name", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(CountryServer).Name(ctx, req.(*Request)) + } + return interceptor(ctx, in, info, handler) +} + +func _Country_Picture_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(Request) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(CountryServer).Picture(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/main.Country/Picture", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(CountryServer).Picture(ctx, req.(*Request)) + } + return interceptor(ctx, in, info, handler) +} + +var _Country_serviceDesc = grpc.ServiceDesc{ + ServiceName: "main.Country", + HandlerType: (*CountryServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Name", + Handler: _Country_Name_Handler, + }, + { + MethodName: "Picture", + Handler: _Country_Picture_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "country.proto", +} diff --git a/country.pb.gw.go b/country.pb.gw.go new file mode 100644 index 0000000..8a3c594 --- /dev/null +++ b/country.pb.gw.go @@ -0,0 +1,241 @@ +// Code generated by protoc-gen-grpc-gateway. DO NOT EDIT. +// source: country.proto + +/* +Package main is a reverse proxy. + +It translates gRPC into RESTful JSON APIs. +*/ +package main + +import ( + "context" + "io" + "net/http" + + "github.com/golang/protobuf/descriptor" + "github.com/golang/protobuf/proto" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/grpc-ecosystem/grpc-gateway/utilities" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/grpclog" + "google.golang.org/grpc/status" +) + +// Suppress "imported and not used" errors +var _ codes.Code +var _ io.Reader +var _ status.Status +var _ = runtime.String +var _ = utilities.NewDoubleArray +var _ = descriptor.ForMessage + +func request_Country_Name_0(ctx context.Context, marshaler runtime.Marshaler, client CountryClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq Request + var metadata runtime.ServerMetadata + + newReader, berr := utilities.IOReaderFactory(req.Body) + if berr != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr) + } + if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.Name(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_Country_Name_0(ctx context.Context, marshaler runtime.Marshaler, server CountryServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq Request + var metadata runtime.ServerMetadata + + newReader, berr := utilities.IOReaderFactory(req.Body) + if berr != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr) + } + if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := server.Name(ctx, &protoReq) + return msg, metadata, err + +} + +func request_Country_Picture_0(ctx context.Context, marshaler runtime.Marshaler, client CountryClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq Request + var metadata runtime.ServerMetadata + + newReader, berr := utilities.IOReaderFactory(req.Body) + if berr != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr) + } + if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.Picture(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_Country_Picture_0(ctx context.Context, marshaler runtime.Marshaler, server CountryServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq Request + var metadata runtime.ServerMetadata + + newReader, berr := utilities.IOReaderFactory(req.Body) + if berr != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr) + } + if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := server.Picture(ctx, &protoReq) + return msg, metadata, err + +} + +// RegisterCountryHandlerServer registers the http handlers for service Country to "mux". +// UnaryRPC :call CountryServer directly. +// StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906. +func RegisterCountryHandlerServer(ctx context.Context, mux *runtime.ServeMux, server CountryServer) error { + + mux.Handle("POST", pattern_Country_Name_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateIncomingContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_Country_Name_0(rctx, inboundMarshaler, server, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Country_Name_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("POST", pattern_Country_Picture_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateIncomingContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_Country_Picture_0(rctx, inboundMarshaler, server, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Country_Picture_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +// RegisterCountryHandlerFromEndpoint is same as RegisterCountryHandler but +// automatically dials to "endpoint" and closes the connection when "ctx" gets done. +func RegisterCountryHandlerFromEndpoint(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error) { + conn, err := grpc.Dial(endpoint, opts...) + if err != nil { + return err + } + defer func() { + if err != nil { + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + return + } + go func() { + <-ctx.Done() + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + }() + }() + + return RegisterCountryHandler(ctx, mux, conn) +} + +// RegisterCountryHandler registers the http handlers for service Country to "mux". +// The handlers forward requests to the grpc endpoint over "conn". +func RegisterCountryHandler(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error { + return RegisterCountryHandlerClient(ctx, mux, NewCountryClient(conn)) +} + +// RegisterCountryHandlerClient registers the http handlers for service Country +// to "mux". The handlers forward requests to the grpc endpoint over the given implementation of "CountryClient". +// Note: the gRPC framework executes interceptors within the gRPC handler. If the passed in "CountryClient" +// doesn't go through the normal gRPC flow (creating a gRPC client etc.) then it will be up to the passed in +// "CountryClient" to call the correct interceptors. +func RegisterCountryHandlerClient(ctx context.Context, mux *runtime.ServeMux, client CountryClient) error { + + mux.Handle("POST", pattern_Country_Name_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Country_Name_0(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Country_Name_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("POST", pattern_Country_Picture_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Country_Picture_0(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Country_Picture_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +var ( + pattern_Country_Name_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"v1", "country", "name"}, "", runtime.AssumeColonVerbOpt(true))) + + pattern_Country_Picture_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"v1", "country", "name", "picture"}, "", runtime.AssumeColonVerbOpt(true))) +) + +var ( + forward_Country_Name_0 = runtime.ForwardResponseMessage + + forward_Country_Picture_0 = runtime.ForwardResponseMessage +) diff --git a/data_regob_test.go b/data_regob_test.go new file mode 100644 index 0000000..4156f5b --- /dev/null +++ b/data_regob_test.go @@ -0,0 +1,7 @@ +package main + +import "testing" + +func TestReGob(t *testing.T) { + +} diff --git a/go.mod b/go.mod index f81c6d2..13ff68b 100644 --- a/go.mod +++ b/go.mod @@ -13,4 +13,5 @@ require ( github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c google.golang.org/genproto v0.0.0-20200511104702-f5ebc3bea380 google.golang.org/grpc v1.29.1 + google.golang.org/protobuf v1.22.0 ) diff --git a/main.go b/main.go index 6b73adc..bc7e16d 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/gob" "log" "math/rand" "net/http" @@ -11,18 +12,19 @@ import ( "github.com/grpc-ecosystem/grpc-gateway/runtime" ) -var fnl *FirstNameList = &FirstNameList{} -var lnl *LastNameList = &LastNameList{} +var cl *KeyList = &KeyList{} func init() { + + gob.Register(KeyList{}) + + gob.Register(Country{}) + f, err := os.OpenFile("./my.log", os.O_CREATE|os.O_RDWR, os.ModePerm) CheckErrorPanic(err) log.SetOutput(f) rand.Seed(time.Now().UnixNano()) - LoadGob("./data/firstname.gob", fnl) - - LoadGob("./data/lastname.gob", lnl) } @@ -35,8 +37,12 @@ func main() { // gserver := grpc.NewServer() mux := runtime.NewServeMux() - s := &nameserver{} - // RegisterNameServer(gserver, s) - RegisterNameHandlerServer(ctx, mux, s) + ns := &nameserver{} + cs := &countryserver{} + + RegisterNameHandlerServer(ctx, mux, ns) + RegisterCountryHandlerServer(ctx, mux, cs) + log.Fatal(http.ListenAndServe(":4433", mux)) + } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..ef64976 --- /dev/null +++ b/main_test.go @@ -0,0 +1,7 @@ +package main + +import "testing" + +func TestMain(t *testing.T) { + main() +} diff --git a/name_service.go b/name_service.go index 58e8440..2402e08 100644 --- a/name_service.go +++ b/name_service.go @@ -2,74 +2,51 @@ package main import ( "context" + "encoding/gob" + "log" ) -type LastNameList struct { - KeyList [][]byte +type LastName struct { + Name []byte } -func (ln *LastNameList) GetKeyList() [][]byte { - return ln.KeyList +type FirstName struct { + Name []byte } -func (ln *LastNameList) AppendKey(key []byte) { - ln.KeyList = append(ln.KeyList, key) -} +var fnl *KeyList = &KeyList{} +var lnl *KeyList = &KeyList{} -func (ln *LastNameList) GetLength() int { - return len(ln.KeyList) -} +func init() { + gob.Register(LastName{}) + gob.Register(FirstName{}) -type FirstNameList struct { - KeyList [][]byte -} - -func (fn *FirstNameList) GetKeyList() [][]byte { - return fn.KeyList -} - -func (fn *FirstNameList) AppendKey(key []byte) { - fn.KeyList = append(fn.KeyList, key) -} - -func (fn *FirstNameList) GetLength() int { - return len(fn.KeyList) + LoadGob("./data/firstname.gob", fnl) + LoadGob("./data/lastname.gob", lnl) } type nameserver struct { } -func (s *nameserver) FirstName(cxt context.Context, request *NameRequest) (*NameReply, error) { +func (s *nameserver) FirstName(cxt context.Context, request *Request) (*Reply, error) { - reply := &NameReply{} - reply.Message = string(GetRandomKey(fnl)) - - // switch request.GetNametype() { - // case NameType_FullName: - // var fullname []byte - // fullname = append(fullname, GetRandomKey(fnl)...) - // fullname = append(fullname, GetRandomKey(lnl)...) - // reply.Message = string(fullname) - - // case NameType_FirstName: - // reply.Message = string(GetRandomKey(fnl)) - // case NameType_LastName: - // reply.Message = string(GetRandomKey(lnl)) - // } + reply := &Reply{} + reply.Message = string(GetRandomKey(fnl).(FirstName).Name) return reply, nil } -func (s *nameserver) LastName(cxt context.Context, request *NameRequest) (*NameReply, error) { - reply := &NameReply{} - reply.Message = string(GetRandomKey(lnl)) +func (s *nameserver) LastName(cxt context.Context, request *Request) (*Reply, error) { + reply := &Reply{} + log.Println(len(lnl.Keys)) + reply.Message = string(GetRandomKey(lnl).(LastName).Name) return reply, nil } -func (s *nameserver) FullName(cxt context.Context, request *NameRequest) (*NameReply, error) { - reply := &NameReply{} +func (s *nameserver) FullName(cxt context.Context, request *Request) (*Reply, error) { + reply := &Reply{} var fullname []byte - fullname = append(fullname, GetRandomKey(fnl)...) - fullname = append(fullname, GetRandomKey(lnl)...) + fullname = append(fullname, GetRandomKey(fnl).(FirstName).Name...) + fullname = append(fullname, GetRandomKey(lnl).(LastName).Name...) reply.Message = string(fullname) return reply, nil } diff --git a/people.pb.go b/people.pb.go index 5f20678..40c54f2 100644 --- a/people.pb.go +++ b/people.pb.go @@ -1,127 +1,99 @@ // Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.22.0 +// protoc (unknown) // source: people.proto package main import ( context "context" - fmt "fmt" proto "github.com/golang/protobuf/proto" _ "google.golang.org/genproto/googleapis/api/annotations" grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" - math "math" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" ) -// Reference imports to suppress errors if they are not otherwise used. -var _ = proto.Marshal -var _ = fmt.Errorf -var _ = math.Inf +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) +) -// This is a compile-time assertion to ensure that this generated file -// is compatible with the proto package it is being compiled against. -// A compilation error at this line likely means your copy of the -// proto package needs to be updated. -const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package +// This is a compile-time assertion that a sufficiently up-to-date version +// of the legacy proto package is being used. +const _ = proto.ProtoPackageIsVersion4 -// The request message containing the user's name. -type NameRequest struct { - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` +var File_people_proto protoreflect.FileDescriptor + +var file_people_proto_rawDesc = []byte{ + 0x0a, 0x0c, 0x70, 0x65, 0x6f, 0x70, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, + 0x6d, 0x61, 0x69, 0x6e, 0x1a, 0x1c, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x61, 0x70, 0x69, + 0x2f, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x1a, 0x0a, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x32, 0xda, + 0x01, 0x0a, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x46, 0x0a, 0x09, 0x46, 0x69, 0x72, 0x73, 0x74, + 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x0d, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x1a, 0x0b, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x52, 0x65, 0x70, 0x6c, 0x79, + 0x22, 0x1d, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x17, 0x22, 0x12, 0x2f, 0x76, 0x31, 0x2f, 0x6e, 0x61, + 0x6d, 0x65, 0x2f, 0x66, 0x69, 0x72, 0x73, 0x74, 0x6e, 0x61, 0x6d, 0x65, 0x3a, 0x01, 0x2a, 0x12, + 0x44, 0x0a, 0x08, 0x4c, 0x61, 0x73, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x0d, 0x2e, 0x6d, 0x61, + 0x69, 0x6e, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0b, 0x2e, 0x6d, 0x61, 0x69, + 0x6e, 0x2e, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x1c, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x16, 0x22, + 0x11, 0x2f, 0x76, 0x31, 0x2f, 0x6e, 0x61, 0x6d, 0x65, 0x2f, 0x6c, 0x61, 0x73, 0x74, 0x6e, 0x61, + 0x6d, 0x65, 0x3a, 0x01, 0x2a, 0x12, 0x44, 0x0a, 0x08, 0x46, 0x75, 0x6c, 0x6c, 0x4e, 0x61, 0x6d, + 0x65, 0x12, 0x0d, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x1a, 0x0b, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x1c, 0x82, + 0xd3, 0xe4, 0x93, 0x02, 0x16, 0x22, 0x11, 0x2f, 0x76, 0x31, 0x2f, 0x6e, 0x61, 0x6d, 0x65, 0x2f, + 0x66, 0x75, 0x6c, 0x6c, 0x6e, 0x61, 0x6d, 0x65, 0x3a, 0x01, 0x2a, 0x42, 0x08, 0x5a, 0x06, 0x2e, + 0x3b, 0x6d, 0x61, 0x69, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } -func (m *NameRequest) Reset() { *m = NameRequest{} } -func (m *NameRequest) String() string { return proto.CompactTextString(m) } -func (*NameRequest) ProtoMessage() {} -func (*NameRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_09461903b56db210, []int{0} +var file_people_proto_goTypes = []interface{}{ + (*Request)(nil), // 0: main.Request + (*Reply)(nil), // 1: main.Reply +} +var file_people_proto_depIdxs = []int32{ + 0, // 0: main.Name.FirstName:input_type -> main.Request + 0, // 1: main.Name.LastName:input_type -> main.Request + 0, // 2: main.Name.FullName:input_type -> main.Request + 1, // 3: main.Name.FirstName:output_type -> main.Reply + 1, // 4: main.Name.LastName:output_type -> main.Reply + 1, // 5: main.Name.FullName:output_type -> main.Reply + 3, // [3:6] is the sub-list for method output_type + 0, // [0:3] 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 (m *NameRequest) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_NameRequest.Unmarshal(m, b) -} -func (m *NameRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_NameRequest.Marshal(b, m, deterministic) -} -func (m *NameRequest) XXX_Merge(src proto.Message) { - xxx_messageInfo_NameRequest.Merge(m, src) -} -func (m *NameRequest) XXX_Size() int { - return xxx_messageInfo_NameRequest.Size(m) -} -func (m *NameRequest) XXX_DiscardUnknown() { - xxx_messageInfo_NameRequest.DiscardUnknown(m) -} - -var xxx_messageInfo_NameRequest proto.InternalMessageInfo - -// The response message containing the greetings -type NameReply struct { - Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` -} - -func (m *NameReply) Reset() { *m = NameReply{} } -func (m *NameReply) String() string { return proto.CompactTextString(m) } -func (*NameReply) ProtoMessage() {} -func (*NameReply) Descriptor() ([]byte, []int) { - return fileDescriptor_09461903b56db210, []int{1} -} - -func (m *NameReply) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_NameReply.Unmarshal(m, b) -} -func (m *NameReply) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_NameReply.Marshal(b, m, deterministic) -} -func (m *NameReply) XXX_Merge(src proto.Message) { - xxx_messageInfo_NameReply.Merge(m, src) -} -func (m *NameReply) XXX_Size() int { - return xxx_messageInfo_NameReply.Size(m) -} -func (m *NameReply) XXX_DiscardUnknown() { - xxx_messageInfo_NameReply.DiscardUnknown(m) -} - -var xxx_messageInfo_NameReply proto.InternalMessageInfo - -func (m *NameReply) GetMessage() string { - if m != nil { - return m.Message +func init() { file_people_proto_init() } +func file_people_proto_init() { + if File_people_proto != nil { + return } - return "" -} - -func init() { - proto.RegisterType((*NameRequest)(nil), "main.NameRequest") - proto.RegisterType((*NameReply)(nil), "main.NameReply") -} - -func init() { - proto.RegisterFile("people.proto", fileDescriptor_09461903b56db210) -} - -var fileDescriptor_09461903b56db210 = []byte{ - // 220 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x29, 0x48, 0xcd, 0x2f, - 0xc8, 0x49, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xc9, 0x4d, 0xcc, 0xcc, 0x93, 0x92, - 0x49, 0xcf, 0xcf, 0x4f, 0xcf, 0x49, 0xd5, 0x4f, 0x2c, 0xc8, 0xd4, 0x4f, 0xcc, 0xcb, 0xcb, 0x2f, - 0x49, 0x2c, 0xc9, 0xcc, 0xcf, 0x2b, 0x86, 0xa8, 0x51, 0xe2, 0xe5, 0xe2, 0xf6, 0x4b, 0xcc, 0x4d, - 0x0d, 0x4a, 0x2d, 0x2c, 0x4d, 0x2d, 0x2e, 0x51, 0x52, 0xe5, 0xe2, 0x84, 0x70, 0x0b, 0x72, 0x2a, - 0x85, 0x24, 0xb8, 0xd8, 0x73, 0x53, 0x8b, 0x8b, 0x13, 0xd3, 0x53, 0x25, 0x18, 0x15, 0x18, 0x35, - 0x38, 0x83, 0x60, 0x5c, 0xa3, 0x4f, 0x8c, 0x5c, 0x2c, 0x20, 0x75, 0x42, 0x7e, 0x5c, 0x9c, 0x6e, - 0x99, 0x45, 0xc5, 0x25, 0x60, 0x8e, 0xa0, 0x1e, 0xc8, 0x42, 0x3d, 0x24, 0xf3, 0xa4, 0xf8, 0x91, - 0x85, 0x0a, 0x72, 0x2a, 0x95, 0x64, 0x9b, 0x2e, 0x3f, 0x99, 0xcc, 0x24, 0xae, 0x24, 0xa4, 0x5f, - 0x66, 0xa8, 0x9f, 0x97, 0x98, 0x9b, 0xaa, 0x9f, 0x06, 0xd2, 0x0f, 0x62, 0x59, 0x31, 0x6a, 0x09, - 0xf9, 0x70, 0x71, 0xf8, 0x24, 0x92, 0x60, 0x9c, 0x0c, 0xd8, 0x38, 0x31, 0x25, 0x41, 0xb8, 0x71, - 0x39, 0x89, 0x28, 0xa6, 0xb9, 0x95, 0xe6, 0xe4, 0x50, 0x60, 0x5a, 0x5a, 0x69, 0x4e, 0x0e, 0xd4, - 0x34, 0x27, 0x8e, 0x28, 0x36, 0x3d, 0x6b, 0x90, 0x8e, 0x24, 0x36, 0x70, 0xd8, 0x19, 0x03, 0x02, - 0x00, 0x00, 0xff, 0xff, 0x59, 0xe5, 0x6d, 0xa1, 0x6f, 0x01, 0x00, 0x00, + file_base_proto_init() + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_people_proto_rawDesc, + NumEnums: 0, + NumMessages: 0, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_people_proto_goTypes, + DependencyIndexes: file_people_proto_depIdxs, + }.Build() + File_people_proto = out.File + file_people_proto_rawDesc = nil + file_people_proto_goTypes = nil + file_people_proto_depIdxs = nil } // Reference imports to suppress errors if they are not otherwise used. @@ -137,9 +109,9 @@ const _ = grpc.SupportPackageIsVersion6 // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. type NameClient interface { // Sends a greeting - FirstName(ctx context.Context, in *NameRequest, opts ...grpc.CallOption) (*NameReply, error) - LastName(ctx context.Context, in *NameRequest, opts ...grpc.CallOption) (*NameReply, error) - FullName(ctx context.Context, in *NameRequest, opts ...grpc.CallOption) (*NameReply, error) + FirstName(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error) + LastName(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error) + FullName(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error) } type nameClient struct { @@ -150,8 +122,8 @@ func NewNameClient(cc grpc.ClientConnInterface) NameClient { return &nameClient{cc} } -func (c *nameClient) FirstName(ctx context.Context, in *NameRequest, opts ...grpc.CallOption) (*NameReply, error) { - out := new(NameReply) +func (c *nameClient) FirstName(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error) { + out := new(Reply) err := c.cc.Invoke(ctx, "/main.Name/FirstName", in, out, opts...) if err != nil { return nil, err @@ -159,8 +131,8 @@ func (c *nameClient) FirstName(ctx context.Context, in *NameRequest, opts ...grp return out, nil } -func (c *nameClient) LastName(ctx context.Context, in *NameRequest, opts ...grpc.CallOption) (*NameReply, error) { - out := new(NameReply) +func (c *nameClient) LastName(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error) { + out := new(Reply) err := c.cc.Invoke(ctx, "/main.Name/LastName", in, out, opts...) if err != nil { return nil, err @@ -168,8 +140,8 @@ func (c *nameClient) LastName(ctx context.Context, in *NameRequest, opts ...grpc return out, nil } -func (c *nameClient) FullName(ctx context.Context, in *NameRequest, opts ...grpc.CallOption) (*NameReply, error) { - out := new(NameReply) +func (c *nameClient) FullName(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error) { + out := new(Reply) err := c.cc.Invoke(ctx, "/main.Name/FullName", in, out, opts...) if err != nil { return nil, err @@ -180,22 +152,22 @@ func (c *nameClient) FullName(ctx context.Context, in *NameRequest, opts ...grpc // NameServer is the server API for Name service. type NameServer interface { // Sends a greeting - FirstName(context.Context, *NameRequest) (*NameReply, error) - LastName(context.Context, *NameRequest) (*NameReply, error) - FullName(context.Context, *NameRequest) (*NameReply, error) + FirstName(context.Context, *Request) (*Reply, error) + LastName(context.Context, *Request) (*Reply, error) + FullName(context.Context, *Request) (*Reply, error) } // UnimplementedNameServer can be embedded to have forward compatible implementations. type UnimplementedNameServer struct { } -func (*UnimplementedNameServer) FirstName(ctx context.Context, req *NameRequest) (*NameReply, error) { +func (*UnimplementedNameServer) FirstName(context.Context, *Request) (*Reply, error) { return nil, status.Errorf(codes.Unimplemented, "method FirstName not implemented") } -func (*UnimplementedNameServer) LastName(ctx context.Context, req *NameRequest) (*NameReply, error) { +func (*UnimplementedNameServer) LastName(context.Context, *Request) (*Reply, error) { return nil, status.Errorf(codes.Unimplemented, "method LastName not implemented") } -func (*UnimplementedNameServer) FullName(ctx context.Context, req *NameRequest) (*NameReply, error) { +func (*UnimplementedNameServer) FullName(context.Context, *Request) (*Reply, error) { return nil, status.Errorf(codes.Unimplemented, "method FullName not implemented") } @@ -204,7 +176,7 @@ func RegisterNameServer(s *grpc.Server, srv NameServer) { } func _Name_FirstName_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(NameRequest) + in := new(Request) if err := dec(in); err != nil { return nil, err } @@ -216,13 +188,13 @@ func _Name_FirstName_Handler(srv interface{}, ctx context.Context, dec func(inte FullMethod: "/main.Name/FirstName", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(NameServer).FirstName(ctx, req.(*NameRequest)) + return srv.(NameServer).FirstName(ctx, req.(*Request)) } return interceptor(ctx, in, info, handler) } func _Name_LastName_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(NameRequest) + in := new(Request) if err := dec(in); err != nil { return nil, err } @@ -234,13 +206,13 @@ func _Name_LastName_Handler(srv interface{}, ctx context.Context, dec func(inter FullMethod: "/main.Name/LastName", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(NameServer).LastName(ctx, req.(*NameRequest)) + return srv.(NameServer).LastName(ctx, req.(*Request)) } return interceptor(ctx, in, info, handler) } func _Name_FullName_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(NameRequest) + in := new(Request) if err := dec(in); err != nil { return nil, err } @@ -252,7 +224,7 @@ func _Name_FullName_Handler(srv interface{}, ctx context.Context, dec func(inter FullMethod: "/main.Name/FullName", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(NameServer).FullName(ctx, req.(*NameRequest)) + return srv.(NameServer).FullName(ctx, req.(*Request)) } return interceptor(ctx, in, info, handler) } diff --git a/people.pb.gw.go b/people.pb.gw.go index 00760bf..4090081 100644 --- a/people.pb.gw.go +++ b/people.pb.gw.go @@ -32,7 +32,7 @@ var _ = utilities.NewDoubleArray var _ = descriptor.ForMessage func request_Name_FirstName_0(ctx context.Context, marshaler runtime.Marshaler, client NameClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { - var protoReq NameRequest + var protoReq Request var metadata runtime.ServerMetadata newReader, berr := utilities.IOReaderFactory(req.Body) @@ -49,7 +49,7 @@ func request_Name_FirstName_0(ctx context.Context, marshaler runtime.Marshaler, } func local_request_Name_FirstName_0(ctx context.Context, marshaler runtime.Marshaler, server NameServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { - var protoReq NameRequest + var protoReq Request var metadata runtime.ServerMetadata newReader, berr := utilities.IOReaderFactory(req.Body) @@ -66,7 +66,7 @@ func local_request_Name_FirstName_0(ctx context.Context, marshaler runtime.Marsh } func request_Name_LastName_0(ctx context.Context, marshaler runtime.Marshaler, client NameClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { - var protoReq NameRequest + var protoReq Request var metadata runtime.ServerMetadata newReader, berr := utilities.IOReaderFactory(req.Body) @@ -83,7 +83,7 @@ func request_Name_LastName_0(ctx context.Context, marshaler runtime.Marshaler, c } func local_request_Name_LastName_0(ctx context.Context, marshaler runtime.Marshaler, server NameServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { - var protoReq NameRequest + var protoReq Request var metadata runtime.ServerMetadata newReader, berr := utilities.IOReaderFactory(req.Body) @@ -100,7 +100,7 @@ func local_request_Name_LastName_0(ctx context.Context, marshaler runtime.Marsha } func request_Name_FullName_0(ctx context.Context, marshaler runtime.Marshaler, client NameClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { - var protoReq NameRequest + var protoReq Request var metadata runtime.ServerMetadata newReader, berr := utilities.IOReaderFactory(req.Body) @@ -117,7 +117,7 @@ func request_Name_FullName_0(ctx context.Context, marshaler runtime.Marshaler, c } func local_request_Name_FullName_0(ctx context.Context, marshaler runtime.Marshaler, server NameServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { - var protoReq NameRequest + var protoReq Request var metadata runtime.ServerMetadata newReader, berr := utilities.IOReaderFactory(req.Body) diff --git a/proto/base.proto b/proto/base.proto new file mode 100644 index 0000000..6577bb3 --- /dev/null +++ b/proto/base.proto @@ -0,0 +1,14 @@ +syntax = "proto3"; + +package main; + +option go_package = ".;main"; +// The request message containing the user's name. +message Request { + // NameType nametype = 1; +} + + // The response message containing the greetings +message Reply { + string message = 1; +} \ No newline at end of file diff --git a/proto/country.proto b/proto/country.proto new file mode 100644 index 0000000..9aad57d --- /dev/null +++ b/proto/country.proto @@ -0,0 +1,39 @@ +syntax = "proto3"; + +package main; + +option go_package = ".;main"; + + +import "base.proto"; +import "google/api/annotations.proto"; + + +// The greeting service definition. +service Country { + // Sends a greeting + rpc Name (Request) returns (Reply){ + option (google.api.http) = { + post: "/v1/country/name" + body: "*" + }; + } + + rpc Picture (Request) returns (CountryReply){ + option (google.api.http) = { + post: "/v1/country/name/picture" + body: "*" + }; + } +} + +// The response message containing the greetings +message CountryReply { + bytes image = 1; +} + +// enum NameType { +// FullName = 0; +// FirstName = 1; +// LastName = 2 ; +// } \ No newline at end of file diff --git a/proto/people.proto b/proto/people.proto index 7b7f7e0..4756356 100644 --- a/proto/people.proto +++ b/proto/people.proto @@ -5,25 +5,26 @@ package main; option go_package = ".;main"; import "google/api/annotations.proto"; +import "base.proto"; // The greeting service definition. service Name { // Sends a greeting - rpc FirstName (NameRequest) returns (NameReply){ + rpc FirstName (Request) returns (Reply){ option (google.api.http) = { post: "/v1/name/firstname" body: "*" }; } - rpc LastName (NameRequest) returns (NameReply){ + rpc LastName (Request) returns (Reply){ option (google.api.http) = { post: "/v1/name/lastname" body: "*" }; } - rpc FullName (NameRequest) returns (NameReply){ + rpc FullName (Request) returns (Reply){ option (google.api.http) = { post: "/v1/name/fullname" body: "*" @@ -31,16 +32,6 @@ service Name { } } -// The request message containing the user's name. -message NameRequest { - // NameType nametype = 1; -} - -// The response message containing the greetings -message NameReply { - string message = 1; -} - // enum NameType { // FullName = 0; // FirstName = 1;