mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 19:53:21 +02:00
Added CORS middleware in debug mode
This commit is contained in:
+17
-3
@@ -37,15 +37,29 @@ func initArticlesRoutes(r *gin.Engine) {
|
||||
articlesRouter.DELETE("/:id", internal.DeleteArticleHandler)
|
||||
}
|
||||
|
||||
func corsMiddleware(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()
|
||||
}
|
||||
|
||||
func initGin() {
|
||||
r := gin.Default()
|
||||
r.SetTrustedProxies([]string{"localhost"})
|
||||
|
||||
ginMode := os.Getenv("GIN_MODE")
|
||||
if ginMode == "release" {
|
||||
gin.SetMode(ginMode)
|
||||
} else {
|
||||
r.Use(corsMiddleware)
|
||||
}
|
||||
|
||||
r := gin.Default()
|
||||
r.SetTrustedProxies([]string{"localhost"})
|
||||
|
||||
initBasicRoutes(r)
|
||||
initArticlesRoutes(r)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user