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 @@