@@ -2,60 +2,43 @@ package grpc
22
33import (
44 "context"
5- "encoding/json"
65
76 "github.com/hashicorp/go-plugin"
87 "github.com/yaoapp/kun/grpc/proto"
9- "github.com/yaoapp/kun/maps"
108 "google.golang.org/grpc"
119)
1210
13- // Handshake is a common handshake that is shared by plugin and host.
14- var Handshake = plugin.HandshakeConfig {
15- // This isn't required when using VersionedPlugins
16- ProtocolVersion : 1 ,
17- MagicCookieKey : "MODEL_PLUGIN" ,
18- MagicCookieValue : "hello" ,
19- }
11+ // Level represents a log level.
12+ type Level int32
2013
21- // PluginMap is the map of plugins we can dispense.
22- var PluginMap = map [ string ]plugin. Plugin {
23- "model" : & ModelGRPCPlugin {},
24- }
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
2518
26- // Model is the interface that we're exposing as a plugin.
27- type Model interface {
28- Exec (name string , args ... interface {}) (* Response , error )
29- }
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
3022
31- // Response GRPC Response
32- type Response struct {
33- Bytes []byte
34- }
23+ // Debug information for programmer lowlevel analysis.
24+ Debug Level = 2
3525
36- // Bind bind struct
37- func (res Response ) Bind (v interface {}) error {
38- return json .Unmarshal (res .Bytes , & v )
39- }
26+ // Info information about steady state operations.
27+ Info Level = 3
4028
41- // Map cast to map
42- func (res Response ) Map () (maps.MapStrAny , error ) {
43- v := maps .MakeMapStrAny ()
44- err := json .Unmarshal (res .Bytes , & v )
45- if err != nil {
46- return nil , err
47- }
48- return v , nil
49- }
29+ // Warn information about rare but handled events.
30+ Warn Level = 4
5031
51- // MustMap cast to map
52- func (res Response ) MustMap () maps.MapStrAny {
53- v := maps .MakeMapStrAny ()
54- err := json .Unmarshal (res .Bytes , & v )
55- if err != nil {
56- panic (err )
57- }
58- return v
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 )
5942}
6043
6144// ModelGRPCPlugin This is the implementation of plugin.Plugin so we can serve/consume this.
@@ -67,6 +50,25 @@ type ModelGRPCPlugin struct {
6750 Impl Model
6851}
6952
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+
7072// GRPCServer the GRPC Server
7173func (p * ModelGRPCPlugin ) GRPCServer (broker * plugin.GRPCBroker , s * grpc.Server ) error {
7274 proto .RegisterModelServer (s , & ServerGRPC {Impl : p .Impl })
0 commit comments