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?
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.
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.
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
Try this
get '/api/test' do
if settings.development?
return "It is dev"
else
return "Not dev"
end
end
Official guide -> environments
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With