Rails 3.1:如何仅为 Web 应用程序运行初始化程序(rails 服务器/unicorn/等)
我的网络应用程序需要加密其会话数据。我的设置是:
config/initializers/encryptor.rb:
require 'openssl'
require 'myapp/encryptor'
MyApp::Encryptor.config[ :random_key ] = OpenSSL::Random.random_bytes( 128 )
Session.delete_all
app/models/session.rb:
require 'attr_encrypted'
class Session < ActiveRecord::Base
attr_accessible :session_id, :data
attr_encryptor :data, :key => proc { MyApp::Encryptor.config[ :random_key ] }, :marshal => true
# Rest of model stuff
end
一切都很好,并且可以保证会话数据的安全。问题是:当我运行自定义 rake 任务时,它会加载初始化程序并清除所有会话。不好!
我可以在初始化程序中放入什么来确保它只为 web 应用程序初始化运行?或者,我可以在初始化程序中放入什么以使其不运行 rake 任务?
更新:好的,我现在所做的是添加MYAPP_IN_RAKE = true,除非定义? MYAPP_IN_RAKE
到我的 .rake 文件。然后在我的初始化程序中我这样做:
unless defined?( MYAPP_IN_RAKE ) && MYAPP_IN_RAKE
# Web only initialization
end
似乎有效。但我愿意接受其他建议。
My webapp needs to encrypt its session data. What I setup is:
config/initializers/encryptor.rb:
require 'openssl'
require 'myapp/encryptor'
MyApp::Encryptor.config[ :random_key ] = OpenSSL::Random.random_bytes( 128 )
Session.delete_all
app/models/session.rb:
require 'attr_encrypted'
class Session < ActiveRecord::Base
attr_accessible :session_id, :data
attr_encryptor :data, :key => proc { MyApp::Encryptor.config[ :random_key ] }, :marshal => true
# Rest of model stuff
end
That all works great, and keeps the session data secured. Here's the problem: when I run my custom rake tasks it loads the initializer and clears all the sessions. Not good!
What can I put in my initializer to make sure it ONLY runs for the webapp initialization? Or, what can I put in my initializer to make it NOT run for rake tasks?
Update: OK, what I've done for the moment is add MYAPP_IN_RAKE = true unless defined? MYAPP_IN_RAKE
to my .rake file. And then in my initializer I do:
unless defined?( MYAPP_IN_RAKE ) && MYAPP_IN_RAKE
# Web only initialization
end
Seems to work. But I'm open to other suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以像这样在`config/application.rb'中对您的应用程序进行修改:
然后在您的Rakefile中添加以下内容:
使用方法而不是常量是很好的,因为有时您更喜欢稍后更改或重新定义它们。另外,它们不会污染根命名空间。
下面是一个
config/initializers/rake_environment_test.rb
脚本示例:Rakefile
的可编程特性为您提供了显着的灵活性。You might make a modification to your application in `config/application.rb' like this:
Then in your
Rakefile
you'd add this:It's nice to use methods rather than constants since sometimes you'd prefer to change or redefine them later. Plus, they don't pollute the root namespace.
Here's a sample
config/initializers/rake_environment_test.rb
script:The programmable nature of the
Rakefile
affords you significant flexibility.还有另一种解决方法:
当您使用 rake 启动时,您的 ENV["RAILS_ENV"] 将为零。 “test”的测试是为了避免在使用 rspec 时运行。
我知道应该使用 Rails.env,但在未初始化时它会返回“development”。
http://apidock.com/rails/Rails/env/class
There is another work around:
When you launch with rake your ENV["RAILS_ENV"] will be nil. The test for 'test' is to avoid to run when using rspec.
I know that is reckon to use Rails.env but it return "development" when it is not initialised.
http://apidock.com/rails/Rails/env/class