clean up config to use global settings

This commit is contained in:
Sam 2013-12-20 15:12:23 +11:00
parent 66afabcf4c
commit b312b4d563
4 changed files with 90 additions and 37 deletions

View File

@ -0,0 +1,65 @@
class GlobalSetting
def self.available_settings(*settings)
settings.each do |name, desc, default|
define_singleton_method(name) do
provider.lookup(name, default)
end
end
end
def self.generate_sample_file(file)
end
available_settings(
[:db_pool, "connection pool size", 5],
[:db_timeout, "database timeout in milliseconds", 5000],
[:db_socket, "socket file used to access db", ""],
[:db_host, "host address for db server", "localhost"],
[:db_port, "port running db server", 5432],
[:db_name, "database name running discourse", "discourse"],
[:db_username, "username accessing database", "discourse"],
[:db_password, "password used to access the db", ""],
[:hostname, "hostname running the forum", "www.example.com"],
[:smtp_address, "address of smtp server used to send emails",""],
[:smtp_port, "port of smtp server used to send emails", 25],
[:smtp_domain, "domain passed to smtp server", ""],
[:smtp_user_name, "username for smtp server", ""],
[:smtp_password, "password for smtp server", ""],
[:smtp_enable_start_tls, "enable TLS encryption for smtp connections", true],
[:enable_mini_profiler, "enable MiniProfiler for administrators", true],
[:cdn_url, "recommended, cdn used to access assets", ""],
[:developer_emails, "comma delimited list of emails that have devloper level access", true],
[:redis_host, "redis server address", "localhost"],
[:redis_port, "redis server port", 6379],
[:redis_password, "redis password", ""]
)
class BaseProvider
def lookup(name, val)
t = ENV["D_" << name.to_s.upcase]
if t.present?
t
else
val.present? ? val : nil
end
end
end
class FileProvider
def self.from(location)
end
end
class EnvProvider
end
class << self
attr_accessor :provider
end
@provider =
FileProvider.from(Rails.root + '/config/discourse.conf') ||
EnvProvider.new
end

View File

@ -37,15 +37,14 @@ profile:
- "localhost" - "localhost"
production: production:
pool: <%= ENV['POSTGRES_CONNECTION_POOL'] || 5 %> pool: <%= GlobalSetting.db_pool %>
timeout: 5000 timeout: <%= GlobalSetting.db_timeout %>
adapter: postgresql adapter: postgresql
uri: <%= uri = URI.parse(ENV['POSTGRES_URL'] ? ENV['POSTGRES_URL'] : "pg://localhost") %> socket: <%= GlobalSetting.db_socket %>
socket: <%= ENV['POSTGRES_SOCKET'] %> host: <%= GlobalSetting.db_host %>
host: <%= ENV['POSTGRES_SOCKET'].present? ? nil : uri.host %> port: <%= GlobalSetting.db_port %>
port: <%= uri.port || (ENV['POSTGRES_SOCKET'].present? ? nil : 5432) %> database: <%= GlobalSetting.db_name %>
database: <%= ENV['POSTGRES_DB'] || "discourse" %> username: <%= GlobalSetting.db_username %>
username: <%= uri.user %> password: <%= GlobalSetting.db_password %>
password: <%= uri.password %>
host_names: host_names:
- <%= ENV["DISCOURSE_HOSTNAME"] || raise("env var for DISCOURSE_HOSTNAME must be set") if Rails.env == "production" %> # Update this to be the domain of your production site - <%= GlobalSetting.hostname %>

View File

@ -32,25 +32,15 @@ Discourse::Application.configure do
# the I18n.default_locale when a translation can not be found) # the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true config.i18n.fallbacks = true
# specify your smtp url using the SMTP_URL env var eg: if GlobalSetting.smtp_address
# SMTP_URL=smtp://user:password@myhost.com config.action_mailer.smtp_settings = {
if ENV.key?('SMTP_URL') address: GlobalSetting.smtp_address,
config.action_mailer.smtp_settings = begin port: GlobalSetting.smtp_port,
uri = URI.parse(ENV['SMTP_URL']) domain: GlobalSetting.smtp_domain,
params = { user_name: GlobalSetting.smtp_password,
:address => uri.host, authentication: 'plain',
:port => uri.port || 25, enable_starttls_auto: GlobalSetting.smtp_enable_start_tls
:domain => (uri.path || "").split("/")[1], }
:user_name => uri.user,
:password => uri.password,
:authentication => 'plain',
:enable_starttls_auto => !ENV['SMTP_DISABLE_TLS']
}
CGI.parse(uri.query || "").each {|k,v| params[k.to_sym] = v.first}
params
rescue
raise "Invalid SMTP_URL"
end
else else
config.action_mailer.delivery_method = :sendmail config.action_mailer.delivery_method = :sendmail
config.action_mailer.sendmail_settings = {arguments: '-i'} config.action_mailer.sendmail_settings = {arguments: '-i'}
@ -63,16 +53,16 @@ Discourse::Application.configure do
config.handlebars.precompile = true config.handlebars.precompile = true
# allows admins to use mini profiler # allows admins to use mini profiler
config.enable_mini_profiler = !ENV["DISABLE_MINI_PROFILER"] config.enable_mini_profiler = GlobalSetting.enable_mini_profiler
# Discourse strongly recommend you use a CDN. # Discourse strongly recommend you use a CDN.
# For origin pull cdns all you need to do is register an account and configure # For origin pull cdns all you need to do is register an account and configure
config.action_controller.asset_host = ENV["CDN_URL"] if ENV["CDN_URL"] config.action_controller.asset_host = GlobalSetting.cdn_url
# a comma delimited list of emails your devs have # a comma delimited list of emails your devs have
# developers have god like rights and may impersonate anyone in the system # developers have god like rights and may impersonate anyone in the system
# normal admins may only impersonate other moderators (not admins) # normal admins may only impersonate other moderators (not admins)
if emails = ENV["DEVELOPER_EMAILS"] if emails = GlobalSetting.developer_emails
config.developer_emails = emails.split(",") config.developer_emails = emails.split(",")
end end

View File

@ -1,8 +1,7 @@
defaults: &defaults defaults: &defaults
uri: <%= uri = URI.parse(ENV['REDIS_PROVIDER_URL'] || "redis://localhost:6379") %> host: <%= GlobalSetting.redis_host %>
host: <%= uri.host %> port: <%= GlobalSetting.redis_port %>
port: <%= uri.port %> password: <%= GlobalSetting.redis_password %>
password: <%= uri.password %>
db: 0 db: 0
cache_db: 2 cache_db: 2
@ -20,4 +19,4 @@ staging:
<<: *defaults <<: *defaults
production: production:
<<: *defaults <<: *defaults