34 lines
814 B
Go
34 lines
814 B
Go
// Package middleware 定义平台 API 自有的 HTTP 中间件。
|
||
package middleware
|
||
|
||
import (
|
||
"time"
|
||
|
||
"github.com/gin-contrib/cors"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// Cors 允许平台前端跨域调用 API,包括状态更新使用的 PATCH 方法。
|
||
func Cors() gin.HandlerFunc {
|
||
return cors.New(cors.Config{
|
||
AllowOriginFunc: func(origin string) bool {
|
||
return true // 允许所有域名
|
||
},
|
||
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
|
||
AllowHeaders: []string{
|
||
"Origin",
|
||
"Content-Type",
|
||
"Authorization",
|
||
"X-Requested-With",
|
||
"X-App-Id",
|
||
"X-Doip-App-Id",
|
||
"X-Lowcode-Mode",
|
||
"X-Lowcode-Org",
|
||
"Token",
|
||
},
|
||
ExposeHeaders: []string{"Content-Length"},
|
||
AllowCredentials: true, // 支持 credentials
|
||
MaxAge: 12 * time.Hour,
|
||
})
|
||
}
|