Skip to content

Commit

Permalink
Added rack-cors gem and initial settings (#595)
Browse files Browse the repository at this point in the history
* Added rack-cors gem and initial settings
  • Loading branch information
Vladimir Bazhanov authored Jul 14, 2020
1 parent 36dbdda commit 2f35cc6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ PORT=5000
# Host name of the application
HOST=lvh.me

# Enabled origins
CORS_ORIGINS=127.0.0.1:5000,localhost:5000,lvh.me:5000

# Current environment
RACK_ENV=development

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ gem "memory_profiler"
gem "puma"
gem "pundit"
gem "rack-canonical-host"
gem "rack-cors"
gem "rack-mini-profiler", require: false, git: "https://github.com/MiniProfiler/rack-mini-profiler.git"
gem "responders"
gem "rollbar"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ GEM
rack-canonical-host (0.2.2)
addressable (> 0, < 3)
rack (>= 1.0.0, < 3)
rack-cors (1.1.1)
rack (>= 2.0.0)
rack-proxy (0.6.5)
rack
rack-test (1.1.0)
Expand Down Expand Up @@ -525,6 +527,7 @@ DEPENDENCIES
puma
pundit
rack-canonical-host
rack-cors
rack-mini-profiler!
rails (= 6.0.3.2)
rails-erd
Expand Down
16 changes: 16 additions & 0 deletions config/initializers/cors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Be sure to restart your server when you modify this file.

# Avoid CORS issues when API is called from the frontend app.
# Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests.

# Read more: https://github.com/cyu/rack-cors

if ENV["CORS_ORIGINS"].present?
Rails.application.config.middleware.insert_before 0, Rack::Cors, debug: true, logger: (-> { Rails.logger }) do
allow do
origins(*ENV.fetch("CORS_ORIGINS").split(","))

resource "/assets/*", headers: :any, methods: :get
end
end
end

0 comments on commit 2f35cc6

Please sign in to comment.