Sinatra 动态配置环境 [英] Sinatra configuring environments on the fly

查看:19
本文介绍了Sinatra 动态配置环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地编写了一个小的 Sinatra 应用程序并且已经成功地将它部署到了 heroku.

I have successfull written a little Sinatra application and already successfully deployed it on heroku.

但是,我想在本地计算机上以开发模式运行该应用程序,并且希望在将其推送到远程存储库后在 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.

目前我可以实现这些选项中的任何一个.当我将 config.ru 更改为以下值时:

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

我可以通过 ruby config.ru 在本地(在端口 4567 上)运行它.当我将 config.ru 更改为:

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

我可以让它在 Heroku(端口 80)上运行.

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.

我想要类似的东西:

ruby config.ru dev 用于开发,ruby config.ru 用于生产.

ruby config.ru dev for development and ruby config.ru for production.

附加信息:

当我尝试在本地机器上运行生产 config.ru 时,我得到:

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

推荐答案

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.您可以指定任何您想要的环境名称,但生产"非常常见.如果您指定未配置的环境,则不会匹配任何配置块.(这可能是您的错误,但这不是代码捕获的错误.)

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.)

请注意,Sinatra 文档说设置 RACK_ENV 环境变量(如果可用)将被使用.这曾经不起作用,但在过去几年中它已修复!

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.

这篇关于Sinatra 动态配置环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆