cors.go 882 B

12345678910111213141516171819202122
  1. package middleware
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. )
  6. func CORSMiddleware() 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-Methods", "POST, GET, OPTIONS, PUT, DELETE")
  11. c.Header("Access-Control-Allow-Headers", "sec-fetch-dest,sec-fetch-mode,sec-fetch-site,Access-Control-Allow-Origin,X-Mode,Authorization, Content-Length, X-CSRF-Token, Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language,DNT, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Pragma,timestamp,nonce,sign,x-vs-language,Debug,App-Version,language,X-Forwarded-For,X-Real-IP")
  12. c.Header("Access-Control-Allow-Credentials", "true")
  13. if method == "OPTIONS" {
  14. c.AbortWithStatus(http.StatusNoContent)
  15. return
  16. }
  17. c.Next()
  18. }
  19. }