!61 middle_msg

Merge pull request !61 from Sodesnei/feat-plugin-install
This commit is contained in:
bandl 2021-10-19 06:52:06 +00:00 committed by Gitee
commit 6ef4654c63
3 changed files with 92 additions and 1 deletions

47
doc/pkg/middle/middle.md Normal file
View File

@ -0,0 +1,47 @@
### 中间件调用
### 创建事件
event := event.NewEvent("logcontext")
### 创建驱动
middleware := NewMiddleWare()
### 将事件推入驱动
middleware.eventProduce.Call(ctx, event)
### 获取驱动的事件
middleware.eventConsumer.Reciver(ctx)
### 插件接口
type MiddleToolsInterface interface {
Init() // 初始化
Exec(interface{}) (interface{}, error) // 处理用户发送事件
Name() string // 获取中间件名称
Describe() string // 描述
}
### 插件的New方法规定为 NewMiddleWare()
每个插件都要定义NewMiddleWare()
### 将插件名 “logMiddle” 注册到配置文件wheat-cache.yaml,其他插件注册
plugins-control:
logcontext: ["logMiddle"]

View File

@ -1,5 +1,34 @@
package middlemsg
import (
"context"
"gitee.com/timedb/wheatCache/pkg/errorx"
"gitee.com/timedb/wheatCache/pkg/event"
)
const (
MiddleMsgKey = "middleMsgKey"
)
func SendMiddleMsg(
ctx context.Context,
middleProduce event.ProduceInterface,
val interface{},
) error {
if middleProduce == nil {
return errorx.New("middleProduce not is nil")
}
var eventName string
switch val.(type) {
case LogContext:
eventName = EventNameLog
}
msgEvent := event.NewEvent(eventName)
msgEvent.SetValue(MiddleMsgKey, val)
middleProduce.Call(ctx, msgEvent)
return nil
}

View File

@ -1,8 +1,23 @@
package middlemsg
import (
"time"
)
// []pulginsINfo
// 1. Version
// 2. desc
// 3. Name
// 4. 运行状态
// 5. 运行时间
const (
EventNamePlug = "plugins-info"
)
type PulginsInfo struct {
Version string
Desc string
Name string
Statux string
Time time.Duration
}