refactor: openpitrix module

Signed-off-by: hongming <talonwan@yunify.com>
This commit is contained in:
hongming
2019-09-25 14:07:15 +08:00
parent d0dc66cf28
commit 1b5681c12b
314 changed files with 72092 additions and 25762 deletions

View File

@@ -0,0 +1,52 @@
// Copyright 2018 The OpenPitrix Authors. All rights reserved.
// Use of this source code is governed by a Apache license
// that can be found in the LICENSE file.
package ctxutil
import (
"context"
"google.golang.org/grpc/metadata"
)
const (
messageIdKey = "x-message-id"
requestIdKey = "x-request-id"
)
type getMetadataFromContext func(ctx context.Context) (md metadata.MD, ok bool)
var getMetadataFromContextFunc = []getMetadataFromContext{
metadata.FromOutgoingContext,
metadata.FromIncomingContext,
}
func GetValueFromContext(ctx context.Context, key string) []string {
if ctx == nil {
return []string{}
}
for _, f := range getMetadataFromContextFunc {
md, ok := f(ctx)
if !ok {
continue
}
m, ok := md[key]
if ok && len(m) > 0 {
return m
}
}
m, ok := ctx.Value(key).([]string)
if ok && len(m) > 0 {
return m
}
s, ok := ctx.Value(key).(string)
if ok && len(s) > 0 {
return []string{s}
}
return []string{}
}
func Copy(src, dst context.Context) context.Context {
return SetMessageId(dst, GetMessageId(src))
}

View File

@@ -0,0 +1,35 @@
// Copyright 2018 The OpenPitrix Authors. All rights reserved.
// Use of this source code is governed by a Apache license
// that can be found in the LICENSE file.
package ctxutil
import (
"context"
"google.golang.org/grpc/metadata"
)
func GetMessageId(ctx context.Context) []string {
return GetValueFromContext(ctx, messageIdKey)
}
func SetMessageId(ctx context.Context, messageId []string) context.Context {
ctx = context.WithValue(ctx, messageIdKey, messageId)
md, ok := metadata.FromOutgoingContext(ctx)
if !ok {
md = metadata.MD{}
}
md[messageIdKey] = messageId
return metadata.NewOutgoingContext(ctx, md)
}
func AddMessageId(ctx context.Context, messageId ...string) context.Context {
m := GetMessageId(ctx)
m = append(m, messageId...)
return SetMessageId(ctx, m)
}
func ClearMessageId(ctx context.Context) context.Context {
return SetMessageId(ctx, []string{})
}

View File

@@ -0,0 +1,29 @@
// Copyright 2018 The OpenPitrix Authors. All rights reserved.
// Use of this source code is governed by a Apache license
// that can be found in the LICENSE file.
package ctxutil
import (
"context"
"google.golang.org/grpc/metadata"
)
func GetRequestId(ctx context.Context) string {
rid := GetValueFromContext(ctx, requestIdKey)
if len(rid) == 0 {
return ""
}
return rid[0]
}
func SetRequestId(ctx context.Context, requestId string) context.Context {
ctx = context.WithValue(ctx, requestIdKey, []string{requestId})
md, ok := metadata.FromOutgoingContext(ctx)
if !ok {
md = metadata.MD{}
}
md[requestIdKey] = []string{requestId}
return metadata.NewOutgoingContext(ctx, md)
}

View File

@@ -0,0 +1,45 @@
// Copyright 2018 The OpenPitrix Authors. All rights reserved.
// Use of this source code is governed by a Apache license
// that can be found in the LICENSE file.
package ctxutil
import (
"context"
"encoding/json"
"google.golang.org/grpc/metadata"
"openpitrix.io/openpitrix/pkg/sender"
)
const (
SenderKey = "sender"
TokenType = "Bearer"
)
func GetSender(ctx context.Context) *sender.Sender {
values := GetValueFromContext(ctx, SenderKey)
if len(values) == 0 || len(values[0]) == 0 {
return nil
}
s := sender.Sender{}
err := json.Unmarshal([]byte(values[0]), &s)
if err != nil {
panic(err)
}
return &s
}
func ContextWithSender(ctx context.Context, user *sender.Sender) context.Context {
if user == nil {
return ctx
}
ctx = context.WithValue(ctx, SenderKey, []string{user.ToJson()})
md, ok := metadata.FromOutgoingContext(ctx)
if !ok {
md = metadata.MD{}
}
md[SenderKey] = []string{user.ToJson()}
return metadata.NewOutgoingContext(ctx, md)
}