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

View File

@ -1,13 +1,14 @@
### [任务执行] 优化配置加载逻辑 ### [任务执行] 服务端口配置化
- **时间戳**: 2026-01-02 - **时间戳**: 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` 参数,如果存在则直接使用指定的配置文件。 - `AppConfig` 增加 `Server.Port` 字段。
2. **查找顺序优化**: - 所有环境配置文件增加 `server.port: 8080` 配置。
* `./config.{env}.yaml` (当前目录,方便生产部署) - `main.go` 读取配置端口启动,若未配置则默认为 8080。
* `./config/config.{env}.yaml` (开发环境)
* `../config/config.{env}.yaml` (测试环境)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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