From bb6e6612dc804ed1d554cd9d2fa13664d17b4244 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Fri, 6 Mar 2015 14:03:11 +0000 Subject: [PATCH] Add CORS and set JSON response type. --- .env.example | 1 + Gemfile | 2 ++ Gemfile.lock | 2 ++ config.ru | 8 ++++++++ tubestatus.rb | 2 ++ 5 files changed, 15 insertions(+) diff --git a/.env.example b/.env.example index 87af043..a68d181 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,2 @@ export TUBESTATUS_FEED_EMAIL= +export CORS_ALLOW_ORIGINS= diff --git a/Gemfile b/Gemfile index 2065886..79ce2fa 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,8 @@ gem 'faraday' gem 'xml-simple' gem 'foreman' +gem 'rack-cors' + gem 'newrelic_rpm' group :development do diff --git a/Gemfile.lock b/Gemfile.lock index f7d70e2..625a01b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -12,6 +12,7 @@ GEM multipart-post (2.0.0) newrelic_rpm (3.10.0.279) rack (1.6.0) + rack-cors (0.3.1) rack-protection (1.5.3) rack shotgun (0.9.1) @@ -35,6 +36,7 @@ DEPENDENCIES faraday foreman newrelic_rpm + rack-cors shotgun sinatra thin diff --git a/config.ru b/config.ru index c97fdee..6acef93 100644 --- a/config.ru +++ b/config.ru @@ -1,4 +1,12 @@ require 'bundler' +require 'rack/cors' + +use Rack::Cors do + allow do + origins ENV['CORS_ALLOW_ORIGINS'] + resource '*', headers: :any + end +end Bundler.setup require './tubestatus' diff --git a/tubestatus.rb b/tubestatus.rb index c30c49c..92dec26 100644 --- a/tubestatus.rb +++ b/tubestatus.rb @@ -29,6 +29,7 @@ class Tubestatus < Sinatra::Base :messages => line["StatusDetails"].empty? ? [] : [line["StatusDetails"]] } end + content_type :json JSON data end @@ -49,6 +50,7 @@ class Tubestatus < Sinatra::Base :messages => messages } end + content_type :json JSON data end end