allow cors

This commit is contained in:
2023-07-18 14:35:15 +08:00
parent f0c7a95f77
commit c35f169e3b
2 changed files with 24 additions and 0 deletions

21
cors.go Normal file
View File

@@ -0,0 +1,21 @@
package main
import (
"github.com/gin-gonic/gin"
)
// 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")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(200)
return
}
c.Next()
}
}