Files
goproxy/examples/plugin/manager/plugin_manager.go
DarkiT 7affdc79c6 update: 2023-03-15
1. 添加URL重写器
2. 添加插件系统
2025-03-14 00:30:20 +08:00

170 lines
3.8 KiB
Go

package main
import (
"context"
"fmt"
"runtime"
"github.com/darkit/goproxy/examples/plugin"
)
// 这是内置插件的示例实现
// LogPlugin 日志插件
type LogPlugin struct {
name string
version string
description string
author string
enabled bool
level string
}
// 创建内置日志插件
func NewLogPlugin() *LogPlugin {
return &LogPlugin{
name: "InternalLogPlugin",
version: "1.0.0",
description: "内置日志插件,在不支持动态加载插件的平台上使用",
author: "开发者",
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
}
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.Println("内置日志插件已初始化,日志级别:", p.level)
return nil
}
func (p *LogPlugin) Start(ctx context.Context) error {
fmt.Println("内置日志插件已启动")
return nil
}
func (p *LogPlugin) Stop(ctx context.Context) error {
fmt.Println("内置日志插件已停止")
return nil
}
// Log 记录日志
func (p *LogPlugin) Log(message string) {
fmt.Printf("[%s] %s\n", 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)
// 在不支持动态加载的系统上使用内置插件
logPlugin := NewLogPlugin()
pm.RegisterPlugin(logPlugin)
}
// 加载插件
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",
i, info.Name, info.Version, info.Description, status, info.Author)
// 打印插件配置
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\n", info.Name)
if p, ok := pm.GetPlugin(info.Name); ok {
// 这里可以根据插件类型执行特定操作
if logPlugin, ok := p.(*LogPlugin); ok {
logPlugin.Log("这是一条测试日志消息")
}
}
break
}
}
}
// 停止插件
fmt.Println("\n正在停止插件...")
if err := pm.StopPlugins(ctx); err != nil {
fmt.Printf("停止插件失败: %v\n", err)
}
fmt.Println("示例结束")
}