diff --git a/config/cmd/cmd.go b/config/cmd/cmd.go index beb4ba97..f80827d2 100644 --- a/config/cmd/cmd.go +++ b/config/cmd/cmd.go @@ -92,6 +92,11 @@ var ( EnvVar: "MICRO_CLIENT_POOL_TTL", Usage: "Sets the client connection pool ttl. e.g 500ms, 5s, 1m. Default: 1m", }, + cli.StringSliceFlag{ + Name: "plugin", + EnvVar: "MICRO_PLUGIN", + Usage: "Comma separated list of plugins e.g /path/to/plugin.so", + }, cli.IntFlag{ Name: "register_ttl", EnvVar: "MICRO_REGISTER_TTL", diff --git a/plugin/plugin.go b/plugin/plugin.go index c648bfaf..c1aecc6e 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -32,3 +32,15 @@ var ( func NewPlugin() Plugin { return &plugin{} } + +func Build(path string, c *Config) error { + return DefaultPlugin.Build(path, c) +} + +func Load(path string) (*Config, error) { + return DefaultPlugin.Load(path) +} + +func Init(c *Config) error { + return DefaultPlugin.Init(c) +} diff --git a/service.go b/service.go index e612a4b3..eed7f8ac 100644 --- a/service.go +++ b/service.go @@ -3,6 +3,7 @@ package micro import ( "os" "os/signal" + "strings" "sync" "syscall" @@ -10,7 +11,9 @@ import ( "github.com/micro/go-micro/config/cmd" "github.com/micro/go-micro/debug/handler" "github.com/micro/go-micro/metadata" + "github.com/micro/go-micro/plugin" "github.com/micro/go-micro/server" + "github.com/micro/go-micro/util/log" ) type service struct { @@ -44,6 +47,24 @@ func (s *service) Init(opts ...Option) { } s.once.Do(func() { + // setup the plugins + for _, p := range strings.Split(os.Getenv("MICRO_PLUGIN"), ",") { + if len(p) == 0 { + continue + } + + // load the plugin + c, err := plugin.Load(p) + if err != nil { + log.Fatal(err) + } + + // initialise the plugin + if err := plugin.Init(c); err != nil { + log.Fatal(err) + } + } + // Initialise the command flags, overriding new service _ = s.opts.Cmd.Init( cmd.Broker(&s.opts.Broker),