This commit is contained in:
vipg
2025-11-14 14:08:01 +08:00
parent f75f26d14c
commit fe61b7c365
4 changed files with 33 additions and 11 deletions

View File

@@ -5,6 +5,7 @@ import (
"country/logger" // 日志工具包
"country/logic" // 业务逻辑处理包
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" // Gin框架用于构建HTTP服务
_ "github.com/lib/pq" // PostgreSQL数据库驱动下划线表示仅初始化不直接使用
"go.uber.org/zap" // Zap日志库用于结构化日志输出
@@ -31,6 +32,23 @@ func main() {
// 创建Gin默认路由器
r := gin.Default()
// 配置跨域中间件
r.Use(cors.New(cors.Config{
// 允许所有来源(生产环境建议指定具体域名)
AllowOrigins: []string{"*"},
// 允许的请求方法
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
// 允许的请求头
AllowHeaders: []string{"Origin", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization", "X-LoginRequest-ID"},
// 允许前端读取的响应头
ExposeHeaders: []string{"Content-Length"},
// 是否允许携带cookie
AllowCredentials: true,
// 预检请求的缓存时间
MaxAge: 12 * time.Hour,
}))
zap.L().Info("✅ 配置跨域中间件完成")
// 注册创建国家的接口POST请求由logic.CreateHandler处理
r.POST("/country/create", logic.CreateHandler)
zap.L().Info("✅ 创建接口注册完成: POST /country/create")