forked from hashicorp/go-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (48 loc) · 1.14 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"encoding/json"
"io/ioutil"
"github.com/lvrongnan/go-plugin"
"github.com/lvrongnan/go-plugin/examples/bidirectional/shared"
)
// Here is a real implementation of KV that writes to a local file with
// the key name and the contents are the value of the key.
type Counter struct {
}
type data struct {
Value int64
}
func (k *Counter) Put(key string, value int64, a shared.AddHelper) error {
v, _ := k.Get(key)
r, err := a.Sum(v, value)
if err != nil {
return err
}
buf, err := json.Marshal(&data{r})
if err != nil {
return err
}
return ioutil.WriteFile("kv_"+key, buf, 0644)
}
func (k *Counter) Get(key string) (int64, error) {
dataRaw, err := ioutil.ReadFile("kv_" + key)
if err != nil {
return 0, err
}
data := &data{}
err = json.Unmarshal(dataRaw, data)
if err != nil {
return 0, err
}
return data.Value, nil
}
func main() {
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: shared.Handshake,
Plugins: map[string]plugin.Plugin{
"counter": &shared.CounterPlugin{Impl: &Counter{}},
},
// A non-nil value here enables gRPC serving for this plugin...
GRPCServer: plugin.DefaultGRPCServer,
})
}