From b312b4d563da4f31310ea0e9ea97e17c360efa29 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 20 Dec 2013 15:12:23 +1100 Subject: [PATCH] clean up config to use global settings --- app/models/global_setting.rb | 65 +++++++++++++++++++++++++++++++ config/database.yml | 19 +++++---- config/environments/production.rb | 34 ++++++---------- config/redis.yml | 9 ++--- 4 files changed, 90 insertions(+), 37 deletions(-) create mode 100644 app/models/global_setting.rb diff --git a/app/models/global_setting.rb b/app/models/global_setting.rb new file mode 100644 index 00000000000..8afef710ecc --- /dev/null +++ b/app/models/global_setting.rb @@ -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 diff --git a/config/database.yml b/config/database.yml index 7282e5f1d92..65da064a842 100644 --- a/config/database.yml +++ b/config/database.yml @@ -37,15 +37,14 @@ profile: - "localhost" production: - pool: <%= ENV['POSTGRES_CONNECTION_POOL'] || 5 %> - timeout: 5000 + pool: <%= GlobalSetting.db_pool %> + timeout: <%= GlobalSetting.db_timeout %> adapter: postgresql - uri: <%= uri = URI.parse(ENV['POSTGRES_URL'] ? ENV['POSTGRES_URL'] : "pg://localhost") %> - socket: <%= ENV['POSTGRES_SOCKET'] %> - host: <%= ENV['POSTGRES_SOCKET'].present? ? nil : uri.host %> - port: <%= uri.port || (ENV['POSTGRES_SOCKET'].present? ? nil : 5432) %> - database: <%= ENV['POSTGRES_DB'] || "discourse" %> - username: <%= uri.user %> - password: <%= uri.password %> + socket: <%= GlobalSetting.db_socket %> + host: <%= GlobalSetting.db_host %> + port: <%= GlobalSetting.db_port %> + database: <%= GlobalSetting.db_name %> + username: <%= GlobalSetting.db_username %> + password: <%= GlobalSetting.db_password %> 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 %> diff --git a/config/environments/production.rb b/config/environments/production.rb index 4c47e32d7da..5da07598f92 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -32,25 +32,15 @@ Discourse::Application.configure do # the I18n.default_locale when a translation can not be found) config.i18n.fallbacks = true - # specify your smtp url using the SMTP_URL env var eg: - # SMTP_URL=smtp://user:password@myhost.com - if ENV.key?('SMTP_URL') - config.action_mailer.smtp_settings = begin - uri = URI.parse(ENV['SMTP_URL']) - params = { - :address => uri.host, - :port => uri.port || 25, - :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 + if GlobalSetting.smtp_address + config.action_mailer.smtp_settings = { + address: GlobalSetting.smtp_address, + port: GlobalSetting.smtp_port, + domain: GlobalSetting.smtp_domain, + user_name: GlobalSetting.smtp_password, + authentication: 'plain', + enable_starttls_auto: GlobalSetting.smtp_enable_start_tls + } else config.action_mailer.delivery_method = :sendmail config.action_mailer.sendmail_settings = {arguments: '-i'} @@ -63,16 +53,16 @@ Discourse::Application.configure do config.handlebars.precompile = true # 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. # 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 # developers have god like rights and may impersonate anyone in the system # normal admins may only impersonate other moderators (not admins) - if emails = ENV["DEVELOPER_EMAILS"] + if emails = GlobalSetting.developer_emails config.developer_emails = emails.split(",") end diff --git a/config/redis.yml b/config/redis.yml index 796589989f4..9422ecd1627 100644 --- a/config/redis.yml +++ b/config/redis.yml @@ -1,8 +1,7 @@ defaults: &defaults - uri: <%= uri = URI.parse(ENV['REDIS_PROVIDER_URL'] || "redis://localhost:6379") %> - host: <%= uri.host %> - port: <%= uri.port %> - password: <%= uri.password %> + host: <%= GlobalSetting.redis_host %> + port: <%= GlobalSetting.redis_port %> + password: <%= GlobalSetting.redis_password %> db: 0 cache_db: 2 @@ -20,4 +19,4 @@ staging: <<: *defaults production: - <<: *defaults \ No newline at end of file + <<: *defaults