github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/plugin/plugin.go (about) 1 package plugin 2 3 import ( 4 "net/http" 5 6 "github.com/urfave/cli/v2" 7 ) 8 9 // Plugin is the interface for plugins to micro. It differs from go-micro in that it's for 10 // the micro API, Web, Sidecar, CLI. It's a method of building middleware for the HTTP side. 11 type Plugin interface { 12 // Global Flags 13 Flags() []cli.Flag 14 // Sub-commands 15 Commands() []*cli.Command 16 // Handle is the middleware handler for HTTP requests. We pass in 17 // the existing handler so it can be wrapped to create a call chain. 18 Handler() Handler 19 // Init called when command line args are parsed. 20 // The initialised cli.Context is passed in. 21 Init(*cli.Context) error 22 // Name of the plugin 23 String() string 24 } 25 26 // Manager is the plugin manager which stores plugins and allows them to be retrieved. 27 // This is used by all the components of micro. 28 type Manager interface { 29 Plugins(...PluginOption) []Plugin 30 Register(Plugin, ...PluginOption) error 31 } 32 33 type PluginOptions struct { 34 Module string 35 } 36 37 type PluginOption func(o *PluginOptions) 38 39 // Module will scope the plugin to a specific module, e.g. the "api" 40 func Module(m string) PluginOption { 41 return func(o *PluginOptions) { 42 o.Module = m 43 } 44 } 45 46 // Handler is the plugin middleware handler which wraps an existing http.Handler passed in. 47 // Its the responsibility of the Handler to call the next http.Handler in the chain. 48 type Handler func(http.Handler) http.Handler 49 50 type plugin struct { 51 opts Options 52 init func(ctx *cli.Context) error 53 handler Handler 54 } 55 56 func (p *plugin) Flags() []cli.Flag { 57 return p.opts.Flags 58 } 59 60 func (p *plugin) Commands() []*cli.Command { 61 return p.opts.Commands 62 } 63 64 func (p *plugin) Handler() Handler { 65 return p.handler 66 } 67 68 func (p *plugin) Init(ctx *cli.Context) error { 69 return p.opts.Init(ctx) 70 } 71 72 func (p *plugin) String() string { 73 return p.opts.Name 74 } 75 76 func newPlugin(opts ...Option) Plugin { 77 options := Options{ 78 Name: "default", 79 Init: func(ctx *cli.Context) error { return nil }, 80 } 81 82 for _, o := range opts { 83 o(&options) 84 } 85 86 handler := func(hdlr http.Handler) http.Handler { 87 for _, h := range options.Handlers { 88 hdlr = h(hdlr) 89 } 90 return hdlr 91 } 92 93 return &plugin{ 94 opts: options, 95 handler: handler, 96 } 97 } 98 99 // Plugins lists the global plugins 100 func Plugins(opts ...PluginOption) []Plugin { 101 return defaultManager.Plugins(opts...) 102 } 103 104 // Register registers a global plugins 105 func Register(plugin Plugin, opts ...PluginOption) error { 106 return defaultManager.Register(plugin, opts...) 107 } 108 109 // IsRegistered check plugin whether registered global. 110 // Notice plugin is not check whether is nil 111 func IsRegistered(plugin Plugin, opts ...PluginOption) bool { 112 return defaultManager.isRegistered(plugin, opts...) 113 } 114 115 // NewManager creates a new plugin manager 116 func NewManager() Manager { 117 return newManager() 118 } 119 120 // NewPlugin makes it easy to create a new plugin 121 func NewPlugin(opts ...Option) Plugin { 122 return newPlugin(opts...) 123 }