43 lines
915 B
Go
43 lines
915 B
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
)
|
|
|
|
func GetBizId(ctx context.Context) (int64, error) {
|
|
bizId := getContextValue(ctx, "biz_id")
|
|
if bizId == "" {
|
|
return 0, fmt.Errorf("biz_id_is_empty")
|
|
}
|
|
return strconv.ParseInt(bizId, 10, 64)
|
|
}
|
|
|
|
func NewContext(ctx context.Context) (context.Context, error) {
|
|
md, _ := metadata.FromIncomingContext(ctx)
|
|
ctx = metadata.NewOutgoingContext(ctx, md)
|
|
|
|
reqIds := md.Get("req_id")
|
|
if len(reqIds) == 0 {
|
|
return ctx, fmt.Errorf("req_id_not_found")
|
|
}
|
|
ctx = context.WithValue(ctx, "req_id", reqIds[0])
|
|
|
|
bizIds := md.Get("biz_id")
|
|
if len(bizIds) == 0 {
|
|
return ctx, fmt.Errorf("biz_id_not_found")
|
|
}
|
|
ctx = context.WithValue(ctx, "biz_id", bizIds[0])
|
|
return ctx, nil
|
|
}
|
|
|
|
func getContextValue(ctx context.Context, keyword string) string {
|
|
if value, ok := ctx.Value(keyword).(string); ok {
|
|
return value
|
|
}
|
|
return ""
|
|
}
|