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