feat: 引入 Viper 实现多层配置管理
引入 Viper 配置管理框架,支持 CLI 参数、环境变量、配置文件和默认值四种配置方式。 主要变更: - 引入 Viper、pflag、validator、mapstructure 依赖 - 实现配置优先级:CLI > ENV > File > Default - 所有 13 个配置项支持 CLI 参数和环境变量 - 规范化命名:server.port → NEX_SERVER_PORT → --server-port - 使用结构体验证器进行配置验证 - 添加配置摘要输出功能 新增能力: - cli-config: 命令行参数配置支持 - env-config: 环境变量配置支持(符合 12-Factor App) - config-priority: 配置优先级管理 修改能力: - config-management: 扩展为多层配置源支持 使用示例: ./server --server-port 9000 --log-level debug export NEX_SERVER_PORT=9000 && ./server ./server --config /path/to/custom.yaml
This commit is contained in:
@@ -151,6 +151,10 @@ go run cmd/server/main.go
|
||||
|
||||
## 配置
|
||||
|
||||
配置支持多种方式:配置文件、环境变量、命令行参数,优先级为:**CLI 参数 > 环境变量 > 配置文件 > 默认值**
|
||||
|
||||
### 配置文件
|
||||
|
||||
配置文件位于 `~/.nex/config.yaml`,首次启动自动生成。
|
||||
|
||||
```yaml
|
||||
@@ -174,6 +178,88 @@ log:
|
||||
compress: true
|
||||
```
|
||||
|
||||
### 环境变量
|
||||
|
||||
所有配置项都支持环境变量,使用 `NEX_` 前缀:
|
||||
|
||||
```bash
|
||||
export NEX_SERVER_PORT=9000
|
||||
export NEX_DATABASE_PATH=/data/nex.db
|
||||
export NEX_LOG_LEVEL=debug
|
||||
./server
|
||||
```
|
||||
|
||||
环境变量命名规则:将配置路径转换为大写,用下划线连接,加 `NEX_` 前缀:
|
||||
- `server.port` → `NEX_SERVER_PORT`
|
||||
- `database.path` → `NEX_DATABASE_PATH`
|
||||
- `log.level` → `NEX_LOG_LEVEL`
|
||||
|
||||
### 命令行参数
|
||||
|
||||
所有配置项都支持命令行参数:
|
||||
|
||||
```bash
|
||||
./server --server-port 9000 --log-level debug --database-path /tmp/test.db
|
||||
```
|
||||
|
||||
CLI 参数命名规则:将配置路径转换为 kebab-case,用连字符连接:
|
||||
- `server.port` → `--server-port`
|
||||
- `database.path` → `--database-path`
|
||||
- `log.level` → `--log-level`
|
||||
|
||||
完整参数列表:
|
||||
|
||||
```
|
||||
服务器配置:
|
||||
--server-port int 服务器端口(默认 9826)
|
||||
--server-read-timeout duration 读超时(默认 30s)
|
||||
--server-write-timeout duration 写超时(默认 30s)
|
||||
|
||||
数据库配置:
|
||||
--database-path string 数据库文件路径(默认 ~/.nex/config.db)
|
||||
--database-max-idle-conns int 最大空闲连接数(默认 10)
|
||||
--database-max-open-conns int 最大打开连接数(默认 100)
|
||||
--database-conn-max-lifetime duration 连接最大生命周期(默认 1h)
|
||||
|
||||
日志配置:
|
||||
--log-level string 日志级别:debug/info/warn/error(默认 info)
|
||||
--log-path string 日志文件目录(默认 ~/.nex/log)
|
||||
--log-max-size int 单个日志文件最大大小 MB(默认 100)
|
||||
--log-max-backups int 保留的旧日志文件最大数量(默认 10)
|
||||
--log-max-age int 保留旧日志文件的最大天数(默认 30)
|
||||
--log-compress 是否压缩旧日志文件(默认 true)
|
||||
|
||||
通用选项:
|
||||
--config string 配置文件路径(默认 ~/.nex/config.yaml)
|
||||
```
|
||||
|
||||
### 使用示例
|
||||
|
||||
```bash
|
||||
# 1. 使用默认配置
|
||||
./server
|
||||
|
||||
# 2. 临时修改端口(不修改配置文件)
|
||||
./server --server-port 9000
|
||||
|
||||
# 3. 测试场景(临时数据库)
|
||||
./server --database-path /tmp/test.db --log-level debug
|
||||
|
||||
# 4. Docker 部署(环境变量)
|
||||
docker run -d \
|
||||
-e NEX_SERVER_PORT=9000 \
|
||||
-e NEX_DATABASE_PATH=/data/nex.db \
|
||||
-e NEX_LOG_LEVEL=info \
|
||||
nex-server
|
||||
|
||||
# 5. 使用自定义配置文件
|
||||
./server --config /path/to/custom.yaml
|
||||
|
||||
# 6. 混合使用(优先级:CLI > ENV > File)
|
||||
export NEX_LOG_LEVEL=debug
|
||||
./server --server-port 9000 # port 用 CLI,log.level 用 ENV
|
||||
```
|
||||
|
||||
数据文件:
|
||||
- `~/.nex/config.yaml` - 配置文件
|
||||
- `~/.nex/config.db` - SQLite 数据库
|
||||
|
||||
Reference in New Issue
Block a user