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)与回归测试。
123 lines
4.4 KiB
Go
123 lines
4.4 KiB
Go
package plugin
|
||
|
||
import (
|
||
"fmt"
|
||
"strconv"
|
||
|
||
"github.com/mitchellh/mapstructure"
|
||
)
|
||
|
||
// enabledKey 是每个模块配置项中由内核保留解释的键,其余键全部属于模块私有配置。
|
||
const enabledKey = "enabled"
|
||
|
||
// ModuleConfig 是单个模块在 `modules:` 配置子树中的配置项。
|
||
type ModuleConfig struct {
|
||
// Enabled 是模块的显式启停开关;nil 表示未显式配置,
|
||
// 由模块注册时声明的 ModuleInfo.EnabledByDefault 决定。
|
||
Enabled *bool
|
||
|
||
// Raw 是模块的私有配置(去掉 enabled 键之后的剩余部分),
|
||
// 由 Config.Of 解码到模块自定义的配置 struct,内核不解释其内容。
|
||
Raw map[string]any
|
||
}
|
||
|
||
// Config 是 `modules:` 配置子树的解析结果:模块 ID → 模块配置。
|
||
//
|
||
// 缺省(子树不存在)时为空 map,所有模块按各自默认 enabled 值运行,
|
||
// 行为与未引入插件内核前完全一致。
|
||
type Config map[ModuleID]ModuleConfig
|
||
|
||
// ParseConfig 将全局配置中 `modules:` 子树的原始映射解析为 Config。
|
||
//
|
||
// raw 的顶层 key 必须是合法的模块 ID 字面量(如 "job.hello",YAML 中带点的
|
||
// 完整 key),value 为该模块的配置映射;其中 enabled 键被内核解释为启停开关
|
||
// (接受 bool 或可解析为 bool 的字符串),其余键原样保留为模块私有配置。
|
||
//
|
||
// 允许出现"格式合法但未注册"的模块 ID(例如不同编译变体的配置共用),
|
||
// 这类配置项会被 Runtime 忽略;但 ID 格式非法或 enabled 类型错误会返回错误,
|
||
// 以便尽早暴露配置笔误。
|
||
//
|
||
// 注意:全局配置经 Viper 加载时,所有 key(含模块 ID 与私有配置键)会被
|
||
// 静默小写化——配置文件里写 "Job.Hello" 实际到达这里时已是 "job.hello"。
|
||
// 因此模块 ID 与私有配置 struct 的 mapstructure 标签必须全小写。
|
||
func ParseConfig(raw map[string]map[string]any) (Config, error) {
|
||
cfg := make(Config, len(raw))
|
||
for key, entry := range raw {
|
||
id := ModuleID(key)
|
||
if err := id.validate(); err != nil {
|
||
return nil, fmt.Errorf("plugin config: invalid module ID in modules config: %w", err)
|
||
}
|
||
mc := ModuleConfig{Raw: make(map[string]any, len(entry))}
|
||
for k, v := range entry {
|
||
if k == enabledKey {
|
||
// YAML 中写空的 `enabled:`(值为 null)按"未显式配置"处理,
|
||
// 走模块默认值——与 Enabled=nil 的三态语义一致(审计 C-1)。
|
||
if v == nil {
|
||
continue
|
||
}
|
||
b, err := toBool(v)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("plugin config: module %q: invalid enabled value: %w", id, err)
|
||
}
|
||
mc.Enabled = &b
|
||
continue
|
||
}
|
||
mc.Raw[k] = v
|
||
}
|
||
cfg[id] = mc
|
||
}
|
||
return cfg, nil
|
||
}
|
||
|
||
// Of 将模块 id 的 raw 私有配置解码到 out(必须是指向配置 struct 的指针)。
|
||
//
|
||
// 解码行为与项目全局配置(Viper 默认解码器)保持一致:
|
||
// mapstructure 标签、弱类型转换、字符串到 time.Duration / 逗号分隔切片的钩子。
|
||
// 模块未配置或私有配置为空时不修改 out(模块应自带默认值并返回 nil);
|
||
// 类型不匹配等解码失败时返回包含模块 ID 的错误。
|
||
func (c Config) Of(id ModuleID, out any) error {
|
||
mc, ok := c[id]
|
||
if !ok || len(mc.Raw) == 0 {
|
||
return nil
|
||
}
|
||
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
||
Result: out,
|
||
WeaklyTypedInput: true,
|
||
DecodeHook: mapstructure.ComposeDecodeHookFunc(
|
||
mapstructure.StringToTimeDurationHookFunc(),
|
||
mapstructure.StringToSliceHookFunc(","),
|
||
),
|
||
})
|
||
if err != nil {
|
||
return fmt.Errorf("plugin config: build decoder for module %q: %w", id, err)
|
||
}
|
||
if err := decoder.Decode(mc.Raw); err != nil {
|
||
return fmt.Errorf("plugin config: decode config for module %q: %w", id, err)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// enabledFor 解析模块的最终启停状态:显式配置优先,否则用注册时声明的默认值。
|
||
func (c Config) enabledFor(info ModuleInfo) bool {
|
||
if mc, ok := c[info.ID]; ok && mc.Enabled != nil {
|
||
return *mc.Enabled
|
||
}
|
||
return info.EnabledByDefault
|
||
}
|
||
|
||
// toBool 将 enabled 配置值弱类型转换为 bool(接受 bool 与 strconv.ParseBool 可解析的字符串)。
|
||
func toBool(v any) (bool, error) {
|
||
switch val := v.(type) {
|
||
case bool:
|
||
return val, nil
|
||
case string:
|
||
b, err := strconv.ParseBool(val)
|
||
if err != nil {
|
||
return false, fmt.Errorf("cannot parse %q as bool", val)
|
||
}
|
||
return b, nil
|
||
default:
|
||
return false, fmt.Errorf("expected bool, got %T", v)
|
||
}
|
||
}
|