github.com/yaoapp/kun@v0.9.0/grpc/grpc.go (about) 1 package grpc 2 3 import ( 4 "encoding/json" 5 6 "github.com/yaoapp/kun/grpc/proto" 7 "golang.org/x/net/context" 8 ) 9 10 // ClientGRPC is an implementation of KV that talks over RPC. 11 type ClientGRPC struct{ client proto.ModelClient } 12 13 // Exec execute the plugin model 14 func (m *ClientGRPC) Exec(name string, args ...interface{}) (*Response, error) { 15 16 payload, err := json.Marshal(args) 17 if err != nil { 18 return nil, err 19 } 20 21 res, err := m.client.Exec(context.Background(), &proto.Request{ 22 Name: name, 23 Payload: payload, 24 }) 25 if err != nil { 26 return nil, err 27 } 28 29 return &Response{Bytes: res.Response, Type: res.Type}, nil 30 } 31 32 // ServerGRPC Here is the gRPC server that ClientGRPC talks to. 33 type ServerGRPC struct { 34 // This is the real implementation 35 Impl Model 36 } 37 38 // Exec ServerGet 39 func (m *ServerGRPC) Exec(ctx context.Context, req *proto.Request) (*proto.Response, error) { 40 args := []interface{}{} 41 err := json.Unmarshal(req.Payload, &args) 42 if err != nil { 43 return nil, err 44 } 45 v, err := m.Impl.Exec(req.Name, args...) 46 return &proto.Response{Response: v.Bytes, Type: v.Type}, err 47 }