feat(config): migrate configuration management to viper

Replace manual environment variable parsing with Viper to support
file-based configuration.

- Enable loading config from `config.yaml` or `./config/config.yaml`
- Allow custom config path via `EZ_CONFIG_FILE` environment variable
- Bind existing environment variables to maintain backward compatibility
- Update documentation with configuration examples and precedence rules
This commit is contained in:
zenfun
2025-12-12 22:50:54 +08:00
parent 5891722526
commit eaf99e1582
4 changed files with 104 additions and 39 deletions

View File

@@ -41,6 +41,24 @@ EZ-API 是"大脑"。它管理着事实的来源 (Source of Truth)。
| `EZ_LOG_BATCH_SIZE` | `10` | 单次 DB 事务写入的日志数量。 |
| `EZ_LOG_LEVEL` | `info` | 日志级别,支持 `debug``info``warn``error`。 |
配置读取优先级:环境变量 > 配置文件 > 默认值。通过 [Viper](https://github.com/spf13/viper) 支持 `./config.yaml`(或 `./config/config.yaml`),也可用 `EZ_CONFIG_FILE` 指定路径。示例:
```yaml
server:
port: 8080
postgres:
dsn: host=localhost user=postgres password=postgres dbname=ezapi port=5432 sslmode=disable
redis:
addr: localhost:6379
db: 0
log:
batch_size: 10
flush_ms: 1000
queue_capacity: 10000
auth:
jwt_secret: change_me_in_production
```
## 运行方式
### 本地运行