Created
May 8, 2016 16:11
-
-
Save madeinfree/ed3564fc0b89e83200ceb5c41ddd3db6 to your computer and use it in GitHub Desktop.
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
class Api::BaseController < ActionController::API | |
respond_to :json | |
# before_action :set_origin | |
# before_action :set_headers | |
before_filter :set_cors_header | |
after_filter :cors_set_access_control_headers | |
def cors_set_access_control_headers | |
headers['Access-Control-Allow-Origin'] = '*' | |
headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS' | |
headers['Access-Control-Request-Method'] = '*' | |
headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization' | |
end | |
def set_cors_header | |
headers['Access-Control-Allow-Origin'] = '*' | |
headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS' | |
headers['Access-Control-Request-Method'] = '*' | |
headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Typee, Accept, Authorization' | |
end | |
private | |
def set_origin | |
@origin = request.headers['HTTP_ORIGIN'] | |
end | |
def set_headers | |
if @origin | |
# allowed = ['lvh.me', 'localhost:8081', 'my-app.com'] | |
# allowed.each do |host| | |
# if @origin.match /^https?:\/\/#{Regexp.escape(host)}/i | |
headers['Access-Control-Allow-Origin'] = '*' | |
# break | |
# end | |
# end | |
# or '*' for public access | |
# headers['Access-Control-Allow-Origin'] = '*' | |
headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS' | |
headers['Access-Control-Request-Method'] = '*' | |
headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Typee, Accept, Authorization' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment