forked from go-chassis/go-chassis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.go
executable file
·45 lines (37 loc) · 1.06 KB
/
bootstrap.go
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
package bootstrap
import (
"github.com/go-chassis/go-chassis/core/lager"
)
var bootstrapPlugins = make([]*PluginItem, 0)
//PluginItem include name and plugin implementation
type PluginItem struct {
Name string
Plugin BootstrapPlugin
}
//BootstrapPlugin is a interface which declares Init method
type BootstrapPlugin interface {
Init() error
}
// The BootstrapFunc type is an adapter to allow the use of ordinary functions as bootstrapPlugin.
type BootstrapFunc func() error
//Init is a method
func (b BootstrapFunc) Init() error {
return b()
}
//InstallPlugin is a function which installs plugin,
// during initiating of go chassis, plugins will be executed
func InstallPlugin(name string, plugin BootstrapPlugin) {
bootstrapPlugins = append(bootstrapPlugins, &PluginItem{
Name: name,
Plugin: plugin,
})
}
//Bootstrap will boot plugins in orders
func Bootstrap() {
for _, bp := range bootstrapPlugins {
lager.Logger.Info("Bootstrap " + bp.Name)
if err := bp.Plugin.Init(); err != nil {
lager.Logger.Errorf(err, "Failed to init %s.", bp.Name)
}
}
}