Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
Made app configurable without modifying any files
Browse files Browse the repository at this point in the history
Settings are loaded from the file pointed to by the SETTINGS_PATH
environment variable. That file contains the location of all settings,
including a path for the database configuration and for logging.

This allows the executor of this application to customize all settings
without creating or modifying any files within the application.
  • Loading branch information
David Stevenson and Ian Lesperance committed Sep 30, 2013
1 parent 34acf6e commit 1f9f3d1
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion app/controllers/v2/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class V2::BaseController < ApplicationController

def authenticate
authenticate_or_request_with_http_basic do |_, password|
password == ENV['AUTH_TOKEN']
password == Settings.auth_token
end
end
end
4 changes: 0 additions & 4 deletions app/models/app_settings.rb

This file was deleted.

5 changes: 0 additions & 5 deletions config/app_settings.yml

This file was deleted.

6 changes: 5 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)

require File.expand_path('../../lib/settings', __FILE__)

module CfMysqlBroker
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
Expand All @@ -30,6 +32,8 @@ class Application < Rails::Application

config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]

config.paths.add 'config/database', with: Settings.database_config_path
config.paths.add 'log', with: Settings.log_path
end
end

4 changes: 4 additions & 0 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# This file should not be used in deployed environments. Instead, the
# application configuration file specified by SETTINGS_PATH should
# include the path to a database configuration that includes these
# settings.

development:
adapter: mysql2
Expand Down
14 changes: 14 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This file should not be used in deployed environments. Instead, set
# the SETTINGS_PATH environment variable to point to a configuration
# file that contains these settings.

defaults: &defaults
database_config_path: 'config/database.yml'
log_path: 'log/<%= Rails.env %>.log'
auth_token: 'secret'

development:
<<: *defaults

test:
<<: *defaults
6 changes: 6 additions & 0 deletions lib/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ENV['SETTINGS_PATH'] ||= File.expand_path('../../config/settings.yml', __FILE__)

class Settings < Settingslogic
source ENV['SETTINGS_PATH']
namespace Rails.env
end
12 changes: 1 addition & 11 deletions spec/support/controller_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
module ControllerHelpers
extend ActiveSupport::Concern

included do
before do
@__original_auth_token, ENV['AUTH_TOKEN'] = ENV['AUTH_TOKEN'], 'secret'
end

after do
ENV['AUTH_TOKEN'], @__original_auth_token = @__original_auth_token, nil
end
end

def authenticate
username = 'test'
password = 'secret'
password = Settings.auth_token

request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(username, password)
end
Expand Down
10 changes: 1 addition & 9 deletions spec/support/request_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@ module RequestHelpers
included do
let(:default_env) do
username = 'test'
password = 'secret'
password = Settings.auth_token

{
'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(username, password)
}
end
let(:env) { default_env }

before do
@__original_auth_token, ENV['AUTH_TOKEN'] = ENV['AUTH_TOKEN'], 'secret'
end

after do
ENV['AUTH_TOKEN'], @__original_auth_token = @__original_auth_token, nil
end
end
end

Expand Down

0 comments on commit 1f9f3d1

Please sign in to comment.