Skip to content

Commit

Permalink
feat:add tenant and business api
Browse files Browse the repository at this point in the history
  • Loading branch information
geebytes committed Jul 4, 2024
1 parent e4cca41 commit bf13367
Show file tree
Hide file tree
Showing 50 changed files with 2,061 additions and 120 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"editor.insertSpaces": true,
"editor.detectIndentation": false,
"go.formatTool": "goimports",
"editor.formatOnSave": true
"editor.formatOnSave": true,
"go.testTimeout": "60s"
}
6 changes: 3 additions & 3 deletions gateway/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"sync"

gosdk "github.com/begonia-org/go-sdk"
"github.com/gorilla/websocket"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -297,7 +297,7 @@ func (h *HttpEndpointImpl) addHexEncodeSHA256HashV2(req *http.Request) error {
if req.ContentLength == 0 {
hashStruct.Write([]byte("{}"))
hexStr := fmt.Sprintf("%x", hashStruct.Sum(nil))
req.Header.Set("X-Content-Sha256", hexStr)
req.Header.Set(gosdk.HeaderXContentSha256, hexStr)
return nil

}
Expand Down Expand Up @@ -386,7 +386,7 @@ func (h *HttpEndpointImpl) RegisterHandlerClient(ctx context.Context, pd Protobu
if req.Header.Get("accept") == "" || req.Header.Get("accept") == "*/*" {
req.Header.Set("accept", "application/json")
}
log.Printf("request content-type:%s", req.Header.Get("content-type"))
// log.Printf("request content-type:%s", req.Header.Get("content-type"))
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req)
Expand Down
20 changes: 19 additions & 1 deletion gateway/mask.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"encoding/json"
"io"

common "github.com/begonia-org/go-sdk/common/api/v1"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/fieldmaskpb"
)
Expand All @@ -22,7 +24,6 @@ func SetUpdateMaskFields(message protoreflect.ProtoMessage, fields []string) {
// 遍历所有字段
for i := 0; i < md.Fields().Len(); i++ {
field := md.Fields().Get(i)

// 检查字段是否是FieldMask类型
if field.Message() != nil && field.Message().FullName() == "google.protobuf.FieldMask" {
// 获取字段的值(确保它是FieldMask类型)
Expand All @@ -42,6 +43,21 @@ type maskDecoder struct {
newDecoder func(r io.Reader) runtime.Decoder
}

func FilterUnUpdatedFields(message protoreflect.ProtoMessage, mask []string) []string {
md := message.ProtoReflect().Descriptor()
filters := make([]string, 0)
for _, field := range mask {
if fd := md.Fields().ByJSONName(field); fd != nil {
// opt, ok := proto.GetExtension(fd.Options(), common.E_UnUpdatable).(bool)
// log.Printf("field:%v,ok:%v,opt:%v", fd.JSONName(),ok,opt)

if opt, ok := proto.GetExtension(fd.Options(), common.E_UnUpdatable).(bool); !ok || !opt {
filters = append(filters, field)
}
}
}
return filters
}
func NewJsonDecoder(r io.Reader) runtime.Decoder {
return runtime.DecoderWrapper{Decoder: json.NewDecoder(r)}
}
Expand Down Expand Up @@ -82,6 +98,8 @@ func (d *maskDecoder) Decode(v interface{}) error {
return err
}
// 设置更新掩码字段
fields = FilterUnUpdatedFields(message, fields)
// log.Printf("fields:%v", fields)
SetUpdateMaskFields(message, fields)

return nil
Expand Down
46 changes: 33 additions & 13 deletions gateway/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gateway

import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
Expand All @@ -28,15 +27,15 @@ import (
)

const (
XRequestID = "x-request-id"
XUID = "x-uid"
XAccessKey = "x-access-key"
XHttpMethod = "x-http-method"
XRemoteAddr = "x-http-forwarded-for"
XProtocol = "x-http-protocol"
XHttpURI = "x-http-uri"
XIdentity = "x-identity"
XApiKey = "x-api-key"
XRequestID = "x-request-id"
XUID = "x-uid"
XAccessKey = "x-access-key"
XHttpMethod = "x-http-method"
XRemoteAddr = "x-http-forwarded-for"
XProtocol = "x-http-protocol"
XHttpURI = "x-http-uri"
XIdentity = "x-identity"
XApiKey = "x-api-key"
XIdentityType = "x-identity-type"
)

Expand Down Expand Up @@ -117,7 +116,7 @@ func IncomingHeadersToMetadata(ctx context.Context, req *http.Request) metadata.
md.Set(gosdk.GetMetadataKey(XRequestID), reqID)
xuid := md.Get(XUID)
accessKey := md.Get(XAccessKey)
apikey:=md.Get(XApiKey)
apikey := md.Get(XApiKey)
author := ""
// idType := gosdk.UidType
if len(xuid) > 0 {
Expand All @@ -127,7 +126,7 @@ func IncomingHeadersToMetadata(ctx context.Context, req *http.Request) metadata.
author = accessKey[0]
// idType = gosdk.AccessKeyType
}
if author == ""&& len(apikey)>0{
if author == "" && len(apikey) > 0 {
author = apikey[0]
// idType = gosdk.ApiKeyType
}
Expand Down Expand Up @@ -182,6 +181,13 @@ func (log *LoggerMiddleware) UnaryInterceptor(ctx context.Context, req interface
elapsed := time.Since(now)
log.logger(ctx, info.FullMethod, err, elapsed)
}
if md, ok := metadata.FromIncomingContext(ctx); ok {
reqId := md.Get(XRequestID)
if len(reqId) > 0 {
_ = grpc.SendHeader(ctx, metadata.Pairs(XRequestID, reqId[0]))
}
}

}()

rsp, err = handler(ctx, req)
Expand All @@ -196,6 +202,12 @@ func (log *LoggerMiddleware) StreamInterceptor(srv interface{}, ss grpc.ServerSt
elapsed := time.Since(now)
log.logger(ss.Context(), info.FullMethod, err, elapsed)
}
if md, ok := metadata.FromIncomingContext(ss.Context()); ok {
reqId := md.Get(XRequestID)
if len(reqId) > 0 {
_ = grpc.SendHeader(ss.Context(), metadata.Pairs(XRequestID, reqId[0]))
}
}
}()

ctx := ss.Context()
Expand All @@ -216,6 +228,9 @@ func getClientMessageMap() map[int32]string {
if msg := proto.GetExtension(opts, common.E_Msg); msg != nil {
codes[int32(v.Number())] = msg.(string)
}
if msg := proto.GetExtension(opts, common.E_Description); msg != nil {
codes[int32(v.Number())] = msg.(string)
}
}
return true
})
Expand Down Expand Up @@ -248,7 +263,7 @@ func HandleErrorWithLogger(logger logger.Logger) runtime.ErrorHandlerFunc {
"status": statusCode,
},
)
fmt.Printf("error type:%T, error:%v", err, err)
// fmt.Printf("error type:%T, error:%v", err, err)
if _, ok := metadata.FromIncomingContext(ctx); !ok {
md := IncomingHeadersToMetadata(ctx, req)
ctx = metadata.NewIncomingContext(ctx, md)
Expand All @@ -261,6 +276,7 @@ func HandleErrorWithLogger(logger logger.Logger) runtime.ErrorHandlerFunc {
msg := st.Message()
details := st.Details()
data.Message = clientMessageFromCode(st.Code())
// log.Info(ctx, fmt.Sprintf("error message:%s", data.Message))
// data.Data = &structpb.Struct{}
for _, detail := range details {
if anyType, ok := detail.(*anypb.Any); ok {
Expand All @@ -275,6 +291,8 @@ func HandleErrorWithLogger(logger logger.Logger) runtime.ErrorHandlerFunc {
})

msg := codes[int32(errDetail.Code)]
// log.Infof(ctx, "error message:%s,err code:%d", msg, errDetail.Code)
// log.Infof(ctx, "codes map:%v", codes)
if errDetail.ToClientMessage != "" {
msg = errDetail.ToClientMessage
}
Expand All @@ -295,6 +313,7 @@ func HandleErrorWithLogger(logger logger.Logger) runtime.ErrorHandlerFunc {
log.WithField("status", code).Errorf(ctx, msg)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
// log.Infof(ctx, "error message:%s,err code:%d", data.Message, data.Code)
bData, _ := protojson.Marshal(data)
_, _ = w.Write(bData)
return
Expand Down Expand Up @@ -344,6 +363,7 @@ func HttpResponseBodyModify(ctx context.Context, w http.ResponseWriter, msg prot
w.Header().Del(key)

}
// log.Printf("send to client rsp header,key:%s,value:%s", key, value[0])
writeHttpHeaders(w, key, value)
if strings.HasSuffix(http.CanonicalHeaderKey(key), http.CanonicalHeaderKey("X-Http-Code")) {
codeStr := value[0]
Expand Down
19 changes: 19 additions & 0 deletions gateway/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ type BinaryDecoder struct {
marshaler runtime.Marshaler
}

// type JSONDecoder struct{
// *runtime.DecoderWrapper
// }
// func (d *JSONDecoder) Decode(v interface{}) error {
// if response, ok := v.(map[string]interface{}); ok {
// if _, ok := response["result"]; ok {
// v = response["result"]
// }

// }
// if msg,ok:=v.(protoreflect.Message);ok{

// }
// return d.DecoderWrapper.Decode(v)

// }
// var typeOfBytes = reflect.TypeOf([]byte(nil))
// var typeOfHttpbody = reflect.TypeOf(&httpbody.HttpBody{})

Expand Down Expand Up @@ -198,3 +214,6 @@ func (m *JSONMarshaler) Marshal(v interface{}) ([]byte, error) {
func (m *JSONMarshaler) ContentType(v interface{}) string {
return "application/json"
}
func (m *JSONMarshaler) NewDecoder(r io.Reader) runtime.Decoder {
return NewMaskDecoder(m.JSONPb.NewDecoder(r))
}
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ require (
github.com/cockroachdb/errors v1.11.1
github.com/google/wire v0.6.0
github.com/smartystreets/goconvey v1.8.1
github.com/spark-lence/tiga v0.0.0-20240628071333-f5e34bac7593
github.com/spark-lence/tiga v0.0.0-20240704075559-c825086a1249
github.com/spf13/cobra v1.8.0
google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d
google.golang.org/grpc v1.64.0
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
)

Expand Down Expand Up @@ -80,7 +80,7 @@ require (
require (
github.com/agiledragon/gomonkey/v2 v2.11.0
github.com/begonia-org/go-loadbalancer v0.0.0-20240519060752-71ca464f0f1a
github.com/begonia-org/go-sdk v0.0.0-20240628071225-2864d45934ea
github.com/begonia-org/go-sdk v0.0.0-20240704075659-182a1008f0ab
github.com/go-git/go-git/v5 v5.11.0
github.com/go-playground/validator/v10 v10.19.0
github.com/gorilla/websocket v1.5.0
Expand Down Expand Up @@ -139,7 +139,7 @@ require (
go.uber.org/zap v1.27.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)

Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ github.com/begonia-org/go-layered-cache v0.0.0-20240510102605-41bdb7aa07fa h1:DH
github.com/begonia-org/go-layered-cache v0.0.0-20240510102605-41bdb7aa07fa/go.mod h1:xEqoca1vNGqH8CV7X9EzhDV5Ihtq9J95p7ZipzUB6pc=
github.com/begonia-org/go-loadbalancer v0.0.0-20240519060752-71ca464f0f1a h1:Mpw7T+90KC5QW7yCa8Nn/5psnlvsexipAOrQAcc7YE0=
github.com/begonia-org/go-loadbalancer v0.0.0-20240519060752-71ca464f0f1a/go.mod h1:crPS67sfgmgv47psftwfmTMbmTfdepVm8MPeqApINlI=
github.com/begonia-org/go-sdk v0.0.0-20240628071225-2864d45934ea h1:jdDBLZVsGKfmF/V+U4WUrz4Hzd3/62GqMIly9gnSppw=
github.com/begonia-org/go-sdk v0.0.0-20240628071225-2864d45934ea/go.mod h1:I70a3fiAADGrOoOC3lv408rFcTRhTwLt3pwr6cQwB4Y=
github.com/begonia-org/go-sdk v0.0.0-20240704075659-182a1008f0ab h1:zr5JyiG4eIkM80SIkRiu/4ulX0735OMS5vPnzB+9e0s=
github.com/begonia-org/go-sdk v0.0.0-20240704075659-182a1008f0ab/go.mod h1:2mHpFudwolu6RHF18EX+lnFYyTNnwDxBD6JcfRcahz8=
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
Expand Down Expand Up @@ -229,8 +229,8 @@ github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sS
github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spark-lence/tiga v0.0.0-20240628071333-f5e34bac7593 h1:wbjjiTqJWiAIQemNAE1Nw71roZQ8sshGJ70XHwBx8b4=
github.com/spark-lence/tiga v0.0.0-20240628071333-f5e34bac7593/go.mod h1:MSL8X9t+qvpQ4Tq3vVPKncq9RJcCzF2XGEWkCuNhm6Q=
github.com/spark-lence/tiga v0.0.0-20240704075559-c825086a1249 h1:Rk1qEMiMzTeZddgGotXAtXNK4sryZh2hhG3i5ncFQ0Y=
github.com/spark-lence/tiga v0.0.0-20240704075559-c825086a1249/go.mod h1:h7BTZeR6xD6+tr3ClEhHC1PeXPOn3jRt7NnThQg1JvE=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
Expand Down Expand Up @@ -393,12 +393,12 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d h1:Aqf0fiIdUQEj0Gn9mKFFXoQfTTEaNopWpfVyYADxiSg=
google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Od4k8V1LQSizPRUK4OzZ7TBE/20k+jPczUDAEyvn69Y=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d h1:k3zyW3BYYR30e8v3x0bTDdE9vpYFjZHK+HcyqkrppWk=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 h1:0+ozOGcrp+Y8Aq8TLNN2Aliibms5LEzsq99ZZmAGYm0=
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094/go.mod h1:fJ/e3If/Q67Mj99hin0hMhiNyCRmt6BQ2aWIJshUSJw=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 h1:BwIjyKYGsK9dMCBOorzRri8MQwmi7mT9rGHsCEinZkA=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/cenkalti/backoff.v1 v1.1.0 h1:Arh75ttbsvlpVA7WtVpH4u9h6Zl46xuptxqLxPiSo4Y=
Expand Down
4 changes: 2 additions & 2 deletions internal/biz/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func (a *AppUsecase) CreateApp(ctx context.Context, in *api.AppsRequest, owner s
appid := GenerateAppid(a.snowflake)
accessKey, err := GenerateAppAccessKey()
if err != nil {
return nil, gosdk.NewError(err, int32(api.APPSvrCode_APP_CREATE_ERR), codes.Internal, "generate_app_access_key")
return nil, gosdk.NewError(err, int32(common.Code_INTERNAL_ERROR), codes.Internal, "generate_app_access_key")

}
secret, err := GenerateAppSecret()
if err != nil {
return nil, gosdk.NewError(err, int32(api.APPSvrCode_APP_CREATE_ERR), codes.Internal, "generate_app_secret_key")
return nil, gosdk.NewError(err, int32(common.Code_INTERNAL_ERROR), codes.Internal, "generate_app_secret_key")
}
app := a.newApp()
app.AccessKey = accessKey
Expand Down
5 changes: 4 additions & 1 deletion internal/biz/biz.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ var ProviderSet = wire.NewSet(NewAuthzUsecase,
endpoint.NewEndpointUsecase,
NewAppUsecase,
endpoint.NewWatcher,
NewDataOperatorUsecase)
NewDataOperatorUsecase,
NewTenantUsecase,
NewBusinessUsecase,
)
Loading

0 comments on commit bf13367

Please sign in to comment.