From a5901d9ac5519bd27d54ed3f889cb2bd5a4308cf Mon Sep 17 00:00:00 2001 From: areeqakbr Date: Thu, 20 Feb 2025 11:48:25 +0700 Subject: [PATCH] edit cors middleware --- middleware/cors_middleware.go | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/middleware/cors_middleware.go b/middleware/cors_middleware.go index 9845ebb..0a39dec 100644 --- a/middleware/cors_middleware.go +++ b/middleware/cors_middleware.go @@ -1,26 +1,24 @@ package middleware import ( - "github.com/gin-gonic/gin" + "net/http" + + "github.com/gin-gonic/gin" ) func CORSMiddleware() gin.HandlerFunc { - return func(c *gin.Context) { - origin := c.Request.Header.Get("Origin") // Get the request origin + return func(c *gin.Context) { + 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 != "" { - c.Writer.Header().Set("Access-Control-Allow-Origin", origin) - } + // Handle OPTIONS method + if c.Request.Method == "OPTIONS" { + c.AbortWithStatus(http.StatusNoContent) + return + } - 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", "POST, OPTIONS, GET, PUT, DELETE") - - if c.Request.Method == "OPTIONS" { - c.AbortWithStatus(204) - return - } - - c.Next() - } + c.Next() + } } \ No newline at end of file