- config: Viper YAML 配置 + 环境变量覆盖 - logs: zap 结构化日志 + trace_id 链路追踪 + 敏感信息脱敏 - db: PostgreSQL (GORM) + Redis (go-redis) 连接管理 - middleware: Trace/Logger/CORS/Recovery 四层中间件 - provider: Wire 编译时依赖注入 - main.go: 优雅启动/关闭 HTTP 服务 Made-with: Cursor
27 lines
651 B
Go
27 lines
651 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// CORS 跨域中间件
|
|
// 允许前端跨域访问后端 API
|
|
func CORS() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
|
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH")
|
|
c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Authorization, X-Request-ID")
|
|
c.Header("Access-Control-Expose-Headers", "X-Request-ID")
|
|
c.Header("Access-Control-Max-Age", "86400")
|
|
|
|
if c.Request.Method == http.MethodOptions {
|
|
c.AbortWithStatus(http.StatusNoContent)
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|