interface.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package main
  2. import (
  3. "context"
  4. plugin "github.com/hashicorp/go-plugin"
  5. "google.golang.org/grpc"
  6. )
  7. // Handshake is a common handshake that is shared by plugin and host.
  8. var Handshake = plugin.HandshakeConfig{
  9. // This isn't required when using VersionedPlugins
  10. ProtocolVersion: 1,
  11. MagicCookieKey: "BASIC_PLUGIN",
  12. MagicCookieValue: "hello",
  13. }
  14. // KV is the interface that we're exposing as a plugin.
  15. type Notifier interface {
  16. Notify(ctx context.Context, notification *Notification) (*Empty, error)
  17. Configure(ctx context.Context, config *Config) (*Empty, error)
  18. }
  19. // This is the implementation of plugin.NotifierPlugin so we can serve/consume this.
  20. type NotifierPlugin struct {
  21. // GRPCPlugin must still implement the Plugin interface
  22. plugin.Plugin
  23. // Concrete implementation, written in Go. This is only used for plugins
  24. // that are written in Go.
  25. Impl Notifier
  26. }
  27. type GRPCClient struct{ client NotifierClient }
  28. func (m *GRPCClient) Notify(ctx context.Context, notification *Notification) (*Empty, error) {
  29. _, err := m.client.Notify(context.Background(), notification)
  30. return &Empty{}, err
  31. }
  32. func (m *GRPCClient) Configure(ctx context.Context, config *Config) (*Empty, error) {
  33. _, err := m.client.Configure(context.Background(), config)
  34. return &Empty{}, err
  35. }
  36. // Here is the gRPC server that GRPCClient talks to.
  37. type GRPCServer struct {
  38. // This is the real implementation
  39. Impl Notifier
  40. }
  41. func (p *NotifierPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
  42. RegisterNotifierServer(s, p.Impl)
  43. return nil
  44. }
  45. func (p *NotifierPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
  46. return &GRPCClient{client: NewNotifierClient(c)}, nil
  47. }