Skip to content

feat(config): seed server address from environment#5785

Draft
zhangzhichaolove wants to merge 2 commits into
QuantumNous:mainfrom
zhangzhichaolove:codex/fix-5549-server-address-env-seed
Draft

feat(config): seed server address from environment#5785
zhangzhichaolove wants to merge 2 commits into
QuantumNous:mainfrom
zhangzhichaolove:codex/fix-5549-server-address-env-seed

Conversation

@zhangzhichaolove

@zhangzhichaolove zhangzhichaolove commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

ServerAddress 增加 SERVER_ADDRESS 环境变量配置。

启动加载系统选项时,如果数据库中还没有 ServerAddress 配置时,并且环境变量 SERVER_ADDRESS 不为空,后端会通过现有 UpdateOption 路径写入该值。这样它会和管理员在 UI 中保存配置一样,同步到运行时的 system_setting.ServerAddress

如果数据库中已经存在 ServerAddress,环境变量不会覆盖已有值,保持优先级为:数据库/管理员配置 > 环境变量设置值 > 代码默认值。

同时补充了 SERVER_ADDRESS 的 README 环境变量说明、宝塔安装文档说明,以及 docker-compose.yml / docker-compose.dev.yml 示例注释。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

go test ./model
go test ./...

验证覆盖:

  • 数据库缺少 ServerAddress 时,SERVER_ADDRESS 会写入 options 表并同步运行时配置。
  • 数据库已存在 ServerAddress 时,SERVER_ADDRESS 不会覆盖管理员配置。

Summary by CodeRabbit

  • New Features

    • Added support for a new SERVER_ADDRESS environment variable to seed the initial server address on first startup when no existing setting is present.
    • Updated setup and installation guides in multiple languages, and Docker compose examples, to document the optional SERVER_ADDRESS usage.
  • Bug Fixes

    • Ensured SERVER_ADDRESS does not overwrite an existing, already-configured server address.
  • Tests

    • Added unit tests validating seeding behavior when missing and non-overwrite behavior when already set.

Add SERVER_ADDRESS as a first-run seed for the ServerAddress option. When the
database does not yet contain a ServerAddress row, startup persists the
environment value through the existing option update path so runtime state and
the options table stay in sync.

Existing database/admin settings continue to take precedence and are never
overwritten by the environment variable. Document the new variable in README
files and docker-compose examples, and cover both seed and no-overwrite behavior
with model tests.

Refs QuantumNous#5549
@coderabbitai

coderabbitai Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 60adbc3c-f69d-4f5e-8b1c-b856841438c0

📥 Commits

Reviewing files that changed from the base of the PR and between 0749931 and 990f5cb.

📒 Files selected for processing (2)
  • model/option.go
  • model/option_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • model/option.go
  • model/option_test.go

Walkthrough

loadOptionsFromDatabase now seeds ServerAddress from SERVER_ADDRESS when no database row exists, and new tests cover seeding plus non-overwrite behavior. The README files, installation guide, and docker-compose files now document the new variable.

Changes

SERVER_ADDRESS First-Run Seed

Layer / File(s) Summary
Seeding logic and tests
model/option.go, model/option_test.go
loadOptionsFromDatabase now detects a missing ServerAddress option and seeds it from SERVER_ADDRESS via UpdateOption; the new tests set up an isolated SQLite-backed option database and verify both seeding and no-overwrite behavior.
Docs and compose hints
README.md, README.en.md, README.fr.md, README.ja.md, README.zh_CN.md, README.zh_TW.md, docs/installation/BT.md, docker-compose.yml, docker-compose.dev.yml
Adds SERVER_ADDRESS to environment-variable tables across the docs and adds commented SERVER_ADDRESS entries in both docker-compose files.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 I hop where the options grow,
A first-run seed now starts to show.
If admins spoke, I leave it be,
SERVER_ADDRESS blooms fresh and free.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately describes the main change: seeding server address from an environment variable.
Linked Issues check ✅ Passed The code seeds ServerAddress from SERVER_ADDRESS only when missing, preserves existing DB values, and writes through the option update flow.
Out of Scope Changes check ✅ Passed The extra changes are documentation, docker-compose examples, and tests, all directly supporting the env seeding feature.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
model/option_test.go (1)

21-23: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Clone OptionMap before saving/restoring it.

previousOptionMap := common.OptionMap only copies the map header. If InitOptionMap or a future test mutates the existing map in place, cleanup restores the already-mutated contents and leaks state into later tests.

Suggested fix
 	common.OptionMapRWMutex.RLock()
-	previousOptionMap := common.OptionMap
+	previousOptionMap := make(map[string]string, len(common.OptionMap))
+	for k, v := range common.OptionMap {
+		previousOptionMap[k] = v
+	}
 	common.OptionMapRWMutex.RUnlock()

Also applies to: 39-40

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@model/option_test.go` around lines 21 - 23, The test setup around
common.OptionMap is only keeping a shallow reference, so later mutations can
leak between tests; update the save/restore logic in the OptionMap test helpers
to clone the map contents before storing previousOptionMap and when restoring
it. Use the existing common.OptionMapRWMutex-protected section in option_test.go
and ensure both the initial snapshot and the cleanup assignment operate on
independent copies rather than the shared map header.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@model/option_test.go`:
- Line 52: The test queries in option_test.go are using dialect-specific
backtick quoting, which breaks PostgreSQL compatibility; update the affected
assertions in the option test flow (including the DB.First calls around the
ServerAddress and related key lookups) to let GORM build the predicate instead
of hardcoding SQL quoting. Use GORM’s structured condition APIs with the
existing option model/query path so the same test works across SQLite, MySQL,
and PostgreSQL.

In `@model/option.go`:
- Around line 190-209: loadOptionsFromDatabase currently ignores the error from
AllOption(), which can make a database read failure look like ServerAddress is
missing and trigger unwanted seeding. Update loadOptionsFromDatabase to handle
the AllOption() error explicitly and return or abort before checking
serverAddressExists or calling UpdateOption, so seeding only happens after a
successful options load.

---

Nitpick comments:
In `@model/option_test.go`:
- Around line 21-23: The test setup around common.OptionMap is only keeping a
shallow reference, so later mutations can leak between tests; update the
save/restore logic in the OptionMap test helpers to clone the map contents
before storing previousOptionMap and when restoring it. Use the existing
common.OptionMapRWMutex-protected section in option_test.go and ensure both the
initial snapshot and the cleanup assignment operate on independent copies rather
than the shared map header.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ed99172a-e805-4aae-9072-249f1c02d144

📥 Commits

Reviewing files that changed from the base of the PR and between 3a506f5 and 0749931.

📒 Files selected for processing (11)
  • README.en.md
  • README.fr.md
  • README.ja.md
  • README.md
  • README.zh_CN.md
  • README.zh_TW.md
  • docker-compose.dev.yml
  • docker-compose.yml
  • docs/installation/BT.md
  • model/option.go
  • model/option_test.go

Comment thread model/option_test.go Outdated
Comment thread model/option.go
Handle option load errors before seeding SERVER_ADDRESS, avoid dialect-specific test predicates, and clone OptionMap snapshots in tests.

Refs QuantumNous#5549
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.

Allow seeding ServerAddress from SERVER_ADDRESS env var on first run

1 participant