-
-
Save raul/d3f6616ef45b6e42c3ef to your computer and use it in GitHub Desktop.
This file contains hidden or 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
namespace :fastly do | |
desc "set fastly cors headers to fix chrome/firefox font loading issues" | |
task allow_cors: :environment do | |
# check for the Cors Allow header | |
versions = Yajl.load(Excon.get("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version", | |
:headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']}).body) | |
last_version = versions.last['number'] | |
exists = Excon.get("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version/#{last_version}/header/Cors%20Allow", | |
:headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']}).status | |
if(exists == 200) | |
puts "Allow Cors already set. Done." | |
else | |
cloned = Yajl.load(Excon.put("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version/#{last_version}/clone", | |
:headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']}).body) | |
cloned_version = cloned['number'] | |
puts cloned | |
response = Yajl.load(Excon.post("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version/#{cloned_version}/header", | |
:headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']}, | |
:body => 'name=Cors Allow&type=response&action=set&dst=http.Access-Control-Allow-Origin&src="*"&ignore_if_set=0&priority=10').body) | |
puts response | |
activated = Excon.put("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version/#{cloned_version}/activate", | |
:headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']}).status | |
if(activated == 200) | |
puts "Allow Cors header set. Done." | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment