mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-29 12:10:19 +08:00
Caddy 式进程内模块系统(详见 docs/plugin-architecture/MODULE-AUTHOR-GUIDE.md): - internal/plugin:命名空间注册表(init 注册/重复 panic)、Provision→Validate→Start→Stop 生命周期(Start 半途逆序回滚、Stop 逆序+ctx deadline)、Host 四项 ports 能力面、 modules: 配置子树(enabled 三态;viper 含点 key 手工提取)、Runtime 状态机 - internal/plugin/plugintest:模块测试夹具(NewHost Options + RunLifecycle) - internal/modules:standard/imports.go 唯一插装清单 + job.hello 示例模块(默认 disabled) - Wire/main 接入:Build+Start 失败 fail-fast(显式 Cleanup 防 leader 锁泄漏)、 runtime.Stop 在 cleanup 并行组首位(先于 Redis/Ent) - admin 可观测:GET /api/v1/admin/modules(只读,错误文本脱敏) - 默认配置零行为变更(Phase-0 安全网 + bench allocs 零漂移验证) 含两轮对抗式审计修复(1×P1 启动失败泄漏 leader 锁 + 11×P2)与回归测试。
61 lines
1.8 KiB
Go
61 lines
1.8 KiB
Go
package admin
|
|
|
|
import (
|
|
"sort"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
|
|
"github.com/Wei-Shaw/sub2api/internal/plugin"
|
|
"github.com/Wei-Shaw/sub2api/internal/util/logredact"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// moduleStatusSource 提供插件模块状态快照能力(*plugin.Runtime 实现该接口,测试可注入桩)。
|
|
type moduleStatusSource interface {
|
|
Snapshot() []plugin.ModuleStatus
|
|
}
|
|
|
|
// ModuleHandler handles plugin module observability operations (read-only)
|
|
type ModuleHandler struct {
|
|
runtime moduleStatusSource
|
|
}
|
|
|
|
// NewModuleHandler creates a new ModuleHandler
|
|
func NewModuleHandler(runtime *plugin.Runtime) *ModuleHandler {
|
|
return &ModuleHandler{runtime: runtime}
|
|
}
|
|
|
|
// ModuleStatusItem 是单个插件模块状态的响应条目
|
|
type ModuleStatusItem struct {
|
|
ID string `json:"id"`
|
|
Namespace string `json:"namespace"`
|
|
Name string `json:"name"`
|
|
Enabled bool `json:"enabled"`
|
|
State string `json:"state"`
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
// List 返回全部已注册插件模块的状态清单(含未启用模块),按模块 ID 字典序排序
|
|
// GET /api/v1/admin/modules
|
|
func (h *ModuleHandler) List(c *gin.Context) {
|
|
statuses := h.runtime.Snapshot()
|
|
modules := make([]ModuleStatusItem, 0, len(statuses))
|
|
for _, status := range statuses {
|
|
errText := status.Err
|
|
if errText != "" {
|
|
// 错误文本可能包含敏感信息(如上游返回的凭据片段),输出前统一脱敏。
|
|
errText = logredact.RedactText(errText)
|
|
}
|
|
modules = append(modules, ModuleStatusItem{
|
|
ID: string(status.ID),
|
|
Namespace: status.ID.Namespace(),
|
|
Name: status.ID.Name(),
|
|
Enabled: status.Enabled,
|
|
State: string(status.State),
|
|
Error: errText,
|
|
})
|
|
}
|
|
sort.Slice(modules, func(i, j int) bool { return modules[i].ID < modules[j].ID })
|
|
response.Success(c, gin.H{"modules": modules})
|
|
}
|