更新 tpl/server.go

This commit is contained in:
yanweidong 2025-04-17 23:16:13 +08:00
parent 9c3ac59467
commit cf7e34c61d
1 changed files with 17 additions and 1 deletions

View File

@ -13,6 +13,7 @@ import (
"git.apinb.com/bsm-sdk/core/vars" "git.apinb.com/bsm-sdk/core/vars"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/reflection" "google.golang.org/grpc/reflection"
"google.golang.org/protobuf/proto"
gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
) )
@ -23,7 +24,9 @@ type Server struct {
} }
func New(addr string) *Server { 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 service to grpc.Server
{register} {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 = ` var Server = `