github.com/yaoapp/kun@v0.9.0/grpc/intefaces.go (about) 1 package grpc 2 3 import ( 4 "context" 5 6 "github.com/hashicorp/go-plugin" 7 "github.com/yaoapp/kun/grpc/proto" 8 "google.golang.org/grpc" 9 ) 10 11 // Level represents a log level. 12 type Level int32 13 14 const ( 15 // NoLevel is a special level used to indicate that no level has been 16 // set and allow for a default to be used. 17 NoLevel Level = 0 18 19 // Trace is the most verbose level. Intended to be used for the tracing 20 // of actions in code, such as function enters/exits, etc. 21 Trace Level = 1 22 23 // Debug information for programmer lowlevel analysis. 24 Debug Level = 2 25 26 // Info information about steady state operations. 27 Info Level = 3 28 29 // Warn information about rare but handled events. 30 Warn Level = 4 31 32 // Error information about unrecoverable events. 33 Error Level = 5 34 35 // Off disables all logging output. 36 Off Level = 6 37 ) 38 39 // Model is the interface that we're exposing as a plugin. 40 type Model interface { 41 Exec(name string, args ...interface{}) (*Response, error) 42 } 43 44 // ModelGRPCPlugin This is the implementation of plugin.Plugin so we can serve/consume this. 45 type ModelGRPCPlugin struct { 46 // GRPCPlugin must still implement the Plugin interface 47 plugin.Plugin 48 // Concrete implementation, written in Go. This is only used for plugins 49 // that are written in Go. 50 Impl Model 51 } 52 53 // Handshake is a common handshake that is shared by plugin and host. 54 var Handshake = plugin.HandshakeConfig{ 55 // This isn't required when using VersionedPlugins 56 ProtocolVersion: 1, 57 MagicCookieKey: "GOU_MODEL_PLUGIN", 58 MagicCookieValue: "GOU VER0.6.0", 59 } 60 61 // PluginMap is the map of plugins we can dispense. 62 var PluginMap = map[string]plugin.Plugin{ 63 "model": &ModelGRPCPlugin{}, 64 } 65 66 // Response GRPC Response 67 type Response struct { 68 Bytes []byte 69 Type string 70 } 71 72 // GRPCServer the GRPC Server 73 func (p *ModelGRPCPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error { 74 proto.RegisterModelServer(s, &ServerGRPC{Impl: p.Impl}) 75 return nil 76 } 77 78 // GRPCClient the GRPC client 79 func (p *ModelGRPCPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) { 80 return &ClientGRPC{client: proto.NewModelClient(c)}, nil 81 }