From 617fb64dbe952e3123d24b651d1279aae70271e1 Mon Sep 17 00:00:00 2001 From: pawlaczyk Date: Wed, 9 Jan 2019 09:22:07 +0100 Subject: [PATCH] =?UTF-8?q?[CORS-POLICY]=20DZIA=C5=81A!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/main.go | 35 +++++++++++++++++++++++++-- backend/views.go | 7 ++++++ frontend/app/src/components/Popup.vue | 20 +++++++-------- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/backend/main.go b/backend/main.go index 2a5107d..3d585a8 100644 --- a/backend/main.go +++ b/backend/main.go @@ -6,16 +6,47 @@ package main import ( "net/http" - "github.com/gin-gonic/contrib/static" "github.com/gin-gonic/gin" ) +func Cors() gin.HandlerFunc { + //https://razil.cc/post/2018/10/go-cors-request-on-gin/ + return func(c *gin.Context) { + method := c.Request.Method + + origin := c.Request.Header.Get("Origin") + + if origin != "" { + c.Header("Access-Control-Allow-Origin", origin) + c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization") //自定义 Header + c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS") + c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type") + c.Header("Access-Control-Allow-Credentials", "true") + + } + + if method == "OPTIONS" { + c.Header("Access-Control-Allow-Origin", "*") + c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization") //自定义 Header + c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS") + c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type") + c.Header("Access-Control-Allow-Credentials", "true") + c.AbortWithStatus(http.StatusNoContent) + } + + c.Next() + } +} + func main() { // Set the router as the default one shipped with Gin router := gin.Default() + router.Use(Cors()) // Serve frontend static files - router.Use(static.Serve("/", static.LocalFile("./views", true))) + // router.Use(static.Serve("/", static.LocalFile("./views", true))) + + // config.AllowOrigins = []string{"http://localhost:8080/"} // Setup route group for the API api := router.Group("/api") diff --git a/backend/views.go b/backend/views.go index 7be7eb1..c0ec818 100644 --- a/backend/views.go +++ b/backend/views.go @@ -147,6 +147,13 @@ func addNewCardView(c *gin.Context) { //ale dodawanei tutaj bo i tak mozna to zrobic w tej samem domenie co couchdb //PATRZ: documentation/couchdb/local.ini; documentation/couchdb/corsy_ustawienie.txt c.Header("Content-Type", "application/json") + fmt.Println("------------------------------------------------------------------") + fmt.Println("------------------------------------------------------------------") + fmt.Println("------------------------------------------------------------------") + fmt.Println("------------------------------------------------------------------") + fmt.Println("------------------------------------------------------------------") + fmt.Println("------------------------------------------------------------------") + fmt.Println("------------------------------------------------------------------") var newCard Card c.Bind(&newCard) diff --git a/frontend/app/src/components/Popup.vue b/frontend/app/src/components/Popup.vue index e3caf7c..96ea660 100644 --- a/frontend/app/src/components/Popup.vue +++ b/frontend/app/src/components/Popup.vue @@ -32,6 +32,8 @@