edit cors middleware

This commit is contained in:
areeqakbr 2025-02-20 11:48:25 +07:00
parent a5fdd64877
commit a5901d9ac5
1 changed files with 15 additions and 17 deletions

View File

@ -1,26 +1,24 @@
package middleware package middleware
import ( import (
"github.com/gin-gonic/gin" "net/http"
"github.com/gin-gonic/gin"
) )
func CORSMiddleware() gin.HandlerFunc { func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
origin := c.Request.Header.Get("Origin") // Get the request origin c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
if origin != "" { // Handle OPTIONS method
c.Writer.Header().Set("Access-Control-Allow-Origin", origin) if c.Request.Method == "OPTIONS" {
} c.AbortWithStatus(http.StatusNoContent)
return
}
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") c.Next()
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", "POST, OPTIONS, GET, PUT, DELETE")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
} }