5

I've created the application on Sinatra, which represents a simple API. I want to make deployment on production and development. I want to choose during deployment, whether it should be dev or production, and the logic of some methods should change, depending on deployment type. Is there any idea, how it can be done and some example of solving this issue.

Example: I have code

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

but after deployment to production I would like see after run /api/test

It is PROD

How it can be done?

Taras Kovalenko
  • 2,323
  • 3
  • 24
  • 49
  • 1
    possible duplicate of [How do I tell Sinatra what environment (development, test, production) it is?](http://stackoverflow.com/questions/19420321/how-do-i-tell-sinatra-what-environment-development-test-production-it-is) – Amaury Medeiros Apr 28 '15 at 16:27
  • @AmauryMedeiros I updated my question, I don't understand how to work it. Please explain me. – Taras Kovalenko Apr 28 '15 at 17:12
  • The nominated duplicate is pretty useless, and not _quite_ a duplicate. – Wayne Conrad Apr 28 '15 at 17:34

2 Answers2

5

According to 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

You also can use the development? and production? methods to change the logic:

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

If settings.development? doesn't work, you can try Sinatra::Application.environment == :development

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Amaury Medeiros
  • 2,093
  • 4
  • 26
  • 42
3

Try this

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

Official guide -> environments

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Renaud
  • 110
  • 1
  • 7
  • i think `ruby myapp.rb -E production` or [like this](http://stackoverflow.com/questions/5832060/sinatra-configuring-environments-on-the-fly#5834009) – Renaud Apr 28 '15 at 17:40