Skip to content

Instantly share code, notes, and snippets.

@jmoe
Last active August 29, 2015 14:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jmoe/2c1ac03d0f5cc02ee2be to your computer and use it in GitHub Desktop.
Save jmoe/2c1ac03d0f5cc02ee2be to your computer and use it in GitHub Desktop.
Quick rake task to automate setting CORS headers on fastly
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