com.snow.auto_monitor/app/http/middlewares/cors.go

23 lines
608 B
Go

package middlewares
import (
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func Cors() gin.HandlerFunc {
return cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, // 允许的请求方法
AllowHeaders: []string{"Origin", "Authorization", "Content-Type"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
// AllowOriginFunc: func(origin string) bool { // 自定义过滤源站的方法
// return origin == "https://github.com"
// },
MaxAge: 12 * time.Hour,
})
}