2013-12-20 12:12:23 +08:00
|
|
|
class GlobalSetting
|
|
|
|
|
2014-01-02 07:46:09 +08:00
|
|
|
def self.register(key, default)
|
|
|
|
define_singleton_method(key) do
|
|
|
|
provider.lookup(key, default)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-20 13:38:51 +08:00
|
|
|
def self.load_defaults
|
2013-12-20 14:41:12 +08:00
|
|
|
default_provider = FileProvider.from(File.expand_path('../../../config/discourse_defaults.conf', __FILE__))
|
2014-01-02 07:46:09 +08: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-20 12:12:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-14 13:59:55 +08:00
|
|
|
def self.database_config
|
|
|
|
hash = {"adapter" => "postgresql"}
|
|
|
|
%w{pool timeout socket host port username password}.each do |s|
|
|
|
|
if val = self.send("db_#{s}")
|
|
|
|
hash[s] = val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
hash["host_names"] = [ hostname ]
|
|
|
|
hash["database"] = db_name
|
|
|
|
|
2015-02-18 08:16:53 +08:00
|
|
|
hash["prepared_statements"] = !!self.db_prepared_statements
|
|
|
|
|
2014-01-14 13:59:55 +08:00
|
|
|
{"production" => hash}
|
|
|
|
end
|
|
|
|
|
2013-12-20 12:12:23 +08:00
|
|
|
|
|
|
|
class BaseProvider
|
2013-12-20 13:17:21 +08: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-20 12:12:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-20 13:17:21 +08:00
|
|
|
class FileProvider < BaseProvider
|
2013-12-20 13:38:51 +08:00
|
|
|
attr_reader :data
|
2013-12-20 13:17:21 +08:00
|
|
|
def self.from(file)
|
|
|
|
if File.exists?(file)
|
|
|
|
parse(file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(file)
|
|
|
|
@file = file
|
|
|
|
@data = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def read
|
2014-02-15 05:35:30 +08:00
|
|
|
ERB.new(File.read(@file)).result().split("\n").each do |line|
|
2013-12-20 13:17:21 +08:00
|
|
|
if line =~ /([a-z_]+)\s*=\s*(\"([^\"]*)\"|\'([^\']*)\'|[^#]*)/
|
|
|
|
@data[$1.strip.to_sym] = ($4 || $3 || $2).strip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def lookup(key,default)
|
2013-12-20 14:09:51 +08:00
|
|
|
var = @data[key]
|
|
|
|
resolve(var, var.nil? ? default : "")
|
2013-12-20 13:17:21 +08:00
|
|
|
end
|
|
|
|
|
2014-01-02 07:46:09 +08:00
|
|
|
def keys
|
|
|
|
@data.keys
|
|
|
|
end
|
|
|
|
|
2013-12-20 13:17:21 +08:00
|
|
|
def self.parse(file)
|
|
|
|
provider = self.new(file)
|
|
|
|
provider.read
|
|
|
|
provider
|
2013-12-20 12:12:23 +08:00
|
|
|
end
|
2014-10-04 11:07:20 +08:00
|
|
|
|
|
|
|
private_class_method :parse
|
2013-12-20 12:12:23 +08:00
|
|
|
end
|
|
|
|
|
2013-12-20 13:17:21 +08:00
|
|
|
class EnvProvider < BaseProvider
|
|
|
|
def lookup(key, default)
|
2013-12-20 14:07:08 +08:00
|
|
|
var = ENV["DISCOURSE_" << key.to_s.upcase]
|
|
|
|
resolve(var , var.nil? ? default : nil)
|
2013-12-20 13:17:21 +08:00
|
|
|
end
|
2014-01-02 07:46:09 +08:00
|
|
|
|
|
|
|
def keys
|
|
|
|
ENV.keys.select{|k| k =~ /^DISCOURSE_/}.map{|k| k[10..-1].downcase.to_sym}
|
|
|
|
end
|
2013-12-20 12:12:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class << self
|
|
|
|
attr_accessor :provider
|
|
|
|
end
|
|
|
|
|
2013-12-20 13:38:51 +08:00
|
|
|
|
2013-12-20 12:12:23 +08:00
|
|
|
@provider =
|
2013-12-20 13:17:21 +08:00
|
|
|
FileProvider.from(File.expand_path('../../../config/discourse.conf', __FILE__)) ||
|
2013-12-20 12:12:23 +08:00
|
|
|
EnvProvider.new
|
2014-01-02 07:46:09 +08:00
|
|
|
|
|
|
|
load_defaults
|
2013-12-20 12:12:23 +08:00
|
|
|
end
|