增加:
1. ✅ 监控指标收集 2. ✅ 中间件机制 3. ✅ 配置热更新 4. ✅ 优雅关闭 5. ✅ 插件系统 6. ✅ API文档 7. ✅ 认证授权系统 8. ✅ 请求/响应压缩优化
This commit is contained in:
78
examples/plugins/example/example.go
Normal file
78
examples/plugins/example/example.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ExamplePlugin 示例插件
|
||||
type ExamplePlugin struct {
|
||||
// 插件配置
|
||||
config map[string]interface{}
|
||||
// 插件状态
|
||||
running bool
|
||||
}
|
||||
|
||||
// Name 插件名称
|
||||
func (p *ExamplePlugin) Name() string {
|
||||
return "example"
|
||||
}
|
||||
|
||||
// Version 插件版本
|
||||
func (p *ExamplePlugin) Version() string {
|
||||
return "1.0.0"
|
||||
}
|
||||
|
||||
// Init 初始化插件
|
||||
func (p *ExamplePlugin) Init(ctx context.Context) error {
|
||||
log.Printf("初始化插件 %s v%s\n", p.Name(), p.Version())
|
||||
|
||||
// 初始化配置
|
||||
p.config = make(map[string]interface{})
|
||||
p.config["startTime"] = time.Now()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start 启动插件
|
||||
func (p *ExamplePlugin) Start(ctx context.Context) error {
|
||||
log.Printf("启动插件 %s\n", p.Name())
|
||||
|
||||
// 启动后台任务
|
||||
go func() {
|
||||
p.running = true
|
||||
ticker := time.NewTicker(5 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for p.running {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
log.Printf("插件 %s 正在运行...\n", p.Name())
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop 停止插件
|
||||
func (p *ExamplePlugin) Stop(ctx context.Context) error {
|
||||
log.Printf("停止插件 %s\n", p.Name())
|
||||
|
||||
// 停止后台任务
|
||||
p.running = false
|
||||
|
||||
// 清理资源
|
||||
if startTime, ok := p.config["startTime"].(time.Time); ok {
|
||||
duration := time.Since(startTime)
|
||||
log.Printf("插件 %s 运行时长: %v\n", p.Name(), duration)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Plugin 导出插件实例
|
||||
var Plugin = &ExamplePlugin{}
|
||||
Reference in New Issue
Block a user