diff --git a/cors.go b/cors.go index e9a98e7..ec5e2fd 100644 --- a/cors.go +++ b/cors.go @@ -5,17 +5,14 @@ import ( ) // Middleware function to handle CORS requests -func corsMiddleware() gin.HandlerFunc { - return func(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") +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" { - c.AbortWithStatus(200) - return - } - - c.Next() + if c.Request.Method == "OPTIONS" { + c.AbortWithStatus(200) + return } + } diff --git a/main.go b/main.go index 87b17c7..f0908a8 100644 --- a/main.go +++ b/main.go @@ -91,7 +91,7 @@ func main() { }) // CORS handler - engine.Use(corsMiddleware()) + engine.OPTIONS("/v1/*any", handleCORS) // get authorization config from db db.Take(&authConfig, "key = ?", "authorization")