init project
This commit is contained in:
1 parent
d63cc8d9f5
commit
ee07869839
39 files changed
+1560
-622
No files matched your search
@@ -0,0 +1,31 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"runtime/debug"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func UnaryRecovery(ctx context.Context, request interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (response interface{}, err error) {
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
log.WithField("method", info.FullMethod).WithField("stack", string(debug.Stack())).Error("grpc handler panic")
|
||||
err = status.Error(codes.Internal, "internal server error")
|
||||
}
|
||||
}()
|
||||
return handler(ctx, request)
|
||||
}
|
||||
|
||||
func StreamRecovery(service interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
log.WithField("method", info.FullMethod).WithField("stack", string(debug.Stack())).Error("grpc stream panic")
|
||||
err = status.Error(codes.Internal, "internal server error")
|
||||
}
|
||||
}()
|
||||
return handler(service, stream)
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"micro_layout/internal/logic"
|
||||
"micro_layout/internal/storage/model"
|
||||
|
||||
"git.vitatech.com.cn/leo/proto.git/micro_layout/proto/user"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type UserServer struct {
|
||||
user.UnimplementedUserServer
|
||||
}
|
||||
|
||||
func (server *UserServer) Create(ctx context.Context, request *user.CreateRequest) (*user.CreateResponse, error) {
|
||||
item, err := logic.Resources.UserLogic.Create(ctx, request.GetName())
|
||||
if errors.Is(err, logic.ErrInvalidUser) {
|
||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||
}
|
||||
if err != nil {
|
||||
return nil, rpcError(ctx, err)
|
||||
}
|
||||
return &user.CreateResponse{Item: userItem(item)}, nil
|
||||
}
|
||||
|
||||
func (server *UserServer) QueryInfo(ctx context.Context, request *user.QueryInfoRequest) (*user.QueryInfoResponse, error) {
|
||||
if request.GetId() <= 0 {
|
||||
return nil, status.Error(codes.InvalidArgument, "invalid user id")
|
||||
}
|
||||
item, exists, err := logic.Resources.UserLogic.QueryInfo(ctx, request.GetId())
|
||||
if err != nil {
|
||||
return nil, rpcError(ctx, err)
|
||||
}
|
||||
if !exists {
|
||||
return &user.QueryInfoResponse{Exists: false}, nil
|
||||
}
|
||||
return &user.QueryInfoResponse{Exists: true, Item: userItem(item)}, nil
|
||||
}
|
||||
|
||||
func userItem(item model.User) *user.UserItem {
|
||||
return &user.UserItem{Id: item.ID, Name: item.Name, Status: item.Status}
|
||||
}
|
||||
|
||||
func rpcError(ctx context.Context, err error) error {
|
||||
if ctx.Err() != nil {
|
||||
return status.FromContextError(ctx.Err()).Err()
|
||||
}
|
||||
log.WithError(err).Error("user operation failed")
|
||||
return status.Error(codes.Internal, "internal server error")
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"micro_layout/internal/logic"
|
||||
"micro_layout/internal/storage"
|
||||
|
||||
"git.vitatech.com.cn/leo/proto.git/micro_layout/proto/user"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func TestUserServerUsesRegisteredLogic(t *testing.T) {
|
||||
st, err := storage.NewStorage(storage.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = st.Close() })
|
||||
if err := logic.Init(logic.WithEnv("test"), logic.WithStorage(st)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
created, err := (&UserServer{}).Create(ctx, &user.CreateRequest{Name: " Alice "})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
queried, err := (&UserServer{}).QueryInfo(ctx, &user.QueryInfoRequest{Id: created.Item.Id})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !queried.Exists || queried.Item.Name != "Alice" {
|
||||
t.Fatalf("created user is not shared between handlers: %v", queried)
|
||||
}
|
||||
if _, err := (&UserServer{}).Create(ctx, &user.CreateRequest{Name: " "}); status.Code(err) != codes.InvalidArgument {
|
||||
t.Fatalf("invalid name: got %v, want InvalidArgument", err)
|
||||
}
|
||||
}
|
||||
+11
-19
@@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"git.xiaotuo.co/pkg/go-plugins/grpc/metax"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
func GetBizId(ctx context.Context) (int64, error) {
|
||||
@@ -17,34 +17,26 @@ func GetBizId(ctx context.Context) (int64, error) {
|
||||
}
|
||||
|
||||
func NewContext(ctx context.Context) (context.Context, error) {
|
||||
ctx = metax.TransparentContext(ctx)
|
||||
md, _ := metadata.FromIncomingContext(ctx)
|
||||
ctx = metadata.NewOutgoingContext(ctx, md)
|
||||
|
||||
reqId, exists, err := metax.GetReqId(ctx)
|
||||
if err != nil {
|
||||
return ctx, fmt.Errorf("req_id_parse_fail")
|
||||
}
|
||||
if !exists {
|
||||
reqIds := md.Get("req_id")
|
||||
if len(reqIds) == 0 {
|
||||
return ctx, fmt.Errorf("req_id_not_found")
|
||||
}
|
||||
ctx = context.WithValue(ctx, "req_id", reqId)
|
||||
ctx = context.WithValue(ctx, "req_id", reqIds[0])
|
||||
|
||||
bizId, exists, err := metax.GetBizId(ctx)
|
||||
if err != nil {
|
||||
return ctx, fmt.Errorf("biz_id_parse_fail")
|
||||
}
|
||||
if !exists {
|
||||
bizIds := md.Get("biz_id")
|
||||
if len(bizIds) == 0 {
|
||||
return ctx, fmt.Errorf("biz_id_not_found")
|
||||
}
|
||||
ctx = context.WithValue(ctx, "biz_id", bizId)
|
||||
|
||||
ctx = context.WithValue(ctx, "biz_id", bizIds[0])
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func getContextValue(ctx context.Context, keyword string) string {
|
||||
if ctx.Value(keyword) != nil {
|
||||
if v, ok := ctx.Value(keyword).(string); ok {
|
||||
return v
|
||||
}
|
||||
if value, ok := ctx.Value(keyword).(string); ok {
|
||||
return value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
func TestNewContextPreservesMetadata(t *testing.T) {
|
||||
ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs("req_id", "request-1", "biz_id", "42", "traceparent", "trace-1"))
|
||||
ctx, err := NewContext(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ctx.Value("req_id") != "request-1" {
|
||||
t.Fatal("request id was not added to the business context")
|
||||
}
|
||||
if id, err := GetBizId(ctx); err != nil || id != 42 {
|
||||
t.Fatalf("business id: got %d, %v", id, err)
|
||||
}
|
||||
md, _ := metadata.FromOutgoingContext(ctx)
|
||||
if values := md.Get("traceparent"); len(values) != 1 || values[0] != "trace-1" {
|
||||
t.Fatalf("outgoing metadata lost: %v", md)
|
||||
}
|
||||
if _, err := NewContext(context.Background()); err == nil {
|
||||
t.Fatal("missing request metadata was accepted")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user