Skip to content

Commit 673baf3

Browse files
authored
Merge pull request #62 from qkgo/feat/env-cors
cors setup
2 parents 4b6140a + 88e30d1 commit 673baf3

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

pkg/util/http-util.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,6 @@ func GetRouter(
186186
gin.SetMode(gin.ReleaseMode)
187187
gin.DefaultWriter = ioutil.Discard
188188
router := gin.Default()
189-
corsDefault := cors.DefaultConfig()
190-
corsDefault.AllowCredentials = true
191-
corsDefault.AllowHeaders = []string{"Origin", "token", "Content-Length", "Content-Type", "session", "DNT", "content-type", "s", "timezone", "tz", "specify", "order"}
192-
corsDefault.AllowAllOrigins = true
193189
if os.Getenv("IGNORE_ROOT") == "" {
194190
router.GET("/", DefaultView)
195191
}
@@ -201,8 +197,14 @@ func GetRouter(
201197
if os.Getenv("HTTP_DETAIL") == "" {
202198
router.Use(RequestLogger())
203199
}
200+
if os.Getenv("NEED_CORS") != "" {
201+
corsDefault := cors.DefaultConfig()
202+
corsDefault.AllowCredentials = true
203+
corsDefault.AllowHeaders = []string{"Origin", "token", "Content-Length", "Content-Type", "session", "DNT", "content-type", "s", "timezone", "tz", "specify", "order"}
204+
corsDefault.AllowAllOrigins = true
205+
router.Use(cors.New(corsDefault))
206+
}
204207
router.Use(GinToLogrus())
205-
router.Use(cors.New(corsDefault))
206208
c := gin.LoggerConfig{
207209
Output: ioutil.Discard,
208210
SkipPaths: []string{"*"},

0 commit comments

Comments
 (0)