Skip to content

Commit

Permalink
fix(bff): add options to cors
Browse files Browse the repository at this point in the history
  • Loading branch information
vbeaucha committed Aug 14, 2023
1 parent 9a14a7e commit 23f5870
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions BFF/services/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"ogree-bff/models"
"ogree-bff/utils/token"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)

Expand All @@ -33,6 +32,24 @@ func APIMiddleware(apiList []models.API) gin.HandlerFunc {
}

}

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,DELETE")

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

c.Next()
}
}


func initDevices(protected, unprotected *gin.RouterGroup) {

protected.GET("/deviceComp/:entity", handlers.GetDevices)
Expand Down Expand Up @@ -110,16 +127,13 @@ func InitRouter(apiList []models.API, env string) *gin.Engine {
}

router := gin.Default()
router.Use(CORSMiddleware())

corsConfig := cors.DefaultConfig()
corsConfig.AllowAllOrigins = true
router.Use(cors.New(corsConfig))


protected := router.Group("/api")
protected.Use(JwtAuthMiddleware())
protected.Use(APIMiddleware(apiList))


unprotected := router.Group("/api")
unprotected.Use(APIMiddleware(apiList))

Expand Down

0 comments on commit 23f5870

Please sign in to comment.