Skip to content

Commit

Permalink
feat: added CORS middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Molaryy committed Nov 19, 2023
1 parent 04b6818 commit ccea48a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
19 changes: 19 additions & 0 deletions backend/middlewares/cors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package middlewares

import "github.com/gin-gonic/gin"

func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")

if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}

c.Next()
}
}
1 change: 1 addition & 0 deletions backend/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func authHandler(r *gin.Engine) {
}

func router(r *gin.Engine) {
r.Use(middlewares.CORSMiddleware())
authHandler(r)
todosHandler(r)
r.NoRoute(RouteNotFound)
Expand Down

0 comments on commit ccea48a

Please sign in to comment.