Skip to content

Commit

Permalink
feat: update go.mod
Browse files Browse the repository at this point in the history
  • Loading branch information
daheige committed Jun 4, 2023
1 parent 67a4644 commit e61c877
Show file tree
Hide file tree
Showing 12 changed files with 2,972 additions and 82 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.txt
*.exe
node_modules/*
example/server/server
61 changes: 60 additions & 1 deletion ctx_keys.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package gmicro

import (
"context"

"google.golang.org/grpc/metadata"
)

// CtxKey ctx key type.
type CtxKey string

Expand All @@ -8,11 +14,64 @@ const (
XRequestID CtxKey = "x-request-id"

// GRPCClientIP grpc client_ip
GRPCClientIP CtxKey = "grpc_client_ip"
GRPCClientIP CtxKey = "client-ip"

// RequestMethod request method
RequestMethod CtxKey = "request_method"

// RequestURI request uri
RequestURI CtxKey = "request_uri"
)

// String returns string
func (c CtxKey) String() string {
return string(c)
}

// GetIncomingMD returns metadata.MD from incoming ctx
// get request metadata
// this method is mainly used at the server end to get the relevant metadata data
func GetIncomingMD(ctx context.Context) metadata.MD {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return metadata.MD{}
}

return md
}

// GetOutgoingMD returns metadata.MD from outgoing ctx
// Use this method when you pass ctx to a downstream service
func GetOutgoingMD(ctx context.Context) metadata.MD {
md, ok := metadata.FromOutgoingContext(ctx)
if !ok {
return metadata.MD{}
}

return md
}

// GetSliceFromMD returns []string from md
func GetSliceFromMD(md metadata.MD, key CtxKey) []string {
return md.Get(key.String())
}

// GetSliceFromMD returns string from md
func GetStringFromMD(md metadata.MD, key CtxKey) string {
values := md.Get(key.String())
if len(values) > 0 {
return values[0]
}

return ""
}

// SetCtxValue returns ctx when you set key/value into ctx
func SetCtxValue(ctx context.Context, key CtxKey, val interface{}) context.Context {
return context.WithValue(ctx, key.String(), val)
}

// GetCtxValue returns ctx when you set key/value into ctx
func GetCtxValue(ctx context.Context, key CtxKey) interface{} {
return ctx.Value(key)
}
13 changes: 11 additions & 2 deletions example/clients/go/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"log"
"os"

"github.com/daheige/gmicro/v2"
"github.com/daheige/gmicro/v2/example/clients/go/pb"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)

const (
var (
address = "localhost:8081" // grpc server and http gateway on share port
// address = "localhost:50051" // grpc server port without http gateway
// address = "localhost:50050" // nginx grpc_pass port
Expand Down Expand Up @@ -46,7 +48,14 @@ func main() {
name = os.Args[1]
}

res, err := c.SayHello(context.Background(), &pb.HelloReq{
requestID := gmicro.Uuid()
log.Println("x-request-id: ", requestID)
md := metadata.New(map[string]string{
"x-request-id": requestID,
})

ctx := metadata.NewOutgoingContext(context.Background(), md)
res, err := c.SayHello(ctx, &pb.HelloReq{
Name: name,
})

Expand Down

0 comments on commit e61c877

Please sign in to comment.