Sinatra 动态配置环境

发布于 2024-11-03 18:34:28 字数 1076 浏览 0 评论 0原文

我已经成功编写了一个小型 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

话少心凉 2024-11-10 18:34:28
C:\>type tmp.ru
require 'sinatra'
configure(:production){  p "I'm production" }
configure(:development){ p "I'mma dev mode" }
configure(:sassycustom){ p "I'mma own mode" }
exit!

C:\>rackup tmp.ru
"I'mma dev mode"

C:\>rackup -E development tmp.ru
"I'mma dev mode"

C:\>rackup -E production tmp.ru
"I'm production"

C:\>rackup -E sassycustom tmp.ru
"I'mma own mode"

C:\>rackup -E notdefined tmp.ru

如果不指定环境,则默认使用development。您可以指定任何您想要的环境名称,尽管“生产”很常见。如果您指定了未配置的环境,则没有配置块将匹配。 (这可能是您的错误,但这不是代码捕获的错误。)

请注意,Sinatra 文档说 如果可用,将使用设置 RACK_ENV 环境变量。这曾经不起作用,但在过去几年的某个时候它已被修复!

例如,如果您可以为服务设置环境变量,则可以控制模式。

C:\>type tmp.ru
require 'sinatra'
configure(:production){  p "I'm production" }
configure(:development){ p "I'mma dev mode" }
configure(:sassycustom){ p "I'mma own mode" }
exit!

C:\>rackup tmp.ru
"I'mma dev mode"

C:\>rackup -E development tmp.ru
"I'mma dev mode"

C:\>rackup -E production tmp.ru
"I'm production"

C:\>rackup -E sassycustom tmp.ru
"I'mma own mode"

C:\>rackup -E notdefined tmp.ru

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.

倾听心声的旋律 2024-11-10 18:34:28

您还可以在 config.ru 中获取 ENV['RACK_ENV'] 并使用它以不同方式配置您的应用。在 Heroku 上,它应该默认在生产环境中运行,如果您rackup来启动服务器,那么默认情况下它将处于开发状态。以下是我的一个应用程序中的一些示例代码,这些代码在具有相同配置文件的两个环境中运行:

#\ -p 4567
require 'bundler'               # gem requires
Bundler.require(:default, ENV['RACK_ENV'].to_sym)  # only loads environment specific gems
if ENV['RACK_ENV'] == 'production'           # production config / requires
  require './lib/middleware/exceptionmailer'

  use Rack::ExceptionMailer, 
    :to => ['[email protected]'],
    :from => '[email protected]',
    :subject => 'Error Occurred on Rack Application'

else                            # development or testing only
  use Rack::ShowExceptions
end

这样,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 you rackup 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:

#\ -p 4567
require 'bundler'               # gem requires
Bundler.require(:default, ENV['RACK_ENV'].to_sym)  # only loads environment specific gems
if ENV['RACK_ENV'] == 'production'           # production config / requires
  require './lib/middleware/exceptionmailer'

  use Rack::ExceptionMailer, 
    :to => ['[email protected]'],
    :from => '[email protected]',
    :subject => 'Error Occurred on Rack Application'

else                            # development or testing only
  use Rack::ShowExceptions
end

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.

森林很绿却致人迷途 2024-11-10 18:34:28

查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文