Ruby Sinatra 配置用于生产和开发

Ruby Sinatra configured to work on production and development

我在 Sinatra 上创建了应用程序,它代表了一个简单的 API。我想在生产和开发上进行部署。我想在部署时选择,是开发还是生产,一些方法的逻辑应该改变,这取决于部署类型。 有什么想法,如何完成以及解决此问题的一些示例。

示例: 我有代码

get '/api/test' do
  return "It is dev"
end

但是在部署到生产环境之后我想在 运行 /api/test

之后看到
It is PROD

如何实现?

试试这个

get '/api/test' do
  if settings.development?
    return "It is dev"
  else
    return "Not dev"
  end
end

Official guide -> environments

根据Sinatra Documentation

Environments can be set through the RACK_ENV environment variable. The default value is "development". In the "development" environment all templates are reloaded between requests, and special not_found and error handlers display stack traces in your browser. In the "production" and "test" environments, templates are cached by default.

To run different environments, set the RACK_ENV environment variable:

RACK_ENV=production ruby my_app.rb

您也可以使用 development?production? 方法来更改逻辑:

get '/api/test' do
  if settings.development?
    return "It is dev"
  else if settings.production?
    return "It is PROD"
  end
end

如果settings.development?不行,你可以试试Sinatra::Application.environment == :development