package middleware

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

// Cors 跨域配置
func Cors(c *gin.Context) {
	method := c.Request.Method
	c.Header("ACCESS-CONTROL-ALLOW-ORIGIN", "*")
	c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
	c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
	c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
	c.Header("Access-Control-Allow-Credentials", "true")
	if method == "OPTIONS" {
		c.AbortWithStatus(http.StatusNoContent)
		return
	}
	c.Next()
}
