This commit is contained in:
zhouwentao 2026-01-02 17:24:40 +08:00
parent 59b7bde12e
commit 43005cdb13
8 changed files with 39 additions and 17 deletions

View File

@ -11,6 +11,7 @@
- `LoadConfig()`: 加载配置。
- 优先级1命令行参数 `-c``-config` 指定的文件。
- 优先级2环境变量 `GO_ENV` 决定的文件(查找顺序:`./config.{env}.yaml` -> `config/config.{env}.yaml` -> `../config/config.{env}.yaml`)。
- `AppConfig`: 全局配置对象,包含 Log, Server, Database 等配置。
- `InitDB()`: 初始化GORM数据库连接支持 SQL 日志配置(自动写入 `logs/sql-YYYY-MM-DD.log`)。
- `InitRedis()`: 初始化Redis客户端。
- `AppConfig`: 全局配置变量,包含 `Log`, `Security`, `RateLimit`, `Swagger`, `Database`, `Redis` 配置。

View File

@ -1,13 +1,14 @@
### [任务执行] 优化配置加载逻辑
### [任务执行] 服务端口配置化
- **时间戳**: 2026-01-02
- **关联任务**: 优化配置加载
- **操作目标**: 增强 `LoadConfig`,支持命令行参数指定配置文件,并优化查找顺序。
- **影响范围**: `server/config/config.go`
- **修改前记录**: 仅通过 `GO_ENV` 决定文件名,并在 `config/` 或上级目录查找。
- **关联任务**: 端口配置化
- **操作目标**: 将服务启动端口从硬编码改为从配置文件读取。
- **影响范围**:
- `server/config/config.go`
- `server/config/config.*.yaml`
- `server/main.go`
- **修改前记录**: `main.go` 中硬编码使用 `:8080`
- **修改结果**:
1. **命令行参数**: 优先检查 `-c``-config` 参数,如果存在则直接使用指定的配置文件。
2. **查找顺序优化**:
* `./config.{env}.yaml` (当前目录,方便生产部署)
* `./config/config.{env}.yaml` (开发环境)
* `../config/config.{env}.yaml` (测试环境)
- `AppConfig` 增加 `Server.Port` 字段。
- 所有环境配置文件增加 `server.port: 8080` 配置。
- `main.go` 读取配置端口启动,若未配置则默认为 8080。

View File

@ -1,3 +1,6 @@
server:
port: 8080
log:
level: debug
dir: logs

View File

@ -13,6 +13,7 @@ var AppConfig = &appConfig{}
type appConfig struct {
Log LogConfig `yaml:"log"`
Server ServerConfig `yaml:"server"`
Security SecurityConfig `yaml:"security"`
RateLimit RateLimitConfig `yaml:"rate_limit"`
Swagger SwaggerConfig `yaml:"swagger"`
@ -27,6 +28,11 @@ type LogConfig struct {
Console bool `yaml:"console"` // 是否输出到控制台
}
// ServerConfig 服务配置
type ServerConfig struct {
Port int `yaml:"port"` // 服务端口
}
// SecurityConfig 安全配置
type SecurityConfig struct {
Enable bool `yaml:"enable"` // 是否启用

View File

@ -1,3 +1,6 @@
server:
port: 8081
log:
level: info
dir: logs

View File

@ -1,3 +1,6 @@
server:
port: 8080
log:
level: debug
dir: logs

View File

@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
@ -91,8 +92,12 @@ func main() {
userController.NewUserMajorController().RegisterRoutes(api)
// 创建 HTTP 服务器
port := config.AppConfig.Server.Port
if port == 0 {
port = 8080 // 默认端口
}
srv := &http.Server{
Addr: ":8080",
Addr: fmt.Sprintf(":%d", port),
Handler: r,
}

View File

@ -1,8 +1,8 @@
## 会话 ID: 20260102-02
- **执行原因**: 用户希望在运行时通过命令行参数指定配置文件,并优化配置文件查找逻辑,优先使用当前目录下的文件
## 会话 ID: 20260102-03
- **执行原因**: 用户希望将服务端口号配置到配置文件中,避免硬编码
- **执行过程**:
1. 修改 `server/config/config.go` 中的 `LoadConfig` 函数
2. 增加命令行参数解析逻辑,支持 `-c``-config` 参数
3. 调整配置文件查找顺序:优先检查 `config.{env}.yaml`(当前目录),其次是 `config/config.{env}.yaml`,最后是上级目录
- **执行结果**: 现在可以通过命令行参数指定配置文件,或者直接将配置文件放在可执行文件同级目录下,无需严格依赖 `config/` 目录结构
1. 修改 `server/config/config.go`,在 `AppConfig` 中添加 `ServerConfig` 结构体
2. 更新 `config.dev.yaml`、`config.prod.yaml` 和 `config.test.yaml`,添加 `server: port: 8080` 配置
3. 修改 `server/main.go`,使其从 `config.AppConfig.Server.Port` 读取端口号,并保留默认值 8080
- **执行结果**: 服务现在会使用配置文件中指定的端口启动