Skip to content

Commit

Permalink
Merge branch 'main' of github.com:OnlyNico43/gin-cors
Browse files Browse the repository at this point in the history
  • Loading branch information
OnlyNico43 committed Nov 1, 2024
2 parents cc749eb + 7679ce4 commit 1710152
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func CorsMiddleware(config Config) gin.HandlerFunc {
c.AbortWithStatus(http.StatusForbidden)
}

if slices.Contains(config.AllowedOrigins, "*") {
currentOrigin = "*"
}

preflight := strings.ToUpper(c.Request.Method) == "OPTIONS"
if preflight {
// Headers for preflight requests
Expand All @@ -142,7 +146,7 @@ func CorsMiddleware(config Config) gin.HandlerFunc {
}

// Headers for all requests
c.Writer.Header().Set("Access-Control-Allow-Origin", strings.Join(config.AllowedOrigins, ", "))
c.Writer.Header().Set("Access-Control-Allow-Origin", currentOrigin)
c.Writer.Header().Set("Access-Control-Allow-Credentials", strconv.FormatBool(config.AllowCredentials))
c.Writer.Header().Set("Access-Control-Expose-Headers", strings.Join(config.ExposeHeaders, ", "))

Expand Down

0 comments on commit 1710152

Please sign in to comment.