cors.go 640 B

1234567891011121314151617181920212223
  1. package middleware
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. )
  6. func Cors() gin.HandlerFunc {
  7. return func(c *gin.Context) {
  8. c.Header("Access-Control-Allow-Origin", "*")
  9. c.Header("Access-Control-Allow-Headers", c.Request.Header.Get("Access-Control-Request-Headers"))
  10. c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
  11. c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
  12. c.Header("Access-Control-Allow-Credentials", "false")
  13. if c.Request.Method == "OPTIONS" {
  14. c.AbortWithStatus(http.StatusOK)
  15. return
  16. }
  17. c.Next()
  18. }
  19. }