From 5de0efd7b4b970f43d7c40227493ebb8a5840af2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Tue, 12 Jun 2018 13:58:43 +0200 Subject: [PATCH] feat(backend): Add CORS headers to API responses --- Gopkg.lock | 9 +++++++++ Gopkg.toml | 4 ++++ main.go | 9 +++++++++ 3 files changed, 22 insertions(+) diff --git a/Gopkg.lock b/Gopkg.lock index 40280eb0e..291c0a337 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -65,6 +65,14 @@ pruneopts = "UT" revision = "563b81fc02b75d664e54da31f787c2cc2186780b" +[[projects]] + digest = "1:2b59aca2665ff804f6606c8829eaee133ddd3aefbc841014660d961b0034f888" + name = "github.com/gin-contrib/cors" + packages = ["."] + pruneopts = "UT" + revision = "cf4846e6a636a76237a28d9286f163c132e841bc" + version = "v1.2" + [[projects]] branch = "master" digest = "1:944620dd72b65b9bac1b715134540e5546ffbf84876c3fe065ec594eef640eaf" @@ -385,6 +393,7 @@ "github.com/cnf/structhash", "github.com/elazarl/go-bindata-assetfs", "github.com/getsentry/raven-go", + "github.com/gin-contrib/cors", "github.com/gin-contrib/gzip", "github.com/gin-contrib/static", "github.com/gin-gonic/contrib/sentry", diff --git a/Gopkg.toml b/Gopkg.toml index c414c74ca..a2b3909bb 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -98,3 +98,7 @@ [[constraint]] branch = "v2" name = "gopkg.in/yaml.v2" + +[[constraint]] + name = "github.com/gin-contrib/cors" + version = "1.2.0" diff --git a/main.go b/main.go index a5889bc20..ca261b10b 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( "github.com/spf13/pflag" "github.com/DeanThompson/ginpprof" + "github.com/gin-contrib/cors" "github.com/gin-contrib/gzip" "github.com/gin-contrib/static" "github.com/gin-gonic/contrib/sentry" @@ -53,6 +54,14 @@ func setupRouter(router *gin.Engine) { router.Use(gzip.Gzip(gzip.DefaultCompression)) router.Use(static.Serve(getViewURL("/static"), newBinaryFileSystem("static"))) + router.Use(cors.New(cors.Config{ + AllowAllOrigins: true, + AllowCredentials: true, + AllowMethods: []string{"GET", "POST", "DELETE"}, + AllowHeaders: []string{"Origin"}, + ExposeHeaders: []string{"Content-Length"}, + })) + router.GET(getViewURL("/favicon.ico"), favicon) router.GET(getViewURL("/"), index) router.GET(getViewURL("/help"), help)