diff --git a/Gemfile b/Gemfile index 2a324fc8..64632540 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ gem 'rack-ssl', '~> 1.3' gem 'rack-protection', '~> 1.3' gem 'rack-mobile-detect' gem 'sinatra' +gem 'hashr' gem 'rake-pipeline', github: 'livingsocial/rake-pipeline' gem 'rake-pipeline-web-filters', github: 'wycats/rake-pipeline-web-filters' diff --git a/Gemfile.lock b/Gemfile.lock index 762adf84..45d47aee 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -49,6 +49,11 @@ GEM pry (>= 0.9.10) terminal-table (>= 1.4.3) thor (>= 0.14.6) + handlebars (0.4.0) + commonjs (~> 0.2.3) + therubyracer (~> 0.11.1) + hashr (0.0.22) + i18n (0.6.3) json (1.7.7) libv8 (3.16.14.3) listen (2.7.9) @@ -119,7 +124,10 @@ DEPENDENCIES compass foreman guard - libv8 (~> 3.16.0) + handlebars + hashr + localeapp + localeapp-handlebars_i18n puma rack-mobile-detect rack-protection (~> 1.3) diff --git a/lib/travis/web.rb b/lib/travis/web.rb index 7d8a6f09..3aa51501 100644 --- a/lib/travis/web.rb +++ b/lib/travis/web.rb @@ -3,6 +3,11 @@ module Travis autoload :Allow, 'travis/web/allow' autoload :ApiRedirect, 'travis/web/api_redirect' autoload :App, 'travis/web/app' + autoload :Config, 'travis/web/config' autoload :SetToken, 'travis/web/set_token' end + + def self.config + @config ||= Travis::Web::Config.new + end end diff --git a/lib/travis/web/app.rb b/lib/travis/web/app.rb index 069524b2..31b436d0 100644 --- a/lib/travis/web/app.rb +++ b/lib/travis/web/app.rb @@ -33,7 +33,7 @@ class Travis::Web::App def build(options = {}) builder = Rack::Builder.new if options[:environment] == 'production' - builder.use Rack::SSL, hsts: Travis.config.ssl.hsts || true + builder.use Rack::SSL, hsts: Travis.config.ssl.hsts end builder.use Rack::Deflater builder.use Rack::Head diff --git a/lib/travis/web/config.rb b/lib/travis/web/config.rb new file mode 100644 index 00000000..902ef9f4 --- /dev/null +++ b/lib/travis/web/config.rb @@ -0,0 +1,50 @@ +require 'hashr' +require 'yaml' + +# Encapsulates the configuration necessary for travis-listener. +# +# Configuration values will be read from +# +# * either ENV['travis_config'] (this variable is set on Heroku by `travis config [env]`, +# see travis-cli) or +# * a local file config/travis.yml which contains the current env key (e.g. development, +# production, test) +# +# The env key can be set through various ENV variables, see Travis::Config.env. +# +module Travis + module Web + class Config < Hashr + class << self + def env + ENV['ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development' + end + + def load_env + @load_env ||= YAML.load(ENV['travis_config']) if ENV['travis_config'] + end + + def load_file + @load_file ||= YAML.load_file(filename)[env] if File.exists?(filename) rescue {} + end + + def filename + @filename ||= File.expand_path('config/travis.yml') + end + end + + define ssl: { hsts: true } + + default _access: [:key] + + def initialize(data = nil, *args) + data ||= self.class.load_env || self.class.load_file || {} + super + end + + def env + self.class.env + end + end + end +end