globals now implemented and documented

This commit is contained in:
Sam 2013-12-20 16:17:21 +11:00
parent b312b4d563
commit 7b8d2547d0
4 changed files with 152 additions and 11 deletions

View File

@ -36,22 +36,62 @@ class GlobalSetting
)
class BaseProvider
def lookup(name, val)
t = ENV["D_" << name.to_s.upcase]
if t.present?
t
else
val.present? ? val : nil
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
)
end
end
class FileProvider < BaseProvider
def self.from(file)
if File.exists?(file)
parse(file)
end
end
end
class FileProvider
def self.from(location)
def initialize(file)
@file = file
@data = {}
end
def read
File.read(@file).split("\n").each do |line|
if line =~ /([a-z_]+)\s*=\s*(\"([^\"]*)\"|\'([^\']*)\'|[^#]*)/
@data[$1.strip.to_sym] = ($4 || $3 || $2).strip
end
end
end
def lookup(key,default)
resolve(@data[key], default)
end
private
def self.parse(file)
provider = self.new(file)
provider.read
provider
end
end
class EnvProvider
class EnvProvider < BaseProvider
def lookup(key, default)
resolve(ENV["DISCOURSE_" << key.to_s.upcase], default)
end
end
@ -60,6 +100,6 @@ class GlobalSetting
end
@provider =
FileProvider.from(Rails.root + '/config/discourse.conf') ||
FileProvider.from(File.expand_path('../../../config/discourse.conf', __FILE__)) ||
EnvProvider.new
end

View File

@ -5,6 +5,9 @@ require 'redis-store' # HACK
# Plugin related stuff
require_relative '../lib/discourse_plugin_registry'
# Global config
require_relative '../app/models/global_setting'
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(assets: %w(development test profile)))

View File

@ -0,0 +1,76 @@
#
# DO NOT EDIT THIS FILE
# If you need to make changes create a file called discourse.conf in this directory with your changes
# Discourse supports multiple mechanisms for production config.
#
# 1. You can do nothing and get these defaults (not recommended, you should at least set hostname)
# 2. You can copy this file to config/discourse.conf and amend with your settings
# 3. You can pass in config from your environment, all the settings below are available.
Append DISCOURSE_ and upper case the setting in ENV. For example:
to pass in db_timeout of 200 you would use DISCOURSE_DB_TIMEOUT=200
# All settings apply to production only
# connection pool size
db_pool = 5
# database timeout in milliseconds
db_timeout = 5000
# socket file used to access db
db_socket
# host address for db server
db_host = localhost
# port running db server
db_port, 5432],
# database name running discourse
db_name = discourse
# username accessing database
db_username = discourse
# password used to access the db
db_password =
# hostname running the forum
hostname = "www.example.com"
# address of smtp server used to send emails
smtp_address =
# port of smtp server used to send emails
smtp_port = 25
# domain passed to smtp server
smtp_domain =
# username for smtp server
smtp_user_name =
# password for smtp server
smtp_password =
# enable TLS encryption for smtp connections
smtp_enable_start_tls = true
# enable MiniProfiler for administrators
enable_mini_profiler = true
# recommended, cdn used to access assets
cdn_url =
# comma delimited list of emails that have devloper level access
developer_emails =
# redis server address
redis_host = localhost
# redis server port
redis_port = 6379
# redis password
redis_password =

View File

@ -0,0 +1,22 @@
require 'spec_helper'
require 'tempfile'
describe GlobalSetting::FileProvider do
it "can parse a simple file" do
f = Tempfile.new('foo')
f.write(" # this is a comment\n")
f.write("\n")
f.write("a = 1000 # this is a comment\n")
f.write("b = \"10 # = 00\" # this is a # comment\n")
f.write("c = \'10 # = 00\' # this is a # comment\n")
f.close
provider = GlobalSetting::FileProvider.from(f.path)
provider.lookup(:a,"").should == 1000
provider.lookup(:b,"").should == "10 # = 00"
provider.lookup(:c,"").should == "10 # = 00"
f.unlink
end
end