mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-24 08:09:13 -05:00
46c406360d
Some days I wonder why we bother taking a whole gem dependency when 10 lines of code does the job right
23 lines
583 B
Ruby
23 lines
583 B
Ruby
if GlobalSetting.enable_cors && GlobalSetting.cors_origin.present?
|
|
|
|
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
|