diff --git a/examples/bidirectional/.gitignore b/examples/bidirectional/.gitignore new file mode 100644 index 0000000..69f8fee --- /dev/null +++ b/examples/bidirectional/.gitignore @@ -0,0 +1,3 @@ +/counter +/counter-* +/kv_* diff --git a/examples/bidirectional/shared/grpc.go b/examples/bidirectional/shared/grpc.go index 85edbd8..74d6dd8 100644 --- a/examples/bidirectional/shared/grpc.go +++ b/examples/bidirectional/shared/grpc.go @@ -77,7 +77,7 @@ func (m *GRPCServer) Get(ctx context.Context, req *proto.GetRequest) (*proto.Get return &proto.GetResponse{Value: v}, err } -// GRPCClient is an implementation of KV that talks over RPC. +// GRPCAddHelperClient is an implementation of AddHelper that talks over RPC. type GRPCAddHelperClient struct{ client proto.AddHelperClient } func (m *GRPCAddHelperClient) Sum(a, b int64) (int64, error) { @@ -92,7 +92,7 @@ func (m *GRPCAddHelperClient) Sum(a, b int64) (int64, error) { return resp.R, err } -// Here is the gRPC server that GRPCClient talks to. +// Here is the gRPC server that GRPCAddHelperClient talks to. type GRPCAddHelperServer struct { // This is the real implementation Impl AddHelper diff --git a/examples/bidirectional/shared/interface.go b/examples/bidirectional/shared/interface.go index a12c329..23594b4 100644 --- a/examples/bidirectional/shared/interface.go +++ b/examples/bidirectional/shared/interface.go @@ -29,7 +29,7 @@ type AddHelper interface { Sum(int64, int64) (int64, error) } -// KV is the interface that we're exposing as a plugin. +// Counter is the interface that we're exposing as a plugin. type Counter interface { Put(key string, value int64, a AddHelper) error Get(key string) (int64, error) diff --git a/examples/grpc/README.md b/examples/grpc/README.md index 63c4af5..5a71c90 100644 --- a/examples/grpc/README.md +++ b/examples/grpc/README.md @@ -10,7 +10,8 @@ $ go build -o kv # This builds the plugin written in Go $ go build -o kv-go-grpc ./plugin-go-grpc -# This tells the KV binary to use the "kv-go-grpc" binary +# This tells the KV binary to use the "kv-go-grpc" binary over gRPC +$ export KV_PLUGIN_NAME=kv_grpc $ export KV_PLUGIN="./kv-go-grpc" # Read and write @@ -24,11 +25,12 @@ world This plugin uses gRPC to serve a plugin that is written in Go: -``` +```sh # This builds the plugin written in Go $ go build -o kv-go-grpc ./plugin-go-grpc -# This tells the KV binary to use the "kv-go-grpc" binary +# This tells the KV binary to use the "kv-go-grpc" binary over gRPC +$ export KV_PLUGIN_NAME=kv_grpc $ export KV_PLUGIN="./kv-go-grpc" ``` @@ -36,11 +38,12 @@ $ export KV_PLUGIN="./kv-go-grpc" This plugin uses the builtin Go net/rpc mechanism to serve the plugin: -``` +```sh # This builds the plugin written in Go $ go build -o kv-go-netrpc ./plugin-go-netrpc -# This tells the KV binary to use the "kv-go-netrpc" binary +# This tells the KV binary to use the "kv-go-netrpc" binary over net/rpc +$ export KV_PLUGIN_NAME=kv $ export KV_PLUGIN="./kv-go-netrpc" ``` @@ -48,10 +51,11 @@ $ export KV_PLUGIN="./kv-go-netrpc" This plugin is written in Python: -``` +```sh $ python -m venv plugin-python/.venv $ source plugin-python/.venv/bin/activate $ pip install -r plugin-python/requirements.txt +$ export KV_PLUGIN_NAME=kv_grpc $ export KV_PLUGIN="python plugin-python/plugin.py" ``` diff --git a/examples/grpc/main.go b/examples/grpc/main.go index bb9ab42..d150d74 100644 --- a/examples/grpc/main.go +++ b/examples/grpc/main.go @@ -31,8 +31,19 @@ func run() error { return err } + var pluginName string + switch os.Getenv("KV_PROTO") { + case "netrpc": + pluginName = shared.PluginNetRPC + case "grpc": + pluginName = shared.PluginGRPC + default: + fmt.Println("must set KV_PROTO to netrpc or grpc") + os.Exit(1) + } + // Request the plugin - raw, err := rpcClient.Dispense("kv_grpc") + raw, err := rpcClient.Dispense(pluginName) if err != nil { return err } diff --git a/examples/grpc/plugin-python/plugin.py b/examples/grpc/plugin-python/plugin.py index 0bc57db..a1a8849 100644 --- a/examples/grpc/plugin-python/plugin.py +++ b/examples/grpc/plugin-python/plugin.py @@ -25,7 +25,7 @@ def Get(self, request, context): def Put(self, request, context): filename = "kv_"+request.key - value = "{0}\n\nWritten from plugin-python".format(request.value) + value = "{0}\n\nWritten from plugin-python".format(request.value.decode('utf-8')) with open(filename, 'w') as f: f.write(value) diff --git a/examples/grpc/shared/interface.go b/examples/grpc/shared/interface.go index 2fad93f..140af04 100644 --- a/examples/grpc/shared/interface.go +++ b/examples/grpc/shared/interface.go @@ -14,6 +14,11 @@ import ( "github.com/hashicorp/go-plugin/examples/grpc/proto" ) +const ( + PluginNetRPC = "kv" + PluginGRPC = "kv_grpc" +) + // Handshake is a common handshake that is shared by plugin and host. var Handshake = plugin.HandshakeConfig{ // This isn't required when using VersionedPlugins @@ -24,8 +29,8 @@ var Handshake = plugin.HandshakeConfig{ // PluginMap is the map of plugins we can dispense. var PluginMap = map[string]plugin.Plugin{ - "kv_grpc": &KVGRPCPlugin{}, - "kv": &KVPlugin{}, + PluginGRPC: &KVGRPCPlugin{}, + PluginNetRPC: &KVPlugin{}, } // KV is the interface that we're exposing as a plugin.