如何从根目录外部获取配置文件?

时间:2013-02-12 12:46:11

标签: ruby rack

当我从我的app目录中运行rackup时,它运行正常:

walkraft@li234-166:~/discourse$ rackup config.ru
Flushing redis (development mode)
/home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `block in require': iconv will be deprecated in the future, use String#encode instead.
/home/walkraft/discourse/vendor/gems/message_bus/lib/message_bus.rb:130: warning: already initialized constant ENCODE_SITE_TOKEN
>> Thin web server (v1.5.0 codename Knife)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9292, CTRL+C to stop

但是,如果我尝试从此目录外部运行rackup:

walkraft@li234-166:~$ rackup discourse/config.ru
/home/walkraft/discourse/config/application.rb:7:in `require': cannot load such file -- ./lib/discourse_plugin_registry (LoadError)
        from /home/walkraft/discourse/config/application.rb:7:in `<top (required)>'
        from /home/walkraft/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
        from /home/walkraft/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
        from /home/walkraft/discourse/config/environment.rb:2:in `<top (required)>'
        from /home/walkraft/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
        from /home/walkraft/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
        from /home/walkraft/discourse/config.ru:2:in `block in <main>'
        from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/builder.rb:51:in `instance_eval'
        from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/builder.rb:51:in `initialize'
        from /home/walkraft/discourse/config.ru:in `new'
        from /home/walkraft/discourse/config.ru:in `<main>'
        from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/builder.rb:40:in `eval'
        from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/builder.rb:40:in `parse_file'
        from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/server.rb:200:in `app'
        from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/server.rb:304:in `wrapped_app'
        from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/server.rb:254:in `start'
        from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/lib/rack/server.rb:137:in `start'
        from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.4.4/bin/rackup:4:in `<top (required)>'
        from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/bin/rackup:19:in `load'
        from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/bin/rackup:19:in `<main>'
        from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/bin/ruby_noexec_wrapper:14:in `eval'
        from /home/walkraft/.rvm/gems/ruby-1.9.3-p374/bin/ruby_noexec_wrapper:14:in `<main>'

当我不在根目录中时,如何运行rackup?

2 个答案:

答案 0 :(得分:5)

如果您不想手动cd到Discourse根文件夹,那么为什么不在fixup config.ru文件中添加修补程序:

# Insert as first line in config.ru
Dir.chdir(File.dirname(File.expand_path(__FILE__)))

答案 1 :(得分:1)

实际上,rackup不是问题;这是你的代码的问题。

你有

require './lib/discourse_plugin_registry'

某处。这不太理想。它应该是这样的:

require File.expand_path('../../lib/discourse_plugin_registry', __FILE__)

你拥有它的方式,它明确地使用当前目录,无论你对rackup做什么,直到你改变当前目录 - 它都行不通。