Files
goproxy/examples/plugin/manager/plugin_manager.go
DarkiT 0990e8c42c 增强插件系统:添加硬件插件类型及默认插件构造函数
- 在插件类型中新增硬件插件类型(PluginTypeHardware),以支持更多插件类型的扩展。
- 添加便捷的构造函数 NewBasePluginWithDefaultType 和 NewPluginWithDefaultType,简化插件创建过程,适用于不需要指定特殊类型的场景。
- 更新日志插件和统计插件的创建示例,展示如何使用默认插件类型。

此更新提升了插件系统的灵活性,便于开发者快速创建和管理插件。
2025-03-14 11:14:47 +08:00

220 lines
5.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"context"
"fmt"
"runtime"
"github.com/darkit/goproxy/examples/plugin"
)
// 这是内置插件的示例实现
// LogPlugin 日志插件
type LogPlugin struct {
name string
version string
description string
author string
pluginType plugin.PluginType
enabled bool
level string
}
// 创建内置日志插件 - 示例1明确指定插件类型
func NewLogPlugin() *LogPlugin {
return &LogPlugin{
name: "InternalLogPlugin",
version: "1.0.0",
description: "内置日志插件,在不支持动态加载插件的平台上使用",
author: "开发者",
pluginType: plugin.PluginTypeUtils, // 明确指定为工具类插件
enabled: true,
level: "info",
}
}
// 创建内置日志插件 - 示例2使用默认插件类型通用插件
func NewDefaultLogPlugin() *LogPlugin {
return &LogPlugin{
name: "DefaultLogPlugin",
version: "1.0.0",
description: "使用默认类型的内置日志插件示例",
author: "开发者",
pluginType: plugin.PluginTypeGeneral, // 自动设置为通用插件类型
enabled: true,
level: "info",
}
}
// 实现Plugin接口
func (p *LogPlugin) Name() string {
return p.name
}
func (p *LogPlugin) Version() string {
return p.version
}
func (p *LogPlugin) Description() string {
return p.description
}
func (p *LogPlugin) Author() string {
return p.author
}
// Type 实现插件类型方法
func (p *LogPlugin) Type() plugin.PluginType {
return p.pluginType
}
func (p *LogPlugin) IsEnabled() bool {
return p.enabled
}
func (p *LogPlugin) SetEnabled(enabled bool) {
p.enabled = enabled
}
func (p *LogPlugin) Init(ctx context.Context, config map[string]interface{}) error {
if level, ok := config["level"].(string); ok {
p.level = level
}
fmt.Printf("内置日志插件[%s]已初始化,日志级别: %s, 插件类型: %s\n", p.name, p.level, p.Type())
return nil
}
func (p *LogPlugin) Start(ctx context.Context) error {
fmt.Printf("内置日志插件[%s]已启动\n", p.name)
return nil
}
func (p *LogPlugin) Stop(ctx context.Context) error {
fmt.Printf("内置日志插件[%s]已停止\n", p.name)
return nil
}
// Log 记录日志
func (p *LogPlugin) Log(message string) {
fmt.Printf("[%s][%s] %s\n", p.name, p.level, message)
}
func main() {
// 创建插件管理器
pluginsDir := "./plugins"
pm := plugin.NewPluginManager(pluginsDir)
// 检查当前系统是否支持动态加载插件
if pm.IsDynamicLoadingSupported() {
fmt.Printf("当前系统(%s)支持动态加载插件\n", runtime.GOOS)
} else {
fmt.Printf("当前系统(%s)不支持动态加载插件\n", runtime.GOOS)
// 在不支持动态加载的系统上注册内置插件
// 示例1注册明确指定类型的插件
logPlugin := NewLogPlugin()
pm.RegisterPlugin(logPlugin)
// 示例2注册使用默认类型的插件
defaultLogPlugin := NewDefaultLogPlugin()
pm.RegisterPlugin(defaultLogPlugin)
fmt.Println("已注册两个内置插件以展示不同的插件类型设置方式")
}
// 加载插件
fmt.Println("正在加载插件...")
if err := pm.LoadPlugins(); err != nil {
fmt.Printf("加载插件失败: %v\n", err)
}
// 配置插件
logPluginConfig := map[string]interface{}{
"level": "debug",
}
if err := pm.SetPluginConfig("InternalLogPlugin", logPluginConfig); err != nil {
fmt.Printf("配置插件失败: %v\n", err)
}
// 初始化插件
ctx := context.Background()
fmt.Println("正在初始化插件...")
if err := pm.InitPlugins(ctx); err != nil {
fmt.Printf("初始化插件失败: %v\n", err)
}
// 启动插件
fmt.Println("正在启动插件...")
if err := pm.StartPlugins(ctx); err != nil {
fmt.Printf("启动插件失败: %v\n", err)
}
// 获取并打印所有插件信息
fmt.Println("\n已加载的插件列表:")
plugins := pm.GetPluginInfos()
for i, info := range plugins {
status := "启用"
if !info.Enabled {
status = "禁用"
}
fmt.Printf("[%d] %s (v%s) - %s [%s]\n作者: %s\n类型: %s\n",
i, info.Name, info.Version, info.Description, status, info.Author, info.Type)
// 打印插件配置
if len(info.Config) > 0 {
fmt.Println("配置:")
for k, v := range info.Config {
fmt.Printf(" %s: %v\n", k, v)
}
}
fmt.Println()
}
// 使用第一个启用的插件(这里只是示例)
if len(plugins) > 0 {
for _, info := range plugins {
if info.Enabled {
fmt.Printf("正在使用插件: %s (类型: %s)\n", info.Name, info.Type)
if p, ok := pm.GetPlugin(info.Name); ok {
// 这里可以根据插件类型执行特定操作
if logPlugin, ok := p.(*LogPlugin); ok {
logPlugin.Log("这是一条测试日志消息")
}
}
break
}
}
}
// 按类型获取和使用插件
fmt.Println("\n按类型获取插件:")
// 获取工具类插件
utilsPlugins := pm.GetPluginsByType(plugin.PluginTypeUtils)
fmt.Printf("找到 %d 个工具类插件\n", len(utilsPlugins))
for _, p := range utilsPlugins {
fmt.Printf("工具类插件: %s\n", p.Name())
if logPlugin, ok := p.(*LogPlugin); ok {
logPlugin.Log("通过类型获取到的日志插件记录消息")
}
}
// 获取通用类型插件
generalPlugins := pm.GetPluginsByType(plugin.PluginTypeGeneral)
fmt.Printf("找到 %d 个通用类型插件\n", len(generalPlugins))
for _, p := range generalPlugins {
fmt.Printf("通用类型插件: %s\n", p.Name())
if logPlugin, ok := p.(*LogPlugin); ok {
logPlugin.Log("通过类型获取到的通用类型插件记录消息")
}
}
// 停止插件
fmt.Println("\n正在停止插件...")
if err := pm.StopPlugins(ctx); err != nil {
fmt.Printf("停止插件失败: %v\n", err)
}
fmt.Println("示例结束")
}