cros.go 672 B

1234567891011121314151617181920212223
  1. package middlewares
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. )
  6. func Cros() gin.HandlerFunc {
  7. return func(c *gin.Context) {
  8. method := c.Request.Method
  9. c.Header("Access-Control-Allow-Origin", "*")
  10. c.Header("Access-Control-Allow-Headers", "Content-Type, Access-Token, X-CSRF-Token, Authorization")
  11. c.Header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
  12. c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
  13. c.Header("Access-Control-Allow-Credentials", "true")
  14. if method == "OPTIONS" {
  15. c.AbortWithStatus(http.StatusNoContent)
  16. }
  17. c.Next()
  18. }
  19. }