github.com/hashicorp/go-plugin@v1.6.0/examples/grpc/plugin-go-grpc/main.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"io/ioutil"
     9  
    10  	"github.com/hashicorp/go-plugin"
    11  	"github.com/hashicorp/go-plugin/examples/grpc/shared"
    12  )
    13  
    14  // Here is a real implementation of KV that writes to a local file with
    15  // the key name and the contents are the value of the key.
    16  type KV struct{}
    17  
    18  func (KV) Put(key string, value []byte) error {
    19  	value = []byte(fmt.Sprintf("%s\n\nWritten from plugin-go-grpc", string(value)))
    20  	return ioutil.WriteFile("kv_"+key, value, 0644)
    21  }
    22  
    23  func (KV) Get(key string) ([]byte, error) {
    24  	return ioutil.ReadFile("kv_" + key)
    25  }
    26  
    27  func main() {
    28  	plugin.Serve(&plugin.ServeConfig{
    29  		HandshakeConfig: shared.Handshake,
    30  		Plugins: map[string]plugin.Plugin{
    31  			"kv": &shared.KVGRPCPlugin{Impl: &KV{}},
    32  		},
    33  
    34  		// A non-nil value here enables gRPC serving for this plugin...
    35  		GRPCServer: plugin.DefaultGRPCServer,
    36  	})
    37  }