diff --git a/project_codebase.md b/project_codebase.md index c8f169a..bee43e1 100644 --- a/project_codebase.md +++ b/project_codebase.md @@ -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` 配置。 diff --git a/project_doing.md b/project_doing.md index a9aeb0d..1209c72 100644 --- a/project_doing.md +++ b/project_doing.md @@ -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。 diff --git a/server/config/config.dev.yaml b/server/config/config.dev.yaml index 33a19af..772ba6a 100644 --- a/server/config/config.dev.yaml +++ b/server/config/config.dev.yaml @@ -1,3 +1,6 @@ +server: + port: 8080 + log: level: debug dir: logs diff --git a/server/config/config.go b/server/config/config.go index d0b7deb..886733e 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -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"` // 是否启用 diff --git a/server/config/config.prod.yaml b/server/config/config.prod.yaml index 3a02ae1..cb987a3 100644 --- a/server/config/config.prod.yaml +++ b/server/config/config.prod.yaml @@ -1,3 +1,6 @@ +server: + port: 8081 + log: level: info dir: logs diff --git a/server/config/config.test.yaml b/server/config/config.test.yaml index a4d3eca..b81e984 100644 --- a/server/config/config.test.yaml +++ b/server/config/config.test.yaml @@ -1,3 +1,6 @@ +server: + port: 8080 + log: level: debug dir: logs diff --git a/server/main.go b/server/main.go index 77d86b2..87b399b 100644 --- a/server/main.go +++ b/server/main.go @@ -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, } diff --git a/task_detail.md b/task_detail.md index 705e592..6150d72 100644 --- a/task_detail.md +++ b/task_detail.md @@ -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。 +- **执行结果**: 服务现在会使用配置文件中指定的端口启动。