github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/docs/v2/plugins/plugins-runtime.md (about)

     1  ---
     2  title: Runtime Plugins
     3  keywords: plugins
     4  tags: [plugins]
     5  sidebar: home_sidebar
     6  permalink: /plugins-runtime
     7  summary: 
     8  ---
     9  
    10  Plugins are a way of integrating external code into the Micro toolkit. This is completely separate to go-micro plugins. 
    11  Using plugins here allows you to add additional flags, commands and HTTP handlers to the toolkit. 
    12  
    13  ## How it works
    14  
    15  There is a global plugin manager under micro/plugin which consists of plugins that will be used across the entire toolkit. 
    16  Plugins can be registered by calling `plugin.Register`. Each component (api, web, proxy, cli, bot) has a separate 
    17  plugin manager used to register plugins which should only be added as part of that component. They can be used in 
    18  the same way by called `api.Register`, `web.Register`, etc.
    19  
    20  Here's the interface
    21  
    22  ```go
    23  // Plugin is the interface for plugins to micro. It differs from go-micro in that it's for
    24  // the micro API, Web, Proxy, CLI. It's a method of building middleware for the HTTP side.
    25  type Plugin interface {
    26  	// Global Flags
    27  	Flags() []cli.Flag
    28  	// Sub-commands
    29  	Commands() []cli.Command
    30  	// Handle is the middleware handler for HTTP requests. We pass in
    31  	// the existing handler so it can be wrapped to create a call chain.
    32  	Handler() Handler
    33  	// Init called when command line args are parsed.
    34  	// The initialised cli.Context is passed in.
    35  	Init(*cli.Context) error
    36  	// Name of the plugin
    37  	String() string
    38  }
    39  
    40  // Manager is the plugin manager which stores plugins and allows them to be retrieved.
    41  // This is used by all the components of micro.
    42  type Manager interface {
    43          Plugins() map[string]Plugin
    44          Register(name string, plugin Plugin) error
    45  }
    46  
    47  // Handler is the plugin middleware handler which wraps an existing http.Handler passed in.
    48  // Its the responsibility of the Handler to call the next http.Handler in the chain.
    49  type Handler func(http.Handler) http.Handler
    50  ```
    51  
    52  ## How to use it
    53  
    54  Here's a simple example of a plugin that adds a flag and then prints the value
    55  
    56  ### The plugin
    57  
    58  Create a plugin.go file in the top level dir
    59  
    60  ```go
    61  package main
    62  
    63  import (
    64  	"log"
    65  	"github.com/micro/cli"
    66  	"github.com/tickoalcantara12/micro/plugin"
    67  )
    68  
    69  func init() {
    70  	plugin.Register(plugin.NewPlugin(
    71  		plugin.WithName("example"),
    72  		plugin.WithFlag(cli.StringFlag{
    73  			Name:   "example_flag",
    74  			Usage:  "This is an example plugin flag",
    75  			EnvVar: "EXAMPLE_FLAG",
    76  			Value: "avalue",
    77  		}),
    78  		plugin.WithInit(func(ctx *cli.Context) error {
    79  			log.Println("Got value for example_flag", ctx.String("example_flag"))
    80  			return nil
    81  		}),
    82  	))
    83  }
    84  ```
    85  
    86  ### Building the code
    87  
    88  Simply build micro with the plugin
    89  
    90  ```shell
    91  go build -o micro ./main.go ./plugin.go
    92  ```
    93  
    94  ## Repository
    95  
    96  The plugins for the toolkit can be found in [github.com/micro/go-plugins/micro](https://github.com/micro/go-plugins/tree/master/micro).
    97