/
core.go
123 lines (106 loc) · 2.79 KB
/
core.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
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 (
"fmt"
"github.com/myrteametrics/myrtea-engine-api/v5/plugins/assistant"
"github.com/myrteametrics/myrtea-engine-api/v5/plugins/baseline"
"github.com/myrteametrics/myrtea-engine-api/v5/plugins/pluginutils"
"github.com/myrteametrics/myrtea-engine-api/v5/plugins/standalone"
"go.uber.org/zap"
"net/http"
)
// MyrteaPlugin is a standard interface for any myrtea plugins
type MyrteaPlugin interface {
ServicePort() int
HandlerPrefix() string
Handler() http.Handler
Start() error
Stop() error
Running() bool
}
type Plugin struct {
Config pluginutils.PluginConfig
Plugin MyrteaPlugin
}
type Core struct {
Plugins []Plugin
}
// RegisterPlugins Registers all plugins that were added into the TOML config file
func (c *Core) RegisterPlugins() {
zap.L().Info("Registering plugins...")
pluginConfigs, err := pluginutils.LoadPluginConfig()
if err != nil {
zap.L().Error("Couldn't parse plugin config", zap.Error(err))
return
}
for _, config := range pluginConfigs {
zap.L().Info(fmt.Sprintf("Registering plugin %s on port %d", config.Name, config.Port))
if c.PluginExists(config.Name) {
zap.L().Warn(fmt.Sprintf("Plugin %s already registered, skipping", config.Name))
continue
}
plugin := Plugin{
Config: config,
}
switch config.Name {
case "assistant":
if a := assistant.NewAssistantPlugin(config); a != nil {
plugin.Plugin = a
} else {
continue
}
break
case "baseline":
if b := baseline.NewBaselinePlugin(config); b != nil {
plugin.Plugin = b
} else {
continue
}
break
default: // default is standalone plugins (no bi-directional communications needed)
if s := standalone.NewPlugin(config); s != nil {
plugin.Plugin = s
} else {
continue
}
break
}
c.Plugins = append(c.Plugins, plugin)
}
if len(c.Plugins) > 0 {
zap.L().Info(fmt.Sprintf("Registered %d plugins !", len(c.Plugins)))
} else {
zap.L().Info("No plugins registered")
}
}
// Start starts all plugins
func (c *Core) Start() {
for _, plugin := range c.Plugins {
err := plugin.Plugin.Start()
if err != nil {
zap.L().Error(fmt.Sprintf("Couldn't start plugin %s", plugin.Config.Name), zap.Error(err))
}
}
}
// Stop stops all plugins registered in core
func (c *Core) Stop() {
for _, plugin := range c.Plugins {
err := plugin.Plugin.Stop()
if err != nil {
zap.L().Error(fmt.Sprintf("Couldn't stop plugin %s", plugin.Config.Name), zap.Error(err))
}
}
}
// PluginExists checks if a plugin with the given name exists
func (c *Core) PluginExists(name string) (exists bool) {
_, exists = c.GetPlugin(name)
return
}
// GetPlugin returns a plugin by its name
func (c *Core) GetPlugin(name string) (MyrteaPlugin, bool) {
for _, p := range c.Plugins {
if p.Config.Name == name {
return p.Plugin, true
}
}
return nil, false
}