Golang Access-Control-Allow-Origin cross-domain

package middleware
import (
    "github.com/gin-gonic/gin"
)
func Cors() gin.HandlerFunc {
    return func(c *gin.Context) {
        origin := c.Request.Header.Get("origin")
        if len(origin) == 0 {
            origin = c.Request.Header.Get("Origin")
        }
        c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
        c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
        c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
        c.Writer.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST")
        c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
        if c.Request.Method == "OPTIONS" {
            c.AbortWithStatus(204)
            return
        }
        c.Next()
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish