Skip to content

Commit

Permalink
feat(#159): auto load plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
qubbei committed Aug 10, 2023
1 parent c5c3fc9 commit bb8bef1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
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
}
}
}

0 comments on commit bb8bef1

Please sign in to comment.