Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#159): auto load plugin #169

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions engine/load_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// └──────┘ └──────┘ └──────┘
func (e *RuleEngine) LoadPlugin(sectionK string, p typex.XPlugin) error {
section := utils.GetINISection(core.INIPath, sectionK)
key, err1 := section.GetKey("enable")
/*key, err1 := section.GetKey("enable")
if err1 != nil {
return err1
}
Expand All @@ -25,7 +25,7 @@ func (e *RuleEngine) LoadPlugin(sectionK string, p typex.XPlugin) error {
if !enable {
glogger.GLogger.Infof("Plugin is not enable:%s", p.PluginMetaInfo().Name)
return nil
}
}*/

if err := p.Init(section); err != nil {
return err
Expand Down
71 changes: 60 additions & 11 deletions engine/runner.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package engine

import (
mqttserver "github.com/hootrhino/rulex/plugin/mqtt_server"
netdiscover "github.com/hootrhino/rulex/plugin/net_discover"
ttyterminal "github.com/hootrhino/rulex/plugin/ttyd_terminal"
usbmonitor "github.com/hootrhino/rulex/plugin/usb_monitor"
"gopkg.in/ini.v1"
"os"
"os/signal"
"strings"
"syscall"

"github.com/hootrhino/rulex/core"
"github.com/hootrhino/rulex/glogger"
httpserver "github.com/hootrhino/rulex/plugin/http_server"
icmpsender "github.com/hootrhino/rulex/plugin/icmp_sender"
mqttserver "github.com/hootrhino/rulex/plugin/mqtt_server"
"github.com/hootrhino/rulex/typex"
)

Expand Down Expand Up @@ -39,22 +44,15 @@ func RunRulex(iniPath string) {
signal.Notify(c, syscall.SIGINT, syscall.SIGABRT, syscall.SIGTERM)
engine := NewRuleEngine(mainConfig)
engine.Start()

// Load Plugin
loadPlugin(engine)
// Load Http api Server
httpServer := httpserver.NewHttpApiServer()
if err := engine.LoadPlugin("plugin.http_server", httpServer); err != nil {
glogger.GLogger.Error(err)
return
}
mqttServer := mqttserver.NewMqttServer()
if err := engine.LoadPlugin("plugin.mqtt_server", mqttServer); err != nil {
glogger.GLogger.Error(err)
return
}
icmpSender := icmpsender.NewICMPSender()
if err := engine.LoadPlugin("plugin.icmpsender", icmpSender); err != nil {
glogger.GLogger.Error(err)
return
}
//
// Load inend from sqlite
//
Expand Down Expand Up @@ -118,3 +116,54 @@ func RunRulex(iniPath string) {
engine.Stop()
os.Exit(0)
}

// loadPlugin 根据Ini配置信息,加载插件
func loadPlugin(engine typex.RuleX) {
cfg, _ := ini.ShadowLoad(core.INIPath)
sections := cfg.ChildSections("plugin")
for _, section := range sections {
name := strings.TrimPrefix(section.Name(), "plugin.")
key, err1 := section.GetKey("enable")
if err1 != nil {
glogger.GLogger.Error(err1)
continue
}
enable, err2 := key.Bool()
if err2 != nil {
glogger.GLogger.Error(err2)
continue
}
if !enable {
glogger.GLogger.Infof("Plugin is not enable:%s", name)
continue
}
var plugin typex.XPlugin
if name == "mqtt_server" {
plugin = mqttserver.NewMqttServer()
goto lab
}
if name == "usbmonitor" {
plugin = usbmonitor.NewUsbMonitor()
goto lab
}
if name == "icmpsender" {
plugin = icmpsender.NewICMPSender()
goto lab
}
if name == "netdiscover" {
plugin = netdiscover.NewNetDiscover()
goto lab
}
if name == "ttyd" {
plugin = ttyterminal.NewWebTTYPlugin()
goto lab
} else {
continue
}
lab:
if err := engine.LoadPlugin(section.Name(), plugin); err != nil {
glogger.GLogger.Error(err)
return
}
}
}
Loading