diff --git a/cors.go b/cors.go index ec5e2fd..ddae754 100644 --- a/cors.go +++ b/cors.go @@ -6,13 +6,26 @@ import ( // Middleware function to handle CORS requests func handleCORS(c *gin.Context) { - c.Writer.Header().Set("Access-Control-Allow-Origin", "*") - c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH") - c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type") - if c.Request.Method == "OPTIONS" { + header := c.Writer.Header() + header.Set("Access-Control-Allow-Origin", "*") + header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH") + header.Set("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type") + c.AbortWithStatus(200) return } + c.Next() + + header := c.Writer.Header() + + header.Del("Access-Control-Allow-Origin") + header.Del("Access-Control-Allow-Methods") + header.Del("Access-Control-Allow-Headers") + + header.Set("Access-Control-Allow-Origin", "*") + header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH") + header.Set("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type") + }