-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some days I wonder why we bother taking a whole gem dependency when 10 lines of code does the job right
- Loading branch information
1 parent
1b733ff
commit 46c4063
Showing
3 changed files
with
19 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
46c4063
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍