Sinatra 动态配置环境
我已经成功编写了一个小型 Sinatra 应用程序,并已成功将其部署在 heroku 上。
但是,我想在本地计算机上以开发模式运行该应用程序,并且在将其推送到远程存储库后,我希望在 Heroku 上将其设置为生产模式。
目前我可以实现其中任何一个选项。当我将 config.ru 更改为以下值时:
require 'rubygems'
require 'sinatra'
require 'sinatra/reloader'
require "./calc.rb"
enable :logging
set :environment, :development
set :port, 4567
我可以通过 ruby config.ru
在本地运行它(在端口 4567 上)。当我将 config.ru 更改为:
require 'rubygems'
require 'sinatra'
require 'sinatra/reloader'
require "./calc.rb"
enable :logging
set :environment, :production
set :port, 4567
run Sinatra::Application
我能够让它在 Heroku 上运行(在端口 80 上)。
但我不能在开发和生产中使用相同的配置。
我想要这样的东西:
用于开发的 ruby config.ru dev
和用于生产的 ruby config.ru
。
附加信息:
当我尝试在本地计算机上运行生产 config.ru 时,我得到:
$ ruby config.ru
(eval):2:in `method_missing': undefined method `run' for main:Object (NoMethodError)
from (eval):4:in `__send__'
from (eval):4:in `method_missing'
from config.ru:10
I have successfull written a little Sinatra application and already successfully deployed it on heroku.
However I want to run that application in development mode on my local computer and I want to have it production mode on heroku once I push it to the remote repository.
Currently I can achieve either one of thos options. When I change my config.ru to the following values:
require 'rubygems'
require 'sinatra'
require 'sinatra/reloader'
require "./calc.rb"
enable :logging
set :environment, :development
set :port, 4567
I'm able to run it locally (on port 4567) via ruby config.ru
. When I change the config.ru to this:
require 'rubygems'
require 'sinatra'
require 'sinatra/reloader'
require "./calc.rb"
enable :logging
set :environment, :production
set :port, 4567
run Sinatra::Application
I'm able to get it to run on Heroku (on port 80).
But I can not use the same configuration for both development and production.
I would like to have something like:
ruby config.ru dev
for development and ruby config.ru
for production.
Additional information:
When I try to run the production config.ru on my local machine I get:
$ ruby config.ru
(eval):2:in `method_missing': undefined method `run' for main:Object (NoMethodError)
from (eval):4:in `__send__'
from (eval):4:in `method_missing'
from config.ru:10
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果不指定环境,则默认使用
development
。您可以指定任何您想要的环境名称,尽管“生产”很常见。如果您指定了未配置的环境,则没有配置块将匹配。 (这可能是您的错误,但这不是代码捕获的错误。)请注意,Sinatra 文档说 如果可用,将使用设置 RACK_ENV 环境变量。这曾经不起作用,但在过去几年的某个时候它已被修复!
例如,如果您可以为服务设置环境变量,则可以控制模式。
If you don't specify an environment,
development
is used by default. You can specify any environment name you want, though 'production' is very common. If you specify an environment that you don't configure, no configuration block will match. (It might be a mistake on your part, but it's not an error caught by the code.)Note that the Sinatra documentation says that setting RACK_ENV environment variable will be used if available. This used to not work, but some time in the last few years it has been fixed!
If, for example, you can set an environment variable for your service, you can control the mode.
您还可以在 config.ru 中获取
ENV['RACK_ENV']
并使用它以不同方式配置您的应用。在 Heroku 上,它应该默认在生产环境中运行,如果您rackup
来启动服务器,那么默认情况下它将处于开发状态。以下是我的一个应用程序中的一些示例代码,这些代码在具有相同配置文件的两个环境中运行:这样,Thin 或 Passenger 或其他任何内容都会选择它,并且正确的模块将加载到生产中,但您可以进行其他开发配置。
You can also grab
ENV['RACK_ENV']
in your config.ru and use that configure your app differently. On Heroku it should run in production by default and if yourackup
to fire up your server it will be development by default. Here's some sample code from one of my apps that runs in both environments with the same config file:This way, Thin or Passenger or whatever will pick it up and the right modules will get loaded in production but you can do other configuration for development.
查看 Heroku 文档:
http://devcenter.heroku.com/articles/rack#frameworks
这基本上就是我的应用程序所使用的,当我在本地启动它时,它在端口 4567 上运行。
Look at the Heroku documentation:
http://devcenter.heroku.com/articles/rack#frameworks
That's basically what I use for my app, when I start it locally it runs on port 4567.