2013-12-19 23:12:23 -05:00
|
|
|
class GlobalSetting
|
|
|
|
|
2014-01-01 18:46:09 -05:00
|
|
|
def self.register(key, default)
|
|
|
|
define_singleton_method(key) do
|
|
|
|
provider.lookup(key, default)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-20 00:38:51 -05:00
|
|
|
def self.load_defaults
|
2013-12-20 01:41:12 -05:00
|
|
|
default_provider = FileProvider.from(File.expand_path('../../../config/discourse_defaults.conf', __FILE__))
|
2014-01-01 18:46:09 -05:00
|
|
|
default_provider.keys.concat(@provider.keys).uniq.each do |key|
|
|
|
|
default = default_provider.lookup(key, nil)
|
|
|
|
define_singleton_method(key) do
|
|
|
|
provider.lookup(key, default)
|
2013-12-19 23:12:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-14 00:59:55 -05:00
|
|
|
def self.database_config
|
|
|
|
hash = {"adapter" => "postgresql"}
|
2016-01-25 01:27:59 -05:00
|
|
|
%w{pool timeout socket host port username password replica_host replica_port}.each do |s|
|
2014-01-14 00:59:55 -05:00
|
|
|
if val = self.send("db_#{s}")
|
|
|
|
hash[s] = val
|
|
|
|
end
|
|
|
|
end
|
2016-01-25 01:27:59 -05:00
|
|
|
|
|
|
|
hash["adapter"] = "postgresql_fallback" if hash["replica_host"]
|
|
|
|
|
2015-07-23 01:22:54 -04:00
|
|
|
hostnames = [ hostname ]
|
2015-07-23 01:33:38 -04:00
|
|
|
hostnames << backup_hostname if backup_hostname.present?
|
2015-07-23 01:22:54 -04:00
|
|
|
|
|
|
|
hash["host_names"] = hostnames
|
2014-01-14 00:59:55 -05:00
|
|
|
hash["database"] = db_name
|
|
|
|
|
2015-02-17 19:16:53 -05:00
|
|
|
hash["prepared_statements"] = !!self.db_prepared_statements
|
|
|
|
|
2014-01-14 00:59:55 -05:00
|
|
|
{"production" => hash}
|
|
|
|
end
|
|
|
|
|
2015-06-25 02:51:48 -04:00
|
|
|
def self.redis_config
|
|
|
|
@config ||=
|
|
|
|
begin
|
|
|
|
c = {}
|
|
|
|
c[:host] = redis_host if redis_host
|
|
|
|
c[:port] = redis_port if redis_port
|
2016-03-02 09:01:48 -05:00
|
|
|
c[:slave_host] = redis_slave_host if redis_slave_host
|
|
|
|
c[:slave_port] = redis_slave_port if redis_slave_port
|
2015-06-30 22:19:02 -04:00
|
|
|
c[:password] = redis_password if redis_password.present?
|
2015-06-25 02:51:48 -04:00
|
|
|
c[:db] = redis_db if redis_db != 0
|
|
|
|
c[:db] = 1 if Rails.env == "test"
|
|
|
|
if redis_sentinels.present?
|
|
|
|
c[:sentinels] = redis_sentinels.split(",").map do |address|
|
|
|
|
host,port = address.split(":")
|
|
|
|
{host: host, port: port}
|
|
|
|
end.to_a
|
|
|
|
end
|
2016-03-02 09:01:48 -05:00
|
|
|
c[:connector] = DiscourseRedis::Connector
|
2015-06-25 02:51:48 -04:00
|
|
|
c.freeze
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-19 23:12:23 -05:00
|
|
|
|
|
|
|
class BaseProvider
|
2013-12-20 00:17:21 -05:00
|
|
|
def self.coerce(setting)
|
|
|
|
return setting == "true" if setting == "true" || setting == "false"
|
|
|
|
return $1.to_i if setting.to_s.strip =~ /^([0-9]+)$/
|
|
|
|
setting
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def resolve(current, default)
|
|
|
|
BaseProvider.coerce(
|
|
|
|
if current.present?
|
|
|
|
current
|
|
|
|
else
|
|
|
|
default.present? ? default : nil
|
|
|
|
end
|
|
|
|
)
|
2013-12-19 23:12:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-20 00:17:21 -05:00
|
|
|
class FileProvider < BaseProvider
|
2013-12-20 00:38:51 -05:00
|
|
|
attr_reader :data
|
2013-12-20 00:17:21 -05:00
|
|
|
def self.from(file)
|
|
|
|
if File.exists?(file)
|
|
|
|
parse(file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(file)
|
|
|
|
@file = file
|
|
|
|
@data = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def read
|
2014-02-14 16:35:30 -05:00
|
|
|
ERB.new(File.read(@file)).result().split("\n").each do |line|
|
2015-08-14 03:01:06 -04:00
|
|
|
if line =~ /^\s*([a-z_]+[a-z0-9_]*)\s*=\s*(\"([^\"]*)\"|\'([^\']*)\'|[^#]*)/
|
2013-12-20 00:17:21 -05:00
|
|
|
@data[$1.strip.to_sym] = ($4 || $3 || $2).strip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def lookup(key,default)
|
2013-12-20 01:09:51 -05:00
|
|
|
var = @data[key]
|
|
|
|
resolve(var, var.nil? ? default : "")
|
2013-12-20 00:17:21 -05:00
|
|
|
end
|
|
|
|
|
2014-01-01 18:46:09 -05:00
|
|
|
def keys
|
|
|
|
@data.keys
|
|
|
|
end
|
|
|
|
|
2013-12-20 00:17:21 -05:00
|
|
|
def self.parse(file)
|
|
|
|
provider = self.new(file)
|
|
|
|
provider.read
|
|
|
|
provider
|
2013-12-19 23:12:23 -05:00
|
|
|
end
|
2014-10-03 23:07:20 -04:00
|
|
|
|
|
|
|
private_class_method :parse
|
2013-12-19 23:12:23 -05:00
|
|
|
end
|
|
|
|
|
2013-12-20 00:17:21 -05:00
|
|
|
class EnvProvider < BaseProvider
|
|
|
|
def lookup(key, default)
|
2013-12-20 01:07:08 -05:00
|
|
|
var = ENV["DISCOURSE_" << key.to_s.upcase]
|
|
|
|
resolve(var , var.nil? ? default : nil)
|
2013-12-20 00:17:21 -05:00
|
|
|
end
|
2014-01-01 18:46:09 -05:00
|
|
|
|
|
|
|
def keys
|
|
|
|
ENV.keys.select{|k| k =~ /^DISCOURSE_/}.map{|k| k[10..-1].downcase.to_sym}
|
|
|
|
end
|
2013-12-19 23:12:23 -05:00
|
|
|
end
|
|
|
|
|
2015-03-08 21:21:41 -04:00
|
|
|
class BlankProvider < BaseProvider
|
|
|
|
def lookup(key, default)
|
|
|
|
default
|
|
|
|
end
|
|
|
|
|
|
|
|
def keys
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-19 23:12:23 -05:00
|
|
|
|
|
|
|
class << self
|
|
|
|
attr_accessor :provider
|
|
|
|
end
|
|
|
|
|
2013-12-20 00:38:51 -05:00
|
|
|
|
2015-03-08 21:21:41 -04:00
|
|
|
if Rails.env == "test"
|
|
|
|
@provider = BlankProvider.new
|
|
|
|
else
|
|
|
|
@provider =
|
|
|
|
FileProvider.from(File.expand_path('../../../config/discourse.conf', __FILE__)) ||
|
|
|
|
EnvProvider.new
|
|
|
|
end
|
2014-01-01 18:46:09 -05:00
|
|
|
|
|
|
|
load_defaults
|
2013-12-19 23:12:23 -05:00
|
|
|
end
|