Skip to content

feat: kk.Picturespreview、kk.Getcorsfile、kk.addTask 支持通过环境变量配置 - #803

Open
yuhaibohotmail wants to merge 1 commit into
kekingcn:masterfrom
yuhaibohotmail:feat/interface-switches-from-env
Open

yuhaibohotmail wants to merge 1 commit into
kekingcn:masterfrom
yuhaibohotmail:feat/interface-switches-from-env

Conversation

@yuhaibohotmail

Copy link
Copy Markdown
Contributor

背景

server/src/main/config/application.properties 里大多数配置项都写成 ${KK_XXX:默认值},可以通过环境变量调整,例如:

cache.enabled = ${KK_CACHE_ENABLED:true}
delete.source.file = ${KK_DELETE_SOURCE_FILE:true}

而「权限与认证配置」一节里的这三项写成了固定值:

kk.Picturespreview = true
kk.Getcorsfile = true
kk.addTask = true

用 Docker 等容器方式部署时,其它配置项加一个 -e KK_XXX=... 就能调整,这三项却只能修改或挂载整份配置文件,写法上也和其它配置项不一致。

修改

把这三项改成与其它配置项相同的写法,application.properties 与 test.properties 同步修改:

kk.Picturespreview = ${KK_PICTURESPREVIEW:true}
kk.Getcorsfile = ${KK_GETCORSFILE:true}
kk.addTask = ${KK_ADDTASK:true}
  • 环境变量名按文件里已有的习惯取:KK_ 前缀 + 配置项名转大写(参照 simText → KK_SIMTEXT、convertMedias → KK_CONVERTMEDIAS);
  • 默认值不变,仍是 true,不设置环境变量时行为与之前完全相同;
  • 没有改动 Java 代码。启动时由 Spring 解析占位符;配置文件热加载(ConfigRefreshComponent → ConfigUtils.restorePropertiesFromEnvFormat)本来就支持这种写法。

验证

新增 InterfaceSwitchEnvConfigTests,对 application.properties、test.properties 两份文件各检查这三项,3 条:

  • shouldReadInterfaceSwitchesFromEnvironmentVariables:存在对应环境变量时,配置项解析为环境变量的值;
  • shouldKeepInterfaceSwitchesEnabledByDefault:没有环境变量时解析为 true;
  • shouldKeepInterfaceSwitchesEnabledByDefaultWhenConfigIsReloaded:按配置热加载的方式处理后,默认仍为 true。

修改前先跑:第 1 条失败(kk.Picturespreview in /application.properties must be configurable through KK_PICTURESPREVIEW ==> expected: <false> but was: <true>),另外 2 条通过;修改后 3 条全部通过。把其中任意一行改回固定值(试的是 test.properties 的 kk.addTask),第 1 条重新失败。

mvn -B -pl server test -Dtest='!ServerMainTests'(Windows,JDK 25):Tests run: 90, Failures: 0, Errors: 1。唯一的 1 条是 ArchiveDirectorySecurityTests.cachedArchivePreviewRendersItsTreeWithoutAnEnumerationRequest,它要创建文件名带双引号的文件,Windows 不允许,在未修改的 master 上同样失败(master 为 87 条、同样 1 条 Error),与本次修改无关。

配置文件中大多数配置项使用 ${KK_XXX:默认值} 的写法,可以通过环境变量调整,
这三项写成了固定值 true,容器化部署时只能修改或挂载配置文件。

改为 ${KK_PICTURESPREVIEW:true}、${KK_GETCORSFILE:true}、${KK_ADDTASK:true},
与其它配置项保持一致,application.properties 与 test.properties 同步修改。
默认值不变,未设置环境变量时行为与之前相同。

新增 InterfaceSwitchEnvConfigTests,校验两份配置文件中这三项可通过环境变量
调整,且默认值仍为 true。

Signed-off-by: yuhaibo <yuhaibo@hotmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant