Skip to content

Commit

Permalink
FIX: cors setting was broken
Browse files Browse the repository at this point in the history
Some days I wonder why we bother taking a whole gem
dependency when 10 lines of code does the job right
  • Loading branch information
SamSaffron committed Jul 23, 2014
1 parent 1b733ff commit 46c4063
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ gem 'htmlentities', require: false
gem 'flamegraph', require: false
gem 'rack-mini-profiler', require: false

# used for caching, optional
gem 'rack-cors', require: false
gem 'unicorn', require: false
gem 'puma', require: false
gem 'rbtrace', require: false, platform: :mri
Expand Down
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ GEM
qunit-rails (0.0.7)
railties
rack (1.5.2)
rack-cors (0.2.9)
rack-mini-profiler (0.9.1)
rack (>= 1.1.3)
rack-openid (1.3.1)
Expand Down Expand Up @@ -455,7 +454,6 @@ DEPENDENCIES
pry-rails
puma
qunit-rails
rack-cors
rack-mini-profiler
rack-protection
rails
Expand Down
25 changes: 19 additions & 6 deletions config/initializers/08-rack-cors.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
if GlobalSetting.enable_cors
require 'rack/cors'
if GlobalSetting.enable_cors && GlobalSetting.cors_origin.present?

Rails.configuration.middleware.use Rack::Cors do
allow do
origins GlobalSetting.cors_origin.split(',').map(&:strip)
resource '*', headers: :any, methods: [:get, :post, :options]
class Discourse::Cors
def initialize(app, options = nil)
@app = app
@origins = GlobalSetting.cors_origin.split(',').map(&:strip)
end

def call(env)
status, headers, body = @app.call(env)
origin = nil

if origin = env['HTTP_ORIGIN']
origin = nil unless @origins.include? origin
end

headers['Access-Control-Allow-Origin'] = origin || @origins[0]
[status,headers,body]
end
end

Rails.configuration.middleware.insert 0, Discourse::Cors
end

1 comment on commit 46c4063

@arpitjalan
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.