package auxgin import ( "net/http" "strings" "github.com/gin-gonic/gin" ) func CorsMiddleware() gin.HandlerFunc { headers := []string{"Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"} headerList := strings.Join(headers, ",") methods := []string{"POST", "GET", "OPTIONS", "PUT", "DELETE", "UPDATE"} methodList := strings.Join(methods, ",") return func(gctx *gin.Context) { gctx.Writer.Header().Set("Access-Control-Allow-Origin", "*") gctx.Writer.Header().Set("Access-Control-Max-Age", "86400") gctx.Writer.Header().Set("Access-Control-Allow-Methods", methodList) gctx.Writer.Header().Set("Access-Control-Allow-Headers", headerList) gctx.Writer.Header().Set("Access-Control-Allow-Credentials", "true") if gctx.Request.Method == "OPTIONS" { gctx.AbortWithStatus(http.StatusOK) } else { gctx.Next() } } }