Documentation
¶
Overview ¶
config.go 站点配置模型与校验:SystemConfig/UserSettings 类型、默认值、 写库前校验(纯函数簇,自 admin 域原样迁入)。
Package config 全站配置中心域:system_configs 单行 JSON 的唯一写者。
2026-10-10 自 admin 域独立——此前三段职责挤在 admin:管理面 API、治理操作 (用户/文件/统计/维护)、以及被 user/storage/bootstrap/mcp 四面消费的 全站配置中心(~500 行)。配置中心的归属错位使 admin 成为事实上的上帝域, 且"管理端在线设置"的读写方横跨四个域。独立后:
- 本域持有:内存缓存+懒加载、写穿 DB、校验、全局 conf overlay 热应用、 组件热重建钩子(ReconfigureHooks)、多副本变更广播回调、运行时存储 配置持久化(storage.RuntimePersister 实现)、用户设置生效语义 (EffectiveUserSettings)与配置操作审计。
- admin 域保留管理面 API 门面(委派方法 + 类型别名,消费方零改动)与 治理/审计职责。
- bootstrap(装配层)直接指向本域单例接线。
单实例约束:system_configs 是"单行 JSON 读改写"语义,必须全站单实例—— 多实例各自缓存内存副本会互相覆盖(用户设置写入后公共配置读旧值的 2026-10-03 事故即由此而来)。Default() 是全站唯一实例;NewService() 仅供测试/嵌入式构造独立实例。
Index ¶
- func InitDefault(gormDB *gorm.DB)
- type ReconfigureHooks
- type Service
- func (s *Service) GetConfig(ctx context.Context) (*SystemConfig, error)
- func (s *Service) InvalidateRuntimeConfig()
- func (s *Service) LoadRuntimeStorage(ctx context.Context) *conf.StorageConfig
- func (s *Service) RestoreAdminSettings()
- func (s *Service) SaveRuntimeStorage(ctx context.Context, cfg *conf.StorageConfig) error
- func (s *Service) SetConfig(config *SystemConfig)
- func (s *Service) SetOnConfigPersisted(fn func())
- func (s *Service) SetReconfigureHooks(h *ReconfigureHooks)
- func (s *Service) UpdateConfig(ctx context.Context, newConfig *SystemConfig) error
- func (s *Service) UpdateUserSettings(ctx context.Context, u UserSettings) error
- type SystemConfig
- type UserSettings
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InitDefault ¶ added in v0.16.0
InitDefault 注册全站配置中心单例(bootstrap 在 InitDatabase 后调用;幂等)。 单实例约束见包注释:system_configs 单行读改写不允许多实例并存。
Types ¶
type ReconfigureHooks ¶
type ReconfigureHooks struct {
// OnNotifyChanged Webhook+SMTP 热重建(mailer 按新 SMTP 段重建,host 空=卸载)
OnNotifyChanged func(n *conf.NotifyConfig)
// OnOIDCChanged OIDC service 热重建(按新段;Enabled=false 时 handler 走未启用响应)
OnOIDCChanged func(o *conf.OIDCConfig)
}
ReconfigureHooks 存储域之外的组件热重建钩子(bootstrap 注册;SMTP mailer / OIDC service)。 参数为合并后的新段;nil 段表示维持现状。
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service 配置中心服务。
func NewService ¶
NewService 构造独立实例(测试/嵌入式;全站运行时请用 Default()—— 单行读改写语义不允许多实例并存,见包注释)。
func (*Service) GetConfig ¶
func (s *Service) GetConfig(ctx context.Context) (*SystemConfig, error)
GetConfig 获取系统配置(首次调用会从 DB 加载管理后台保存的配置)
func (*Service) InvalidateRuntimeConfig ¶
func (s *Service) InvalidateRuntimeConfig()
InvalidateRuntimeConfig 丢弃内存配置缓存(仅影响缓存,不写库)。 多副本:public 副本收到管理端变更广播后调用,下次 GetConfig 重读 DB—— 单机 standalone 不调用(保持既有"进程内即真相"语义)。
func (*Service) LoadRuntimeStorage ¶
func (s *Service) LoadRuntimeStorage(ctx context.Context) *conf.StorageConfig
LoadRuntimeStorage 读取持久化的运行时存储配置(storage.RuntimePersister 实现)。 无记录/未设置返回 nil;DB 不可用返回 nil(与配置加载的降级策略一致)。
func (*Service) RestoreAdminSettings ¶
func (s *Service) RestoreAdminSettings()
RestoreAdminSettings 启动时把持久化的新段 overlay 回全局 conf(DB 先于服务启动就绪)。 由 bootstrap 在 restoreRuntimeStorage 同相位调用。
func (*Service) SaveRuntimeStorage ¶
SaveRuntimeStorage 持久化运行时存储配置(storage.RuntimePersister 实现)。 读改写单行 JSON,只更新 runtime_storage 段;DB 不可用时返回错误, 由调用方(存储域)决定提示语义——切换已生效,仅持久化失败。
func (*Service) SetConfig ¶
func (s *Service) SetConfig(config *SystemConfig)
SetConfig 设置配置(显式注入优先于 DB 持久化配置)
func (*Service) SetOnConfigPersisted ¶
func (s *Service) SetOnConfigPersisted(fn func())
SetOnConfigPersisted 注入配置持久化回调(bootstrap 调用)。多副本拆分时 admin 实例经它发布 Redis 变更广播,public 副本订阅后失效本地缓存并热重建 (详见 docs/specs/2026-10-06-multi-replica-deployment-modes.md §4)。
func (*Service) SetReconfigureHooks ¶
func (s *Service) SetReconfigureHooks(h *ReconfigureHooks)
SetReconfigureHooks 注册组件热重建钩子(bootstrap 调用)。
func (*Service) UpdateConfig ¶
func (s *Service) UpdateConfig(ctx context.Context, newConfig *SystemConfig) error
UpdateConfig 更新系统配置:写穿到 DB(单行),成功后才更新内存。 防御性合并:handler 会从请求重建 SystemConfig,请求不含 runtime_storage (存储域维护的运行时段),为 nil 时保留旧值,避免保存站点配置时被冲掉。
func (*Service) UpdateUserSettings ¶
func (s *Service) UpdateUserSettings(ctx context.Context, u UserSettings) error
UpdateUserSettings 在线更新"用户配置"段:读改写当前配置,仅替换 User, 其余段保持不变;写穿 DB 并即时应用副作用(会话时长)。
type SystemConfig ¶
type SystemConfig struct {
Base struct {
Name string `json:"name"`
Description string `json:"description"`
Port int `json:"port"`
} `json:"base"`
Storage struct {
Type string `json:"type"`
MaxSize int64 `json:"max_size"`
} `json:"storage"`
Transfer struct {
MaxCount int `json:"max_count"`
ExpireDefault int `json:"expire_default"`
} `json:"transfer"`
// RuntimeStorage 运行时存储配置(存储域经 RuntimePersister 接口读写,
// 本域只负责持久化不解释其内容;nil 表示管理端从未在线改过存储后端)。
RuntimeStorage *conf.StorageConfig `json:"runtime_storage,omitempty"`
// User 用户设置(注册开关/上传限制/存储配额/会话时长)。
// nil 表示从未在线保存过 → 运行时回退 yaml 全局配置(EffectiveUserSettings)。
// JSON 键沿用前端 configForm.user 的既有键名,读写端点直传免转换。
User *UserSettings `json:"user,omitempty"`
// ===== v0.7.3 扩容的在线设置段(同 User 语义:nil = 未在线设置 → yaml 生效;
// 非 nil = 保存时整体 overlay 到全局 conf 并热应用,重启由 restoreAdminSettings 恢复)=====
// 外观(背景图/主题色/管理入口可见性)
UI *conf.UIConfig `json:"ui,omitempty"`
// 上传细项(文本上限/扩展名白黑名单/匿名日配额/整文件上限/全局过期上限/过期样式裁剪)
UploadEx *conf.UploadConfig `json:"upload_ex,omitempty"`
// 下载(S3 直下/超时/需登录)
Download *conf.DownloadConfig `json:"download,omitempty"`
// 通知(Webhook + SMTP;保存经 Reconfigurers 热重建 mailer)
Notify *conf.NotifyConfig `json:"notify,omitempty"`
// 本地导入(enabled/roots)
LocalImport *conf.LocalImportConfig `json:"local_import,omitempty"`
// OIDC 单点登录
OIDC *conf.OIDCConfig `json:"oidc,omitempty"`
// API Key 认证总开关与 per-Key 限流
APIToken *conf.APITokenConfig `json:"api_token,omitempty"`
}
type UserSettings ¶
type UserSettings struct {
// AllowUserRegistration 是否开放注册(/user/register 与 /api/config 同源读取)
AllowUserRegistration bool `json:"allowuserregistration"`
// UserUploadSize 用户单次上传大小默认上限(字节,0 = 不限)
UserUploadSize int64 `json:"useruploadsize"`
// UserStorageQuota 用户存储配额默认值(字节,0 = 不限)
UserStorageQuota int64 `json:"userstoragequota"`
// SessionExpiryHours 会话时长(小时,0 = auth 包默认 7 天)
SessionExpiryHours int `json:"sessionexpiryhours"`
}
UserSettings 管理后台"用户配置"标签页的在线设置。 语义:作为对应运行时消费点的系统级默认值(用户级覆盖仍然优先)。
func EffectiveUserSettings ¶
func EffectiveUserSettings(ctx context.Context) UserSettings
EffectiveUserSettings 生效的用户设置:优先管理后台持久化值, 无记录时回退 yaml 全局配置。注册开关、配额默认值、会话时长的 运行时消费点统一走这里(修复管理端开关不生效)。