Files
goproxy/examples/plugin/example/openapi_example.go
DarkiT cc4c677553 增强插件系统:添加参数验证功能
- 在插件助手中新增 ValidateParams 函数,用于验证传入参数是否符合定义,确保参数的完整性和正确性。
- 更新 ExecuteAction 方法,集成参数验证逻辑,提升插件执行的安全性和可靠性。
- 示例程序中添加新的选项,展示如何生成插件API文档和OpenAPI/Swagger文档,增强用户体验。

此更新提升了插件系统的健壮性,便于开发者更好地管理和使用插件功能。
2025-03-14 13:32:36 +08:00

92 lines
2.2 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"
"os"
"path/filepath"
"github.com/darkit/goproxy/examples/plugin"
)
// 展示OpenAPI文档生成功能
func OpenAPIGeneratorExample() {
// 创建上下文
ctx := context.Background()
// 获取插件目录
pluginsDir := filepath.Join("..", "plugins", "dist")
if _, err := os.Stat(pluginsDir); os.IsNotExist(err) {
fmt.Printf("插件目录不存在: %s\n", pluginsDir)
return
}
// 创建插件管理器
pm := plugin.NewPluginManager(pluginsDir)
// 加载插件
fmt.Println("正在加载插件...")
if err := pm.LoadPlugins(); err != nil {
fmt.Printf("加载插件失败: %v\n", err)
return
}
// 初始化所有插件
if err := pm.InitPlugins(ctx); err != nil {
fmt.Printf("初始化插件失败: %v\n", err)
return
}
// 启动所有插件
if err := pm.StartPlugins(ctx); err != nil {
fmt.Printf("启动插件失败: %v\n", err)
return
}
// 创建文档输出目录
apiDocsDir := "./openapi_docs"
if err := os.MkdirAll(apiDocsDir, 0755); err != nil {
fmt.Printf("创建API文档目录失败: %v\n", err)
return
}
fmt.Printf("将在 %s 目录生成OpenAPI文档\n", apiDocsDir)
// 创建OpenAPI生成器
openAPIGen := plugin.NewOpenAPIGenerator(pm, apiDocsDir)
// 设置文档信息
openAPIGen.SetInfo(
"插件系统API",
"插件系统提供的所有API接口包括插件管理和操作执行",
"1.0.0",
)
// 设置联系人信息
openAPIGen.SetContact(
"插件系统开发团队",
"plugins@example.com",
"https://example.com/plugins",
)
// 生成OpenAPI文档
fmt.Println("正在生成OpenAPI文档...")
if err := openAPIGen.GenerateOpenAPI(); err != nil {
fmt.Printf("生成OpenAPI文档失败: %v\n", err)
} else {
fmt.Printf("成功生成OpenAPI文档文档位于: %s\n", apiDocsDir)
fmt.Println("文档包括:")
fmt.Println("- OpenAPI规范文件 (openapi.json)")
fmt.Println("- Swagger UI 界面 (index.html)")
fmt.Printf("打开 %s/index.html 可以在浏览器中查看API文档\n", apiDocsDir)
}
// 停止所有插件
if err := pm.StopPlugins(ctx); err != nil {
fmt.Printf("停止插件失败: %v\n", err)
return
}
fmt.Println("\n示例结束")
}