middleware.go 621 B

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