diff --git a/go.mod b/go.mod index 7781d2c..f5fe555 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/nats-io/go-nats v1.7.0 github.com/nats-io/nkeys v0.0.2 // indirect github.com/nats-io/nuid v1.0.0 // indirect + github.com/rs/cors v1.6.0 github.com/satori/go.uuid v1.2.0 github.com/spf13/viper v1.3.1 github.com/stretchr/testify v1.3.0 // indirect diff --git a/go.sum b/go.sum index 4927578..f2d1d3b 100644 --- a/go.sum +++ b/go.sum @@ -56,6 +56,8 @@ github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181 github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI= +github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= diff --git a/server.go b/server.go index 5ed32f2..cf467a6 100644 --- a/server.go +++ b/server.go @@ -4,6 +4,7 @@ import ( "net/http" "github.com/gorilla/mux" + "github.com/rs/cors" "github.com/spaceuptech/space-cloud/config" "github.com/spaceuptech/space-cloud/modules/auth" @@ -34,7 +35,19 @@ func initServer(isProd bool) *server { } func (s *server) start(port string) error { - return http.ListenAndServe(":"+port, s.router) + // Allow cors + corsObj := cors.New(cors.Options{ + AllowCredentials: true, + AllowOriginFunc: func(s string) bool { + return true + }, + AllowedMethods: []string{"GET", "PUT", "POST", "DELETE"}, + AllowedHeaders: []string{"Authorization", "Content-Type"}, + ExposedHeaders: []string{"Authorization", "Content-Type"}, + }) + + handler := corsObj.Handler(s.router) + return http.ListenAndServe(":"+port, handler) } func (s *server) loadConfig(config *config.Project) error {