Read configuration from config.defaults.yml and config.yml.
[arvados.git] / services / api / config / initializers / load_config.rb
1 conf = {}
2 %w(config.defaults config).each do |cfgfile|
3   path = "#{::Rails.root.to_s}/config/#{cfgfile}.yml"
4   if File.exists? path
5     yaml = ERB.new(IO.read path).result(binding)
6     confs = YAML.load(yaml)
7     conf.merge!(confs['common'] || {})
8     conf.merge!(confs[::Rails.env.to_s] || {})
9   end
10 end
11
12 Server::Application.configure do
13   nils = []
14   conf.each do |k, v|
15     # "foo.bar: baz" --> { config.foo.bar = baz }
16     cfg = config
17     ks = k.split '.'
18     k = ks.pop
19     ks.each do |kk|
20       cfg = cfg.send(kk)
21     end
22     if cfg.respond_to?(k.to_sym) and !cfg.send(k).nil?
23       # Config must have been set already in environments/*.rb.
24       #
25       # After config files have been migrated, this mechanism should
26       # be deprecated, then removed.
27     elsif v.nil?
28       # Config variables are not allowed to be nil. Make a "naughty"
29       # list, and present it below.
30       nils << k
31     else
32       cfg.send "#{k}=", v
33     end
34   end
35   if !nils.empty?
36     raise <<EOS
37 Refusing to start in #{::Rails.env.to_s} mode with missing configuration.
38
39 The following configuration settings must be specified in config/config.yml:
40 * #{nils.join "\n* "}
41
42 EOS
43   end
44 end