ソースを参照

add middleware of cors

Paul 6 年 前
コミット
35267b6a0a
共有3 個のファイルを変更した41 個の追加1 個の削除を含む
  1. 7
    1
      Gopkg.lock
  2. 4
    0
      Gopkg.toml
  3. 30
    0
      middleware/cors/cors.go

+ 7
- 1
Gopkg.lock ファイルの表示

@@ -1,6 +1,12 @@
1 1
 # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
2 2
 
3 3
 
4
+[[projects]]
5
+  name = "github.com/gin-contrib/cors"
6
+  packages = ["."]
7
+  revision = "cf4846e6a636a76237a28d9286f163c132e841bc"
8
+  version = "v1.2"
9
+
4 10
 [[projects]]
5 11
   branch = "master"
6 12
   name = "github.com/gin-contrib/sse"
@@ -90,6 +96,6 @@
90 96
 [solve-meta]
91 97
   analyzer-name = "dep"
92 98
   analyzer-version = 1
93
-  inputs-digest = "69a38d98563bce4ac32cfa2afc36aa3298629f41568f13a4b6d3d18d97b22f67"
99
+  inputs-digest = "97fe6c44fd65faf279ffdc31df5bafbcb85cbf2945ee4cac49217b5186d2dc1b"
94 100
   solver-name = "gps-cdcl"
95 101
   solver-version = 1

+ 4
- 0
Gopkg.toml ファイルの表示

@@ -32,3 +32,7 @@
32 32
 [prune]
33 33
   go-tests = true
34 34
   unused-packages = true
35
+
36
+[[constraint]]
37
+  name = "github.com/gin-contrib/cors"
38
+  version = "1.2.0"

+ 30
- 0
middleware/cors/cors.go ファイルの表示

@@ -0,0 +1,30 @@
1
+package cors
2
+
3
+import (
4
+	"net/http"
5
+	"strings"
6
+
7
+	"github.com/gin-contrib/cors"
8
+	"github.com/gin-gonic/gin"
9
+)
10
+
11
+func Cors() gin.HandlerFunc {
12
+	config := cors.DefaultConfig()
13
+
14
+	config.AddAllowMethods(http.MethodDelete, http.MethodOptions, http.MethodPatch)
15
+	config.AddAllowHeaders("Authorization", "X-Require-Cookie")
16
+	config.AllowCredentials = true
17
+	config.AllowOriginFunc = func(origin string) bool {
18
+		if gin.Mode() == gin.DebugMode {
19
+			return true
20
+		}
21
+
22
+		if strings.Contains(origin, "links123.com") {
23
+			return true
24
+		}
25
+
26
+		return false
27
+	}
28
+
29
+	return cors.New(config)
30
+}