cors.go 691 B

1234567891011121314151617181920212223
  1. package middleware
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. )
  6. func Cors() 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,AccessToken,X-CSRF-Token, Authorization, Token,X-Token,X-User-Id")
  11. c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS,DELETE,PUT")
  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. }