Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Sinatra configured to work on production and development

Tags:

ruby

sinatra

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?

like image 761
Taras Kovalenko Avatar asked Apr 28 '15 16:04

Taras Kovalenko


People also ask

What does Sinatra Do Ruby?

Sinatra is a Domain Specific Language implemented in Ruby that's used for writing web applications. Created by Blake Mizerany, Sinatra is Rack-based, which means it can fit into any Rack-based application stack, including Rails. It's used by companies such as Apple, BBC, GitHub, LinkedIn, and more.

When should I take Sinatra?

Choose Sinatra for projects with no more than 10 actions. Being lightweight and having fewer dependencies, Sinatra is known to have a small loading time.


2 Answers

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

like image 199
Amaury Medeiros Avatar answered Nov 08 '22 00:11

Amaury Medeiros


Try this

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

Official guide -> environments

like image 33
Renaud Avatar answered Nov 07 '22 23:11

Renaud