Update handlers, services, router, and data conversion logic to support server-sent events and related message pipeline changes. Made-with: Cursor
24 lines
791 B
Go
24 lines
791 B
Go
package middleware
|
|
|
|
import "github.com/gin-gonic/gin"
|
|
|
|
// CORS CORS中间件
|
|
func CORS() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
|
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
|
|
// 添加 WebSocket 升级所需的头
|
|
c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, Authorization, Connection, Upgrade, Sec-WebSocket-Key, Sec-WebSocket-Version, Sec-WebSocket-Protocol, Sec-WebSocket-Extensions")
|
|
c.Header("Access-Control-Expose-Headers", "Content-Length, Connection, Upgrade")
|
|
c.Header("Access-Control-Allow-Credentials", "true")
|
|
|
|
// 处理 WebSocket 升级请求的预检
|
|
if c.Request.Method == "OPTIONS" {
|
|
c.AbortWithStatus(204)
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|