fix duplicated cors headers

This commit is contained in:
2024-01-04 19:03:58 +08:00
parent 9fdbf259c0
commit 2bbe98e694
3 changed files with 29 additions and 4 deletions

15
cors.go
View File

@@ -4,10 +4,19 @@ import (
"github.com/gin-gonic/gin"
)
// this function is aborded
func corsMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH")
c.Header("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type")
// set cors header
header := c.Request.Header
if header.Get("Access-Control-Allow-Origin") == "" {
c.Header("Access-Control-Allow-Origin", "*")
}
if header.Get("Access-Control-Allow-Methods") == "" {
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH")
}
if header.Get("Access-Control-Allow-Headers") == "" {
c.Header("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type")
}
}
}