初始化
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
module code.yun.ink/pkg/grpcx
|
||||
|
||||
go 1.20
|
||||
|
||||
require google.golang.org/grpc v1.63.2
|
||||
|
||||
require (
|
||||
golang.org/x/net v0.21.0 // indirect
|
||||
golang.org/x/sys v0.17.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
|
||||
google.golang.org/protobuf v1.33.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
|
||||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
|
||||
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de h1:cZGRis4/ot9uVm639a+rHCUaG0JJHEsdyzSQTMX+suY=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:H4O17MA/PE9BsGx3w+a+W2VOLLD1Qf7oJneAoU6WktY=
|
||||
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
|
||||
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
|
||||
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
|
||||
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
@@ -3,7 +3,7 @@ package grpcx
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"xiaoxin-plus/pkg/errorx"
|
||||
"errors"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/metadata"
|
||||
@@ -14,6 +14,8 @@ type GrpcReqHeader struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
// 请求头
|
||||
|
||||
// 客户端写入请求header
|
||||
// 注意事项
|
||||
func WriteHeader(ctx context.Context, headers ...GrpcReqHeader) context.Context {
|
||||
@@ -43,7 +45,7 @@ func readHeaderAll(ctx context.Context) (header []GrpcReqHeader, err error) {
|
||||
// 本地的方式
|
||||
md, ok = metadata.FromOutgoingContext(ctx)
|
||||
if !ok {
|
||||
err = errorx.New("metadata.FromIncomingContext error")
|
||||
err = errors.New("metadata.FromIncomingContext error")
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -62,7 +64,7 @@ func ReadHeader(ctx context.Context, keys ...string) (header []GrpcReqHeader, er
|
||||
// 本地的方式
|
||||
md, ok = metadata.FromOutgoingContext(ctx)
|
||||
if !ok {
|
||||
err = errorx.New("metadata.FromIncomingContext error")
|
||||
err = errors.New("metadata.FromIncomingContext error")
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -77,6 +79,8 @@ func ReadHeader(ctx context.Context, keys ...string) (header []GrpcReqHeader, er
|
||||
return
|
||||
}
|
||||
|
||||
// 响应头
|
||||
|
||||
type GrpcRespHeader struct{}
|
||||
|
||||
func ServerWriteRespHeader(ctx context.Context, header GrpcRespHeader) {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package grpcx_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"code.yun.ink/pkg/grpcx"
|
||||
)
|
||||
|
||||
func TestHeader(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
h := grpcx.GrpcReqHeader{
|
||||
Key: "test",
|
||||
Value: "test_val",
|
||||
}
|
||||
ctx = grpcx.WriteHeader(ctx, h)
|
||||
|
||||
hh, err := grpcx.ReadHeader(ctx, "test")
|
||||
fmt.Println(hh, err)
|
||||
}
|
||||
Reference in New Issue
Block a user