aboutsummaryrefslogtreecommitdiffstats
path: root/modules/setting
diff options
context:
space:
mode:
authorTamal Saha <tamal@appscode.com>2019-05-13 08:38:53 -0700
committertechknowlogick <techknowlogick@gitea.io>2019-05-13 11:38:53 -0400
commit34d06f4c6b23dfc458d51e9e3827c9400a87e84d (patch)
treea68b3f707251a11383ff056debfb1a933c0729d2 /modules/setting
parent6fb58a8cdcd76aa45902e50da8f2b450fe9d3d35 (diff)
downloadgitea-34d06f4c6b23dfc458d51e9e3827c9400a87e84d.tar.gz
gitea-34d06f4c6b23dfc458d51e9e3827c9400a87e84d.zip
Handle CORS requests (#6289)
Diffstat (limited to 'modules/setting')
-rw-r--r--modules/setting/cors.go41
-rw-r--r--modules/setting/setting.go1
2 files changed, 42 insertions, 0 deletions
diff --git a/modules/setting/cors.go b/modules/setting/cors.go
new file mode 100644
index 0000000000..c1c3bfb813
--- /dev/null
+++ b/modules/setting/cors.go
@@ -0,0 +1,41 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package setting
+
+import (
+ "time"
+
+ "code.gitea.io/gitea/modules/log"
+
+ "github.com/go-macaron/cors"
+)
+
+var (
+ // CORSConfig defines CORS settings
+ CORSConfig cors.Options
+ // EnableCORS defines whether CORS settings is enabled or not
+ EnableCORS bool
+)
+
+func newCORSService() {
+ sec := Cfg.Section("cors")
+ // Check cors setting.
+ EnableCORS = sec.Key("ENABLED").MustBool(false)
+
+ maxAge := sec.Key("MAX_AGE").MustDuration(10 * time.Minute)
+
+ CORSConfig = cors.Options{
+ Scheme: sec.Key("SCHEME").String(),
+ AllowDomain: sec.Key("ALLOW_DOMAIN").String(),
+ AllowSubdomain: sec.Key("ALLOW_SUBDOMAIN").MustBool(),
+ Methods: sec.Key("METHODS").Strings(","),
+ MaxAgeSeconds: int(maxAge.Seconds()),
+ AllowCredentials: sec.Key("ALLOW_CREDENTIALS").MustBool(),
+ }
+
+ if EnableCORS {
+ log.Info("CORS Service Enabled")
+ }
+}
diff --git a/modules/setting/setting.go b/modules/setting/setting.go
index 687f01bc29..d7f361c01e 100644
--- a/modules/setting/setting.go
+++ b/modules/setting/setting.go
@@ -1006,6 +1006,7 @@ func NewServices() {
NewLogServices(false)
newCacheService()
newSessionService()
+ newCORSService()
newMailService()
newRegisterMailService()
newNotifyMailService()