增强插件系统:添加插件执行功能
- 在插件接口和基本插件实现中新增 Execute 方法,支持插件功能的动态执行。 - 更新各个插件(如日志插件、统计插件、存储插件等)以实现 Execute 方法,允许通过操作名称和参数执行特定功能。 - 在插件管理器中添加 ExecutePlugin、ExecutePluginsByType 和 ExecuteAllPlugins 方法,便于批量执行插件操作。 - 示例程序中更新插件调用方式,展示如何使用 Execute 方法进行操作。 此更新提升了插件系统的灵活性和可扩展性,便于开发者动态管理和执行插件功能。
This commit is contained in:
@@ -104,6 +104,62 @@ func (p *LoggerPlugin) Stop(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Execute 执行插件功能
|
||||
func (p *LoggerPlugin) Execute(ctx context.Context, action string, params map[string]interface{}) (interface{}, error) {
|
||||
switch action {
|
||||
case "log":
|
||||
// 需要参数: level, message
|
||||
level, ok := params["level"].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("缺少必需参数: level")
|
||||
}
|
||||
message, ok := params["message"].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("缺少必需参数: message")
|
||||
}
|
||||
p.Log(level, message)
|
||||
return true, nil
|
||||
|
||||
case "info":
|
||||
// 需要参数: message
|
||||
message, ok := params["message"].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("缺少必需参数: message")
|
||||
}
|
||||
p.Info(message)
|
||||
return true, nil
|
||||
|
||||
case "warn":
|
||||
// 需要参数: message
|
||||
message, ok := params["message"].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("缺少必需参数: message")
|
||||
}
|
||||
p.Warn(message)
|
||||
return true, nil
|
||||
|
||||
case "error":
|
||||
// 需要参数: message
|
||||
message, ok := params["message"].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("缺少必需参数: message")
|
||||
}
|
||||
p.Error(message)
|
||||
return true, nil
|
||||
|
||||
case "getLoggerStatus":
|
||||
// 不需要参数
|
||||
status := map[string]interface{}{
|
||||
"initialized": p.logger != nil,
|
||||
"config": p.config,
|
||||
}
|
||||
return status, nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("未知的操作: %s", action)
|
||||
}
|
||||
}
|
||||
|
||||
// Log 记录日志
|
||||
func (p *LoggerPlugin) Log(level, message string) {
|
||||
if p.logger == nil {
|
||||
|
@@ -109,6 +109,115 @@ func (p *StatsPlugin) Stop(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Execute 执行插件功能
|
||||
func (p *StatsPlugin) Execute(ctx context.Context, action string, params map[string]interface{}) (interface{}, error) {
|
||||
switch action {
|
||||
case "incrementStat":
|
||||
// 需要参数: name, value
|
||||
name, ok := params["name"].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("缺少必需参数: name")
|
||||
}
|
||||
|
||||
// 处理整数值参数
|
||||
var value int64
|
||||
if floatValue, ok := params["value"].(float64); ok {
|
||||
value = int64(floatValue)
|
||||
} else if strValue, ok := params["value"].(string); ok {
|
||||
var err error
|
||||
_, err = fmt.Sscanf(strValue, "%d", &value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("参数value必须是整数: %v", err)
|
||||
}
|
||||
} else {
|
||||
return nil, fmt.Errorf("缺少必需参数: value")
|
||||
}
|
||||
|
||||
p.IncrementStat(name, value)
|
||||
return true, nil
|
||||
|
||||
case "getStat":
|
||||
// 需要参数: name
|
||||
name, ok := params["name"].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("缺少必需参数: name")
|
||||
}
|
||||
|
||||
value := p.GetStat(name)
|
||||
return value, nil
|
||||
|
||||
case "getAllStats":
|
||||
// 不需要参数
|
||||
return p.GetAllStats(), nil
|
||||
|
||||
case "recordRequest":
|
||||
// 需要参数: bytesReceived, bytesSent, isError
|
||||
var bytesReceived, bytesSent int64
|
||||
var isError bool
|
||||
|
||||
// 处理bytesReceived参数
|
||||
if floatValue, ok := params["bytesReceived"].(float64); ok {
|
||||
bytesReceived = int64(floatValue)
|
||||
} else {
|
||||
return nil, fmt.Errorf("缺少必需参数: bytesReceived")
|
||||
}
|
||||
|
||||
// 处理bytesSent参数
|
||||
if floatValue, ok := params["bytesSent"].(float64); ok {
|
||||
bytesSent = int64(floatValue)
|
||||
} else {
|
||||
return nil, fmt.Errorf("缺少必需参数: bytesSent")
|
||||
}
|
||||
|
||||
// 处理isError参数
|
||||
if value, ok := params["isError"].(bool); ok {
|
||||
isError = value
|
||||
}
|
||||
|
||||
p.RecordRequest(bytesReceived, bytesSent, isError)
|
||||
return true, nil
|
||||
|
||||
case "resetStats":
|
||||
// 不需要参数
|
||||
p.mu.Lock()
|
||||
p.stats = make(map[string]int64)
|
||||
p.stats["requests"] = 0
|
||||
p.stats["errors"] = 0
|
||||
p.stats["bytes_sent"] = 0
|
||||
p.stats["bytes_received"] = 0
|
||||
p.startTime = time.Now()
|
||||
p.mu.Unlock()
|
||||
return true, nil
|
||||
|
||||
case "getStatsReport":
|
||||
// 生成统计报告
|
||||
report := p.generateStatsReport()
|
||||
return report, nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("未知的操作: %s", action)
|
||||
}
|
||||
}
|
||||
|
||||
// generateStatsReport 生成统计报告
|
||||
func (p *StatsPlugin) generateStatsReport() map[string]interface{} {
|
||||
p.mu.RLock()
|
||||
defer p.mu.RUnlock()
|
||||
|
||||
uptime := time.Since(p.startTime).Seconds()
|
||||
report := map[string]interface{}{
|
||||
"uptime_seconds": uptime,
|
||||
"stats": p.stats,
|
||||
}
|
||||
|
||||
if uptime > 0 && p.stats["requests"] > 0 {
|
||||
report["requests_per_second"] = float64(p.stats["requests"]) / uptime
|
||||
report["error_rate"] = float64(p.stats["errors"]) * 100 / float64(p.stats["requests"])
|
||||
}
|
||||
|
||||
return report
|
||||
}
|
||||
|
||||
// logStats 记录当前统计信息
|
||||
func (p *StatsPlugin) logStats() {
|
||||
p.mu.RLock()
|
||||
|
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -69,6 +70,83 @@ func (p *StoragePlugin) Stop(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Execute 执行插件功能
|
||||
func (p *StoragePlugin) Execute(ctx context.Context, action string, params map[string]interface{}) (interface{}, error) {
|
||||
switch action {
|
||||
case "saveFile":
|
||||
// 需要参数: filename, data
|
||||
filename, ok := params["filename"].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("缺少必需参数: filename")
|
||||
}
|
||||
|
||||
// 处理两种数据格式:字符串或Base64编码的二进制数据
|
||||
var data []byte
|
||||
if dataStr, ok := params["data"].(string); ok {
|
||||
// 检查是否为Base64编码
|
||||
if base64Str, ok := params["isBase64"].(bool); ok && base64Str {
|
||||
var err error
|
||||
data, err = base64.StdEncoding.DecodeString(dataStr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Base64解码失败: %v", err)
|
||||
}
|
||||
} else {
|
||||
data = []byte(dataStr)
|
||||
}
|
||||
} else {
|
||||
return nil, fmt.Errorf("缺少必需参数: data")
|
||||
}
|
||||
|
||||
err := p.SaveFile(filename, data)
|
||||
return err == nil, err
|
||||
|
||||
case "loadFile":
|
||||
// 需要参数: filename, returnBase64
|
||||
filename, ok := params["filename"].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("缺少必需参数: filename")
|
||||
}
|
||||
|
||||
returnBase64, _ := params["returnBase64"].(bool)
|
||||
|
||||
data, err := p.LoadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if returnBase64 {
|
||||
return base64.StdEncoding.EncodeToString(data), nil
|
||||
}
|
||||
return string(data), nil
|
||||
|
||||
case "deleteFile":
|
||||
// 需要参数: filename
|
||||
filename, ok := params["filename"].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("缺少必需参数: filename")
|
||||
}
|
||||
|
||||
err := p.DeleteFile(filename)
|
||||
return err == nil, err
|
||||
|
||||
case "listFiles":
|
||||
// 不需要参数
|
||||
files, err := p.ListFiles()
|
||||
return files, err
|
||||
|
||||
case "getStorageInfo":
|
||||
// 不需要参数
|
||||
info := map[string]interface{}{
|
||||
"storageDir": p.storageDir,
|
||||
"config": p.config,
|
||||
}
|
||||
return info, nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("未知的操作: %s", action)
|
||||
}
|
||||
}
|
||||
|
||||
// SaveFile 保存文件
|
||||
func (p *StoragePlugin) SaveFile(filename string, data []byte) error {
|
||||
p.mu.Lock()
|
||||
|
Reference in New Issue
Block a user