当前位置: 动力学知识库 > 问答 > 编程问答 >

rack - How to determine if Rails is running from CLI, console or as server?

问题描述:

I have a middleware for announcing my application on the local network app using Bonjour, but it's also announcing the service when Rails is invoked from rake or through the console.

I'd like to exclude these cases, and only use the Bonjour middleware when Rails is running as a server.

The middleware configuration accepts a proc to exclude middlewares under certain conditions using a proc:

config.middleware.insert_before ActionDispatch::Static, Rack::SSL, :exclude => proc { |env|

env['HTTPS'] != 'on'

}

But how do I determine if Rails was invoked from the CLI, console or as a server?

网友答案:

Peeking at the Rails module using pry reveals that console invocations can be detected like this:

Rails.const_defined? 'Console'

And server invocations like this:

Rails.const_defined? 'Server'
网友答案:

Super helpful. Thanks @crishoj.

I wanted to examine the Console object more closely for another problem I am working on and found out that the Console constant can be reached with Rails::Console, so another option for checking would be to use:

defined? Rails::Console
defined? Rails::Server
分享给朋友:
您可能感兴趣的文章:
随机阅读: