refactor(main): 重构主应用结构以提高模块化

- 将main.go中的初始化逻辑提取到新的app包中
- 创建bootstrap包来处理配置加载和数据库初始化
- 添加middleware包来管理CORS中间件
- 创建router包来处理路由配置
- 简化main函数,只保留应用启动逻辑
- 使用依赖注入模式组织服务和处理器
This commit is contained in:
2026-02-26 16:19:13 +08:00
parent 2230b5ec9e
commit cf0685d102
6 changed files with 121 additions and 64 deletions

View File

@@ -0,0 +1,17 @@
package middleware
import "github.com/gin-gonic/gin"
func CORS() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}