From efa2be9b7a29df49af74f18d25fcf6110f1c1727 Mon Sep 17 00:00:00 2001 From: wyh Date: Fri, 3 Nov 2023 15:53:43 +0800 Subject: [PATCH] update --- .gitignore | 4 +- README.md | 49 ++++++-- cert.pem | 29 ----- cert/cert.pem | 30 +++++ cert/key.pem | 52 ++++++++ openssl.cnf => cert/openssl.cnf | 4 +- go.mod | 32 ++--- key.pem | 52 -------- main.go | 9 +- model/token-model.go | 6 + pb/product.pb.go | 211 ++++++++++++++++++++++++++++++++ pb/product_grpc.pb.go | 105 ++++++++++++++++ pb/user.pb.go | 171 ++++++++++++++++++++++++++ protos/product.proto | 15 +++ protos/user.proto | 10 ++ server/employee-server.go | 36 +++++- server/product-server.go | 28 +++++ service/grpc-service.go | 72 ++++++++++- 18 files changed, 795 insertions(+), 120 deletions(-) delete mode 100644 cert.pem create mode 100644 cert/cert.pem create mode 100644 cert/key.pem rename openssl.cnf => cert/openssl.cnf (99%) delete mode 100644 key.pem create mode 100644 model/token-model.go create mode 100644 pb/product.pb.go create mode 100644 pb/product_grpc.pb.go create mode 100644 pb/user.pb.go create mode 100644 protos/product.proto create mode 100644 protos/user.proto create mode 100644 server/product-server.go diff --git a/.gitignore b/.gitignore index 7df5754..8d2133c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.sum -logs \ No newline at end of file +logs +*.exe +.vscode \ No newline at end of file diff --git a/README.md b/README.md index 542ae90..51ede77 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,45 @@ ### grpc-server -- 获取组件 +##### 获取组件 - go get -u google.golang.org/grpc -- 生成 pb 文件 -- protoc --proto_path=./protos ./protos/\*.proto --go_out=plugins=grpc:./pb +- go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28 +- go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2 -- 生成 ssl 证书 -- cmd: openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj /CN=localhost -- go1.15 之后需使用临时配置 添加扩展 SAN -- 临时配置 openssl.cnf 并使用 -- -config 指定配置文件 -- -extensions v3_req 扩展 -- cmd: openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj /CN=localhost -config ./openssl.cnf -extensions v3_req + +##### pb文件 生成两个文件一个基础模型文件,一个grpc文件 +- protoc --go_out=../pb ./product.proto +- protoc --go-grpc_out=../pb ./product.proto + + +### TSL证书生成 + + +1. openssl安装:https://slproweb.com/products/Win32OpenSSL.html light版即可 +2. 所以操作都在cert目录下执行 +> 生成私钥文件 +- openssl genrsa -des3 -out ca.key 2048 +> 创建证书请求 +- openssl req -new -key ca.key -out ca.csr +>生成crt文件 //windows需要在cmd命令窗口运行 +- openssl x509 -req -days 365 -in ca.csr-signkey ca.key -out ca.crt +>生成证书私钥server.key +- openssl genpkey -algorithm RSA -out server.key +>通过server.key生成证书请求文件server.csr +- openssl req -new -nodes -key server.key -out server.csr -days 3650 -config ./openssl.cnf -extensions v3_req +>生成SAN证书 +- openssl x509 -req -days 365 -in server.csr -out server.pem -CA ca.crt -CAkey ca.key -CAcreateserial -extfile ./openssl.cnf -extensions v3_req + +##### 下面为之前版本应该也能使用 +``` +临时配置 openssl.cnf 并使用 + +-config 指定配置文件 + +-extensions v3_req 扩展 + +cmd: openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj /CN=localhost -config ./openssl.cnf -extensions v3_req + + +``` \ No newline at end of file diff --git a/cert.pem b/cert.pem deleted file mode 100644 index 574dd62..0000000 --- a/cert.pem +++ /dev/null @@ -1,29 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIE4TCCAsmgAwIBAgIUJC8WZ7nPROEEwjUJwJpwsUkHQu0wDQYJKoZIhvcNAQEL -BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTIyMDEyMDA4MTE0MFoXDTIzMDEy -MDA4MTE0MFowFDESMBAGA1UEAwwJbG9jYWxob3N0MIICIjANBgkqhkiG9w0BAQEF -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ixz2AG7ICxePdxXpRY3B02zgW9zOOhr09bbypz -SP/pQFq2TfV/1w6HGS1MKs+VN0DUQAzEmrGf7IR1MlpEv4ujFIXZTaxUPXfbdzu1 -3fsc66FKDsyLd5I0/+DG1+kwFvYNwD/mjDs75S1nLCnOevYTZcdLN40p9OUPXtka -BtEHgBCtmSAqFM9HH6+szziSlRG6iPMX0gWIqB0YzfXadZi7QOiBIvrJhRsP9JgB -VywTEb81V9qHQKHb9rt2kqYgS5S8khW7wJpI6u2S9Dz6JZKvlfdKj6Syuo0c2Uxo -S961yvqiDXGQDRu1rzF8+xFHpVENlDeN2lKHh+vDL36kACWgsozRWYnCEuxgcTwn -bWCuLo8VeA6VQuVQ53Gw5Pd+aOp/fQX0pobv1bdN1HsHwmozjY04mL/1BmMFBbXN -XgDF/7k= ------END CERTIFICATE----- diff --git a/cert/cert.pem b/cert/cert.pem new file mode 100644 index 0000000..063f039 --- /dev/null +++ b/cert/cert.pem @@ -0,0 +1,30 @@ +-----BEGIN CERTIFICATE----- +MIIFDjCCAvagAwIBAgIUcqWf//49z/zT8kVDQvKLNjsuaAYwDQYJKoZIhvcNAQEL +BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTIzMTEwMzAxMjQxOFoXDTI0MTEw +MjAxMjQxOFowFDESMBAGA1UEAwwJbG9jYWxob3N0MIICIjANBgkqhkiG9w0BAQEF +AAOCAg8AMIICCgKCAgEA2XvjMScP+cpYvMyHHdCYtDyOKh/wW5/vZq912BiyZK0o +MVF7HEF605z73tnpZ4lvW9QiNzlM+HTKGM/w6hzf9fJaaKh0lfpPTWztE2l6UaMU +ft4xOlBrDhZSkcuZByo2tWhsoMfi/4iAa6ndJbZsNNLKKGOwqJFJHO8zqg99Y+Z4 +SUJa4N5DSjHPi1eyPUcpXibtF+ud/BcJptLFLjAvunCL/NxWUqtweOI+Y3kkrBSd +k1ScmTAtpI1YAx3MYvU6JWU+EZaEjq/BABkYd1IDD7+Bmtpsl2yrL0t/63W0Xa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qXx0QXnWmJUrpPEOMyBBEZsZtm8/+EZakez7bLkG +IVeRccBImuVCmmDQ/Djs5xew/gJMYJgwvLPLch/wEGUlVqvmMZ/oKK5taO/grV4h +gzARNVU2SB0I6oacrzXB8mqYkEu++yRJHqKQFqbOZLkhgQFRTz4qVhPf2/5Gb4Dz +X54= +-----END CERTIFICATE----- diff --git a/cert/key.pem b/cert/key.pem new file mode 100644 index 0000000..09e1c5f --- /dev/null +++ b/cert/key.pem @@ -0,0 +1,52 @@ +-----BEGIN PRIVATE KEY----- +MIIJRAIBADANBgkqhkiG9w0BAQEFAASCCS4wggkqAgEAAoICAQDZe+MxJw/5yli8 +zIcd0Ji0PI4qH/Bbn+9mr3XYGLJkrSgxUXscQXrTnPve2elniW9b1CI3OUz4dMoY +z/DqHN/18lpoqHSV+k9NbO0TaXpRoxR+3jE6UGsOFlKRy5kHKja1aGygx+L/iIBr +qd0ltmw00sooY7CokUkc7zOqD31j5nhJQlrg3kNKMc+LV7I9RyleJu0X6538Fwmm +0sUuMC+6cIv83FZSq3B44j5jeSSsFJ2TVJyZMC2kjVgDHcxi9TolZT4RloSOr8EA +GRh3UgMPv4Ga2myXbKsvS3/rdbRdpZhEudlekJWMs2d7znjnPPE3BxNr+iQYblD0 +V9KV9nmrHwMHP/SkinxrPMlXTPb/Y8qFmCDCGyLgJZfrfvmIUEwt0Xk6m2dfYcCR +u1sKy3mibgF0Gs6CD4Wlsw6bWtx2jdEqvLDHTiJWWOWD3p8EDBqj7UZbOItO5/Rs +qQZEP9+JCYTEbH9zT1bUYEzDk9XNVmCdxIC/CYrYyuGAkEv2OyqYrcS//a7ZYGnL +mjYGQWytkLjfPZwAiFiVtisoXRiMjfK5dJptkeQq1MeoVMd7bLd3t9rVVTbu5uFm +SnWtniTIrh9kx+6gg9m3Dxjyy2a8a44OqGvhsKwPUbNCk4NCOlcdrZ+qSnnJolAQ +P+1VyQc4sW8oPq93wU5w4x42/XiseQIDAQABAoICAAx0gyAhQdG3JCpZ10I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GbN7rPx4y3ezMSkKc06 +QOg/BxjeGKrs1N4LGQptZo9UWqFcpaXDthc9OJQmZVvoNTORrsYwGlm68zDX8pno +dgRe/fQwgiLWB5p5yj4zqZh28Tu6tXcteVUud1h3xVhOk9J05bp6q7QcEdKKCQkJ +MHtJ0evMLakzgW17zxw0jfM6Y8qWHveb6A9gOBWnfxqNiHO2KN6Tf6/h5bZNRX6H +hiTaZ2EwFcL4Y9b47x8rAZdAcq9MzH76I1miijbS+nDWiN1hFsRPwMx8/on0LCKZ +nJPGSQUK2+IZTi4sUmCQcy3H9tTurpA3 +-----END PRIVATE KEY----- diff --git a/openssl.cnf b/cert/openssl.cnf similarity index 99% rename from openssl.cnf rename to cert/openssl.cnf index 9c6cc70..df1104a 100644 --- a/openssl.cnf +++ b/cert/openssl.cnf @@ -227,8 +227,8 @@ subjectAltName = @alt_names [ alt_names ] -DNS.1 = * -DNS.2 = * +DNS.1 = localhost +DNS.2 = 0.0.0.0 [ v3_ca ] diff --git a/go.mod b/go.mod index ac907f0..a7c3086 100644 --- a/go.mod +++ b/go.mod @@ -3,33 +3,21 @@ module myschools.me/wyh/grpcservice go 1.17 require ( - github.com/gin-gonic/gin v1.7.7 github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 - github.com/sirupsen/logrus v1.8.1 - golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d - google.golang.org/grpc v1.43.0 - google.golang.org/protobuf v1.27.1 + github.com/sirupsen/logrus v1.9.3 + google.golang.org/grpc v1.59.0 + google.golang.org/protobuf v1.31.0 ) require ( - github.com/gin-contrib/sse v0.1.0 // indirect - github.com/go-playground/locales v0.13.0 // indirect - github.com/go-playground/universal-translator v0.17.0 // indirect - github.com/go-playground/validator/v10 v10.4.1 // indirect - github.com/golang/protobuf v1.5.2 // indirect + github.com/golang/protobuf v1.5.3 // indirect github.com/jonboulle/clockwork v0.2.2 // indirect - github.com/json-iterator/go v1.1.9 // indirect - github.com/leodido/go-urn v1.2.0 // indirect - github.com/lestrrat-go/strftime v1.0.5 // indirect - github.com/mattn/go-isatty v0.0.12 // indirect - github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect - github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect + github.com/lestrrat-go/strftime v1.0.6 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/ugorji/go/codec v1.1.7 // indirect - golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect - golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect - golang.org/x/text v0.3.7 // indirect - google.golang.org/genproto v0.0.0-20220118154757-00ab72f36ad5 // indirect - gopkg.in/yaml.v2 v2.2.8 // indirect + golang.org/x/net v0.17.0 // indirect + golang.org/x/sys v0.13.0 // indirect + golang.org/x/text v0.13.0 // indirect + google.golang.org/genproto v0.0.0-20231016165738-49dd2c1f3d0b // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect ) diff --git a/key.pem b/key.pem deleted file mode 100644 index 483ce7d..0000000 --- a/key.pem +++ /dev/null @@ -1,52 +0,0 @@ ------BEGIN PRIVATE KEY----- -MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQC43AM8F2ZOUatm -BUSdiSPT9qO43uX/0m6iq5GmONLNNRHZsgMv94VBEFRhc8VW5kZ6nTahukSCLhuM -/At/Z4J4dk7PYI/tRTw7KcqHhXl8iB6tQoilW1n4gq+F/O2+fk5s0rlvidkJxvbT -CY2dCOfkumOgxNPgQ1PJgVRDTxjfVcU2CcrpRCaXzoE0xY3s3qDX0uM6drbEr88O -uguKWaErXraWIdrDRL5YQTpe+Uu0NEaiOC4H7fAQEEgVAniS8sHzL1+3Xd6H9RgA -ILv/hKscf1TBNQqFPAzb532XgkCc9zutTXTc5Ee7eSHgk7ELT+7qAkoWSlTV4UhD -9uEYDNL9wcvgzr4oXX7ohChatr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u/CcJRh+ijDdouZRTouKBt6T5uN9xOhk+P+ -M0XeejV/ttOOtpBZbHur9MMS3xp3M3BXqCorqxGU0IIKlnSd64au3rqdqi8x6l2f -Wk0yxShTHeb4wlSuImTdMoViCajXrl9zH0N8RdzpL8QmLUCwlkcqsnPcwrBAq/ZZ -qkNt8pyWcadXaPXmcgXgSr3h6x2taokHTb35O6A6ms+Y/slQGo7MxamgzVe+Q3Lf -96b1ac/K21mSenNgRaP6mR7vWPistqZaFmJn1A2tNdLd0M0pVsAMVPH/N4ILhSAp -bQKCAQEAi6kAq3RyexDcvaMuqheNjeiCVp0NcTbc3gzM0lDVHPIjhjEUpUj/9j8a -QMjC5wLqjPSDSQHDq8gSEzvS+InUpM9Z6C8e/tEa6O8WtrA6CQ8p1ewzwbIuUpCI -PbuUCnWKNGcvfgQNECC7XU/8wHTK3EA7RJJY/v/5q85oPoXWAqGdPaRgWZY0Dty4 -emi8yIzipsgaKWhh5AUghdoSQnzUEvaZwhMse/dztNAZXKTGcPJXRdmeExWLbbUO -4MQMLSWUcDBuQlgiSsdHAHmieiK0PXSxeiqwxWnnxML/7Fj8TB4T88Od26lEYUxD -tPbMW45EPgRyUxyKaQtIrUgBe8lvsg== ------END PRIVATE KEY----- diff --git a/main.go b/main.go index 5917b03..62fdd8d 100644 --- a/main.go +++ b/main.go @@ -18,8 +18,13 @@ const ( ) func main() { + // user := &pb.User{ + // Username: "ll", + // Age: 18, + // } + // proto.Marshal(user) // 证书 - creds, err := credentials.NewServerTLSFromFile("cert.pem", "key.pem") + creds, err := credentials.NewServerTLSFromFile("./cert/cert.pem", "./cert/key.pem") if err != nil { log.Fatalf("err: %s", err.Error()) } @@ -29,7 +34,7 @@ func main() { server := grpc.NewServer(options...) //服务注册 pb.RegisterEmployeeServiceServer(server, &s.EmployeeService{}) - + pb.RegisterProductServiceServer(server, &s.ProductService{}) log.Println("grpc server started..." + port) tcp, err := net.ResolveTCPAddr("tcp", "0.0.0.0:"+port) if err != nil { diff --git a/model/token-model.go b/model/token-model.go new file mode 100644 index 0000000..5053589 --- /dev/null +++ b/model/token-model.go @@ -0,0 +1,6 @@ +package model + +type Token struct { + Token string + ExpireIn int +} diff --git a/pb/product.pb.go b/pb/product.pb.go new file mode 100644 index 0000000..df3f827 --- /dev/null +++ b/pb/product.pb.go @@ -0,0 +1,211 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.1 +// protoc v4.24.0 +// source: product.proto + +package pb + +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 ProductRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + ProdId int32 `protobuf:"varint,1,opt,name=prod_id,json=prodId,proto3" json:"prod_id,omitempty"` +} + +func (x *ProductRequest) Reset() { + *x = ProductRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_product_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ProductRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ProductRequest) ProtoMessage() {} + +func (x *ProductRequest) ProtoReflect() protoreflect.Message { + mi := &file_product_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 ProductRequest.ProtoReflect.Descriptor instead. +func (*ProductRequest) Descriptor() ([]byte, []int) { + return file_product_proto_rawDescGZIP(), []int{0} +} + +func (x *ProductRequest) GetProdId() int32 { + if x != nil { + return x.ProdId + } + return 0 +} + +type ProductResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + ProdStock int32 `protobuf:"varint,1,opt,name=prod_stock,json=prodStock,proto3" json:"prod_stock,omitempty"` +} + +func (x *ProductResponse) Reset() { + *x = ProductResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_product_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ProductResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ProductResponse) ProtoMessage() {} + +func (x *ProductResponse) ProtoReflect() protoreflect.Message { + mi := &file_product_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 ProductResponse.ProtoReflect.Descriptor instead. +func (*ProductResponse) Descriptor() ([]byte, []int) { + return file_product_proto_rawDescGZIP(), []int{1} +} + +func (x *ProductResponse) GetProdStock() int32 { + if x != nil { + return x.ProdStock + } + return 0 +} + +var File_product_proto protoreflect.FileDescriptor + +var file_product_proto_rawDesc = []byte{ + 0x0a, 0x0d, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, + 0x29, 0x0a, 0x0e, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x12, 0x17, 0x0a, 0x07, 0x70, 0x72, 0x6f, 0x64, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x05, 0x52, 0x06, 0x70, 0x72, 0x6f, 0x64, 0x49, 0x64, 0x22, 0x30, 0x0a, 0x0f, 0x50, 0x72, + 0x6f, 0x64, 0x75, 0x63, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1d, 0x0a, + 0x0a, 0x70, 0x72, 0x6f, 0x64, 0x5f, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x05, 0x52, 0x09, 0x70, 0x72, 0x6f, 0x64, 0x53, 0x74, 0x6f, 0x63, 0x6b, 0x32, 0x46, 0x0a, 0x0e, + 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x34, + 0x0a, 0x0f, 0x47, 0x65, 0x74, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x53, 0x74, 0x6f, 0x63, + 0x6b, 0x12, 0x0f, 0x2e, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x1a, 0x10, 0x2e, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x07, 0x5a, 0x05, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_product_proto_rawDescOnce sync.Once + file_product_proto_rawDescData = file_product_proto_rawDesc +) + +func file_product_proto_rawDescGZIP() []byte { + file_product_proto_rawDescOnce.Do(func() { + file_product_proto_rawDescData = protoimpl.X.CompressGZIP(file_product_proto_rawDescData) + }) + return file_product_proto_rawDescData +} + +var file_product_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_product_proto_goTypes = []interface{}{ + (*ProductRequest)(nil), // 0: ProductRequest + (*ProductResponse)(nil), // 1: ProductResponse +} +var file_product_proto_depIdxs = []int32{ + 0, // 0: ProductService.GetProductStock:input_type -> ProductRequest + 1, // 1: ProductService.GetProductStock:output_type -> ProductResponse + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] 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_product_proto_init() } +func file_product_proto_init() { + if File_product_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_product_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ProductRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_product_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ProductResponse); 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_product_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_product_proto_goTypes, + DependencyIndexes: file_product_proto_depIdxs, + MessageInfos: file_product_proto_msgTypes, + }.Build() + File_product_proto = out.File + file_product_proto_rawDesc = nil + file_product_proto_goTypes = nil + file_product_proto_depIdxs = nil +} diff --git a/pb/product_grpc.pb.go b/pb/product_grpc.pb.go new file mode 100644 index 0000000..c811e16 --- /dev/null +++ b/pb/product_grpc.pb.go @@ -0,0 +1,105 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v4.24.0 +// source: product.proto + +package pb + +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 + +// ProductServiceClient is the client API for ProductService 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 ProductServiceClient interface { + GetProductStock(ctx context.Context, in *ProductRequest, opts ...grpc.CallOption) (*ProductResponse, error) +} + +type productServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewProductServiceClient(cc grpc.ClientConnInterface) ProductServiceClient { + return &productServiceClient{cc} +} + +func (c *productServiceClient) GetProductStock(ctx context.Context, in *ProductRequest, opts ...grpc.CallOption) (*ProductResponse, error) { + out := new(ProductResponse) + err := c.cc.Invoke(ctx, "/ProductService/GetProductStock", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// ProductServiceServer is the server API for ProductService service. +// All implementations must embed UnimplementedProductServiceServer +// for forward compatibility +type ProductServiceServer interface { + GetProductStock(context.Context, *ProductRequest) (*ProductResponse, error) + mustEmbedUnimplementedProductServiceServer() +} + +// UnimplementedProductServiceServer must be embedded to have forward compatible implementations. +type UnimplementedProductServiceServer struct { +} + +func (UnimplementedProductServiceServer) GetProductStock(context.Context, *ProductRequest) (*ProductResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetProductStock not implemented") +} +func (UnimplementedProductServiceServer) mustEmbedUnimplementedProductServiceServer() {} + +// UnsafeProductServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to ProductServiceServer will +// result in compilation errors. +type UnsafeProductServiceServer interface { + mustEmbedUnimplementedProductServiceServer() +} + +func RegisterProductServiceServer(s grpc.ServiceRegistrar, srv ProductServiceServer) { + s.RegisterService(&ProductService_ServiceDesc, srv) +} + +func _ProductService_GetProductStock_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(ProductRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ProductServiceServer).GetProductStock(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/ProductService/GetProductStock", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ProductServiceServer).GetProductStock(ctx, req.(*ProductRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// ProductService_ServiceDesc is the grpc.ServiceDesc for ProductService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var ProductService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "ProductService", + HandlerType: (*ProductServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "GetProductStock", + Handler: _ProductService_GetProductStock_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "product.proto", +} diff --git a/pb/user.pb.go b/pb/user.pb.go new file mode 100644 index 0000000..0a4b043 --- /dev/null +++ b/pb/user.pb.go @@ -0,0 +1,171 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.1 +// protoc v4.24.0 +// source: user.proto + +package pb + +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 User struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Username string `protobuf:"bytes,1,opt,name=username,proto3" json:"username,omitempty"` + Age int32 `protobuf:"varint,2,opt,name=age,proto3" json:"age,omitempty"` + Password *string `protobuf:"bytes,3,opt,name=password,proto3,oneof" json:"password,omitempty"` + Addresses []string `protobuf:"bytes,4,rep,name=addresses,proto3" json:"addresses,omitempty"` +} + +func (x *User) Reset() { + *x = User{} + if protoimpl.UnsafeEnabled { + mi := &file_user_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *User) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*User) ProtoMessage() {} + +func (x *User) ProtoReflect() protoreflect.Message { + mi := &file_user_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 User.ProtoReflect.Descriptor instead. +func (*User) Descriptor() ([]byte, []int) { + return file_user_proto_rawDescGZIP(), []int{0} +} + +func (x *User) GetUsername() string { + if x != nil { + return x.Username + } + return "" +} + +func (x *User) GetAge() int32 { + if x != nil { + return x.Age + } + return 0 +} + +func (x *User) GetPassword() string { + if x != nil && x.Password != nil { + return *x.Password + } + return "" +} + +func (x *User) GetAddresses() []string { + if x != nil { + return x.Addresses + } + return nil +} + +var File_user_proto protoreflect.FileDescriptor + +var file_user_proto_rawDesc = []byte{ + 0x0a, 0x0a, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x80, 0x01, 0x0a, + 0x04, 0x55, 0x73, 0x65, 0x72, 0x12, 0x1a, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, + 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, + 0x65, 0x12, 0x10, 0x0a, 0x03, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, + 0x61, 0x67, 0x65, 0x12, 0x1f, 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, + 0x03, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, + 0x64, 0x88, 0x01, 0x01, 0x12, 0x1c, 0x0a, 0x09, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x65, + 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x09, 0x52, 0x09, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, + 0x65, 0x73, 0x42, 0x0b, 0x0a, 0x09, 0x5f, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x42, + 0x07, 0x5a, 0x05, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_user_proto_rawDescOnce sync.Once + file_user_proto_rawDescData = file_user_proto_rawDesc +) + +func file_user_proto_rawDescGZIP() []byte { + file_user_proto_rawDescOnce.Do(func() { + file_user_proto_rawDescData = protoimpl.X.CompressGZIP(file_user_proto_rawDescData) + }) + return file_user_proto_rawDescData +} + +var file_user_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_user_proto_goTypes = []interface{}{ + (*User)(nil), // 0: User +} +var file_user_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_user_proto_init() } +func file_user_proto_init() { + if File_user_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_user_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*User); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + file_user_proto_msgTypes[0].OneofWrappers = []interface{}{} + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_user_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_user_proto_goTypes, + DependencyIndexes: file_user_proto_depIdxs, + MessageInfos: file_user_proto_msgTypes, + }.Build() + File_user_proto = out.File + file_user_proto_rawDesc = nil + file_user_proto_goTypes = nil + file_user_proto_depIdxs = nil +} diff --git a/protos/product.proto b/protos/product.proto new file mode 100644 index 0000000..6611def --- /dev/null +++ b/protos/product.proto @@ -0,0 +1,15 @@ +syntax="proto3"; + +option go_package="../pb"; + +message ProductRequest { + int32 prod_id=1; +} + +message ProductResponse { + int32 prod_stock=1; +} + +service ProductService{ + rpc GetProductStock (ProductRequest) returns (ProductResponse); +} \ No newline at end of file diff --git a/protos/user.proto b/protos/user.proto new file mode 100644 index 0000000..12af96d --- /dev/null +++ b/protos/user.proto @@ -0,0 +1,10 @@ +syntax="proto3"; + +option go_package="../pb"; + +message User { + string username =1; + int32 age =2; + optional string password =3; + repeated string addresses =4; +} \ No newline at end of file diff --git a/server/employee-server.go b/server/employee-server.go index 7ce3fcb..70b4c07 100644 --- a/server/employee-server.go +++ b/server/employee-server.go @@ -1,9 +1,12 @@ package server import ( + "bytes" "context" "fmt" + "image/png" "io" + "os" "time" "google.golang.org/grpc/metadata" @@ -23,6 +26,7 @@ func (msg *EmployeeService) GetByNo(ctx context.Context, req *pb.GetByNoRequset) }, nil } } + fmt.Println(1) return &pb.EmployeeResponse{}, nil } @@ -47,6 +51,7 @@ func (msg *EmployeeService) AddPhoto(stream pb.EmployeeService_AddPhotoServer) e data, err := stream.Recv() if err == io.EOF { fmt.Printf("file size:%d\n", len(img)) + serveFrames(img) return stream.SendAndClose(&pb.AddPhotoResponse{IsOk: true}) } if err != nil { @@ -57,7 +62,7 @@ func (msg *EmployeeService) AddPhoto(stream pb.EmployeeService_AddPhotoServer) e } } func (msg *EmployeeService) Save(context.Context, *pb.EmployeeRequest) (*pb.EmployeeResponse, error) { - return nil, nil + return &pb.EmployeeResponse{Employee: &data.Employees[0]}, nil } func (msg *EmployeeService) SaveAll(stream pb.EmployeeService_SaveAllServer) error { for { @@ -76,3 +81,32 @@ func (msg *EmployeeService) SaveAll(stream pb.EmployeeService_SaveAllServer) err } return nil } + +func serveFrames(imgByte []byte) { + // 解码 + file, err := os.Create("img.png") + if err != nil { + fmt.Println(err) + return + } + defer file.Close() + + reader := bytes.NewReader(imgByte) + // 文件解码,生成image.image + imgfile, err := png.Decode(reader) + if err != nil { + panic(err) + } + // // 新建画布 + // newTemplateImage := image.NewRGBA(templateFileImage.Bounds()) + // // 将图片画到新建的画布上 + // draw.Draw(newTemplateImage, templateFileImage.Bounds(), templateFileImage, templateFileImage.Bounds().Min, draw.Over) + dstFile, err := os.Create("img.png") + if err != nil { + fmt.Println(err) + } + defer dstFile.Close() + // 文件编码,写入image.image + png.Encode(dstFile, imgfile) + +} diff --git a/server/product-server.go b/server/product-server.go new file mode 100644 index 0000000..c210a8f --- /dev/null +++ b/server/product-server.go @@ -0,0 +1,28 @@ +package server + +import ( + "context" + "fmt" + + "myschools.me/wyh/grpcservice/model" + "myschools.me/wyh/grpcservice/pb" +) + +type ProductService struct { + pb.UnimplementedProductServiceServer +} + +func (server *ProductService) GetProductStock(ctx context.Context, request *pb.ProductRequest) (*pb.ProductResponse, error) { + tokenany := ctx.Value("token") + token, ok := tokenany.(*model.Token) + if !ok { + return &pb.ProductResponse{ProdStock: 60}, nil + } + stcok := server.GetStockByID(request.ProdId) + fmt.Println(token) + return &pb.ProductResponse{ProdStock: stcok}, nil +} + +func (server *ProductService) GetStockByID(prodid int32) int32 { + return 100 +} diff --git a/service/grpc-service.go b/service/grpc-service.go index c6ebb94..dc429dd 100644 --- a/service/grpc-service.go +++ b/service/grpc-service.go @@ -2,10 +2,80 @@ package service import ( "context" + "errors" + "time" "google.golang.org/grpc" + "google.golang.org/grpc/metadata" + "myschools.me/wyh/grpcservice/model" ) func AuthGrpcInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { - return nil, nil + md, ok := metadata.FromIncomingContext(ctx) + if !ok { + return handler(ctx, req) + } + // md 是一个 map[string][]string 类型的 + val, ok := md["token"] + if !ok { + return handler(ctx, req) + } + token, err := grpcAuth(val[0]) + if err != nil { + return handler(ctx, req) + } + ctx = context.WithValue(ctx, "token", token) + return handler(ctx, req) +} + +func grpcAuth(token string) (*model.Token, error) { + if token == "1111" { + return &model.Token{ + Token: "1111", + ExpireIn: 7200, + }, nil + } + // 从缓存获取token中存储的结构体返回, + + return nil, errors.New("token不合法") +} + +func ContextWithToken(token *string, reqid *string, timeout int) (context.Context, context.CancelFunc) { + if timeout == 0 { + timeout = 1 + } + requestid := "" + if reqid != nil { + requestid = *reqid + } + accesstoken := "" + if token != nil { + accesstoken = *token + } + md := metadata.MD{} + md.Set("token", accesstoken) //这里的TOKENNAME无论大小写,获取时均为小写 + md.Set("requestid", requestid) + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second) + // ctx = context.WithValue(ctx, "requestid", requestid) + ctx = metadata.NewOutgoingContext(ctx, md) + return ctx, cancel +} + +// ContextRequestID 从context中获取请求ID,任何出错时均返回"" +func ContextRequestID(ctx context.Context) string { + md, ok := metadata.FromIncomingContext(ctx) + if !ok { + return "" + } + + val, ok := md["requestid"] + if !ok { + return "" + } + + if len(val) == 0 { + return "" + } + + return val[0] }