From 3b05ed599d32018d3d299e252a79df2c3bf89293 Mon Sep 17 00:00:00 2001 From: pirotyyy Date: Sat, 16 Mar 2024 09:36:31 +0900 Subject: [PATCH 1/2] feat: go get cors module --- go.mod | 1 + go.sum | 3 +++ 2 files changed, 4 insertions(+) diff --git a/go.mod b/go.mod index 1aba95a..7e0626d 100644 --- a/go.mod +++ b/go.mod @@ -41,6 +41,7 @@ require ( github.com/felixge/httpsnoop v1.0.4 // indirect github.com/frankban/quicktest v1.14.6 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gin-contrib/cors v1.7.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-kit/kit v0.13.0 // indirect github.com/go-logr/logr v1.4.1 // indirect diff --git a/go.sum b/go.sum index aa72bc8..5050bc6 100644 --- a/go.sum +++ b/go.sum @@ -76,6 +76,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gin-contrib/cors v1.7.0 h1:wZX2wuZ0o7rV2/1i7gb4Jn+gW7HBqaP91fizJkBUJOA= +github.com/gin-contrib/cors v1.7.0/go.mod h1:cI+h6iOAyxKRtUtC6iF/Si1KSFvGm/gK+kshxlCi8ro= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= @@ -425,6 +427,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U= gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/src-d/go-errors.v1 v1.0.0 h1:cooGdZnCjYbeS1zb1s6pVAAimTdKceRrpn7aKOnNIfc= gopkg.in/src-d/go-errors.v1 v1.0.0/go.mod h1:q1cBlomlw2FnDBDNGlnh6X0jPihy+QxZfMMNxPCbdYg= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= From 05b5afc09a6ebd2dc109926946624377305181a9 Mon Sep 17 00:00:00 2001 From: pirotyyy Date: Sat, 16 Mar 2024 09:36:41 +0900 Subject: [PATCH 2/2] feat: setting cors --- svc/cmd/dev/main.go | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/svc/cmd/dev/main.go b/svc/cmd/dev/main.go index 4157cc8..3f2aead 100644 --- a/svc/cmd/dev/main.go +++ b/svc/cmd/dev/main.go @@ -7,7 +7,9 @@ import ( "a-project-backend/svc/pkg/middleware" "fmt" "log" + "time" + "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "gorm.io/driver/mysql" "gorm.io/gorm" @@ -48,6 +50,34 @@ func main() { } engine := gin.Default() + engine.Use(cors.New(cors.Config{ + // アクセスを許可したいアクセス元 + AllowOrigins: []string{ + "http://localhost:3000", + }, + // アクセスを許可したいHTTPメソッド(以下の例だとPUTやDELETEはアクセスできません) + AllowMethods: []string{ + "POST", + "GET", + "PUT", + "DELETE", + "OPTIONS", + }, + // 許可したいHTTPリクエストヘッダ + AllowHeaders: []string{ + "Access-Control-Allow-Credentials", + "Access-Control-Allow-Headers", + "Content-Type", + "Content-Length", + "Accept-Encoding", + "Authorization", + }, + // cookieなどの情報を必要とするかどうか + AllowCredentials: true, + // preflightリクエストの結果をキャッシュする時間 + MaxAge: 24 * time.Hour, + })) + engine.GET("/health", func(c *gin.Context) { c.JSON(200, gin.H{"status": "ok"}) }) @@ -64,8 +94,8 @@ func main() { } func Implement(rg *gin.RouterGroup, db *gorm.DB, g *gcs.GCS) error { - middlewareCROS := middleware.NewCORS() - middlewareCROS.ConfigureCORS(rg) + //middlewareCROS := middleware.NewCORS() + //middlewareCROS.ConfigureCORS(rg) middlewareAuth := middleware.NewAuth(db) authRg := rg.Use(middlewareAuth.VerifyUser())