2 Commits

Author SHA1 Message Date
cf7e34c61d 更新 tpl/server.go 2025-04-17 23:16:13 +08:00
9c3ac59467 更新 tpl/logic.go 2025-04-17 22:02:56 +08:00
2 changed files with 18 additions and 2 deletions

View File

@@ -45,7 +45,7 @@ var FetchValidCode = `
var StatusReplyCode = `
return &pb.StatusReply{
Status: 0,
Code: 0,
Message: "OK",
Timeseq: time.Now().UnixNano(),
}, nil

View File

@@ -13,6 +13,7 @@ import (
"git.apinb.com/bsm-sdk/core/vars"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"google.golang.org/protobuf/proto"
gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
)
@@ -23,7 +24,9 @@ type Server struct {
}
func New(addr string) *Server {
srv := &Server{Ctx: context.Background(), Grpc: grpc.NewServer(), Mux: gwRuntime.NewServeMux()}
srv := &Server{Ctx: context.Background(), Grpc: grpc.NewServer(), Mux: gwRuntime.NewServeMux(
gwRuntime.WithForwardResponseRewriter(responseEnvelope),
)}
// register service to grpc.Server
{register}
@@ -58,6 +61,19 @@ func (s *Server) RegisterSwagger() {
})
}
// response envelope
func responseEnvelope(_ context.Context, response proto.Message) (interface{}, error) {
name := string(response.ProtoReflect().Descriptor().Name())
if name == "Status" || name == "Error" || name == "StatusReply" {
return response, nil
}
return map[string]any{
"code": 0,
"message": string(response.ProtoReflect().Descriptor().Name()),
"result": response,
}, nil
}
`
var Server = `