Rework application configuration

Use a preinitializer to load the settings from application.yml so
that they are available as early as possible. All settings can also
be overridden using environment variables.

The ad-hoc settins in environment.rb are then moved to this new
system so we have one consistent location for settings.
This commit is contained in:
Tom Hughes 2010-08-04 22:06:05 +01:00
parent 8b9cacd3c2
commit f07819d81a
33 changed files with 100 additions and 99 deletions

View file

@ -1,4 +1,21 @@
standard_settings: &standard_settings
# The server URL
server_url: "www.openstreetmap.org"
# The generator
generator: "OpenStreetMap server"
# Sender addresses for emails
email_from: "OpenStreetMap <webmaster@openstreetmap.org>"
email_return_path: "bounces@openstreetmap.org"
# API version
api_version: "0.6"
# Application status - posstible values are:
# :online - online and operating normally
# :api_readonly - site online but API in read-only mode
# :api_offline - site online but API offline
# :database_readonly - database and site in read-only mode
# :database_offline - database offline with site in emergency mode
# :gpx_offline - gpx storage offline
status: :online
# The maximum area you're allowed to request, in square degrees
max_request_area: 0.25
# Number of GPS trace/trackpoints returned per-page

View file

@ -1,36 +1,8 @@
# Be sure to restart your server when you modify this file
# Uncomment below to force Rails into production mode when
# you don't control web/app server and can't set it the proper way
ENV['RAILS_ENV'] ||= 'production'
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.3.8' unless defined? RAILS_GEM_VERSION
# Set the server URL
SERVER_URL = ENV['OSM_SERVER_URL'] || 'www.openstreetmap.org'
# Set the generator
GENERATOR = ENV['OSM_SERVER_GENERATOR'] || 'OpenStreetMap server'
# Settings for generated emails (e.g. signup confirmation
EMAIL_FROM = ENV['OSM_EMAIL_FROM'] || 'OpenStreetMap <webmaster@openstreetmap.org>'
EMAIL_RETURN_PATH = ENV['OSM_EMAIL_RETURN_PATH'] || 'bounces@openstreetmap.org'
# Application constants needed for routes.rb - must go before Initializer call
API_VERSION = ENV['OSM_API_VERSION'] || '0.6'
# Set application status - possible settings are:
#
# :online - online and operating normally
# :api_readonly - site online but API in read-only mode
# :api_offline - site online but API offline
# :database_readonly - database and site in read-only mode
# :database_offline - database offline with site in emergency mode
# :gpx_offline - gpx storage offline
#
OSM_STATUS = :online
# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
@ -43,7 +15,7 @@ Rails::Initializer.run do |config|
# config.load_paths += %W( #{RAILS_ROOT}/extras )
# Specify gems that this application depends on and have them installed with rake gems:install
unless OSM_STATUS == :database_offline
unless STATUS == :database_offline
config.gem 'composite_primary_keys', :version => '2.2.2'
end
config.gem 'libxml-ruby', :version => '>= 1.1.1', :lib => 'libxml'
@ -59,7 +31,7 @@ Rails::Initializer.run do |config|
# Skip frameworks you're not going to use. To use Rails without a database,
# you must remove the Active Record framework.
if OSM_STATUS == :database_offline
if STATUS == :database_offline
config.frameworks -= [ :active_record ]
config.eager_load_paths = []
end

View file

@ -1,4 +0,0 @@
# This file loads various yml configuration files
# Load application config
APP_CONFIG = YAML.load(File.read(RAILS_ROOT + "/config/application.yml"))[RAILS_ENV]

View file

@ -1,11 +1,11 @@
# Setup any specified hard limit on the virtual size of the process
if APP_CONFIG.include?('hard_memory_limit') and Process.const_defined?(:RLIMIT_AS)
Process.setrlimit Process::RLIMIT_AS, APP_CONFIG['hard_memory_limit']*1024*1024, Process::RLIM_INFINITY
if defined?(HARD_MEMORY_LIMIT) and Process.const_defined?(:RLIMIT_AS)
Process.setrlimit Process::RLIMIT_AS, HARD_MEMORY_LIMIT*1024*1024, Process::RLIM_INFINITY
end
# If we're running under passenger and a soft memory limit is
# configured then setup some rack middleware to police the limit
if APP_CONFIG.include?('soft_memory_limit') and defined?(PhusionPassenger)
if defined?(SOFT_MEMORY_LIMIT) and defined?(PhusionPassenger)
# Define some rack middleware to police the soft memory limit
class MemoryLimit
def initialize(app)
@ -17,7 +17,7 @@ if APP_CONFIG.include?('soft_memory_limit') and defined?(PhusionPassenger)
status, headers, body = @app.call(env)
# Restart if we've hit our memory limit
if resident_size > APP_CONFIG['soft_memory_limit']
if resident_size > SOFT_MEMORY_LIMIT
Process.kill("USR1", 0)
end

View file

@ -12,6 +12,6 @@ ActionController::Base.session = {
# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
# (create the session table with "rake db:sessions:create")
unless OSM_STATUS == :database_offline or OSM_STATUS == :database_readonly
unless STATUS == :database_offline or STATUS == :database_readonly
ActionController::Base.session_store = :sql_session_store
end

View file

@ -4,6 +4,6 @@ adapter = Rails.configuration.database_configuration[environment]["adapter"]
session_class = adapter + "_session"
# Configure SqlSessionStore
unless OSM_STATUS == :database_offline
unless STATUS == :database_offline
SqlSessionStore.session_class = session_class.camelize.constantize
end

16
config/preinitializer.rb Normal file
View file

@ -0,0 +1,16 @@
require 'yaml'
config = YAML.load_file("#{RAILS_ROOT}/config/application.yml")
env = ENV['RAILS_ENV'] || 'development'
ENV.each do |key,value|
if key.match(/^OSM_(.*)$/)
Object.const_set(Regexp.last_match(1).upcase, value)
end
end
config[env].each do |key,value|
unless Object.const_defined?(key.upcase)
Object.const_set(key.upcase, value)
end
end