Rails 3:如何检测应用程序是否在服务器模式下运行多个不同的环境?

Ale*_*lex 7 initialization ruby-on-rails heroku

我有一个在多个服务器上运行的应用程序: - 在开发机器上本地 - 在heroku上 - 在Nginx上使用Passanger的特定服务器上

我试图启动一个特定的代码(加载一些REDIS密钥),只有在启动Web服务器时才需要.

我已经做了很多挖掘,我发现最好的解决方案是在初始化程序中执行我的代码:

if defined?(Rails::Server)
   #my code
end
Run Code Online (Sandbox Code Playgroud)

这在本地运行良好,但似乎Rails :: Server永远不会在Heroku或Passanger上定义.

我需要一个适用于所有情况的解决方案,请帮助,这非常重要.

谢谢,

亚历克斯

ps:我正在运行Rails 3.0.4,Ruby 1.8.7

Jor*_*ugh 8

将代码放在config.ru文件中可能是一种更健壮的方法,可以跨不同类型的服务器(Unicorn/Passenger/Rails :: Server /等)检测服务器模式.

例如,在rails-root/config.ru中:

# This file is used by Rack-based servers to start the application.

# ADD this line and read the value later:
ENV['server_mode'] = '1'

require ::File.expand_path...
Run Code Online (Sandbox Code Playgroud)


bon*_*iii 2

关于什么?

config.serve_static_assets = ( defined?(Mongrel) || defined?(WEBrick) ) ? true : false
Run Code Online (Sandbox Code Playgroud)