Table of contents
1.
Introduction
2.
Create an Elastic Beanstalk environment. 
2.1.
To start a new environment (console)
3.
Deploy your application. 
3.1.
To install a source bundle 
4.
Environments 
4.1.
Not able to Found
4.2.
Error 
5.
Frequently Asked Questions
5.1.
Why should you always use Sinatra over rails? 
5.2.
What exactly is Rack middleware? 
5.3.
What exactly is config ru? 
5.4.
Describe Ruby rack.
5.5.
Rails with Sinatra: what is it?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Environments in Sinatra

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

This article covers how to set up your Ruby development environment for a Sinatra project, especially Environments in Sinatra. Ngrok and the Twilio Ruby SDK, two valuable tools we suggest for all Ruby apps that use Twilio, will also be covered.
 

Introduction

Sinatra is powerful enough to create a working web application from a single file. Sinatra is recognized as a good way for new developers to get started in Ruby web application development and can aid in learning for larger frameworks such as Rails.

Create an Elastic Beanstalk environment. 

Elastic load Balancer

Create an Elastic Beanstalk environment using the Elastic Beanstalk console. Select the Ruby platform and accept the default settings and sample code.

To start a new environment (console)

  • Navigate to the Elastic Beanstalk console. 
     
  • Select the platform and platform branch that correspond to the language used by your application for Platform. 
     
  • Select Sample application for Application code. 
     
  • Select Review and launch. 
     
  • Examine the alternatives. Choose the available option you want to use, and then click Create app.
     

It takes about a few minutes to create an environment, which includes the following resources: 

EC2 instance - A virtual machine in Amazon Elastic Compute Cloud (Amazon EC2) configured to run web apps on the platform of your choice. 

Each platform employs software, configuration files, and scripts to support a particular language version, framework, web container, or combination. Most media use Apache or NGINX as a reverse proxy to sit in front of your web app, forwarding requests to it, serving static assets, and generating access and error logs. 

Amazon EC2

Instance security group - An Amazon EC2 security group set up to allow inbound traffic on port 80. This resource provides HTTP traffic from the load balancer to reach the EC2 instance hosting your web application. Other ports are generally not open to traffic. 

Security Group

Load balancer - A load balancer configured with Elastic Load Balancing to distribute requests to your application’s instances. A load balancer also removes the need to expose your instances to the internet directly. 

Load balancer

A load balancer security group- is an Amazon EC2 security group that accepts inbound traffic on port 80. This resource allows internet HTTP traffic to reach the load balancer. Other ports are generally not open to traffic. 

An Auto Scaling group is set up to replace an instance if it is terminated or becomes unavailable. 

Amazon S3 bucket - A location to store your source code, logs, and other artifacts generated by Elastic Beanstalk. 

aws

Amazon CloudWatch alarms - Two CloudWatch alarms monitor the load on your environment's instances and are triggered if it is too high or too low. When an alarm is triggered, your Auto Scaling group responds by scaling up or down. 

Cloud Watch alarm

Elastic Beanstalk uses AWS CloudFormation - to launch resources in your environment and to propagate configuration changes. The resources are specified in a template, which can be viewed in the AWS CloudFormation console. 

Domain name - A domain name in the form subdomain.region.elasticbeanstalk.com that routes to your web app.

Domain Names

Deploy your application. 

Make a source bundle with your source files in it. The command below generates a source bundle called Sinatra-default.zip.

~/eb-sinatra$ zip ../Sinatra-default.zip -r * .[^.]*

 

To deploy Sinatra in your environment, upload the source bundle to Elastic Beanstalk. 

To install a source bundle 

  • Select your Amazon Region from the Regions list in the Elastic Beanstalk console. 
  • Choose Environments from the navigation pane, and then select the name of your environment from the list. 

Note: If you have a lot of environments, use the search bar to narrow down the list. 

  • Select Upload and deploy from the environment overview page. 
  • Upload the source bundle using the on-screen dialogue box. 
  • Select Deploy. 
  • When the deployment is finished, you can open your website in a new tab by selecting the site URL.

Environments 

There are three environments available: "development," "production," and "test." The APP ENV environment variable can be used to configure environments. The default setting is "development." All templates are reloaded between requests in the "development" environment, and special not found and error handlers display stack traces in your browser. Templates are cached by default in the "production" and "test" environments. 

Set the APP ENV environment variable to run different environments: 

APP_ENV=production ruby my_app.rb

To check the current environment setting, you can use predefined methods: development? test?, and production?

get '/' do
  if settings.development?
    "development!"
  else
    "not development!"
  end
end


Error handlers run in the same context as routes and before filters, so you get all the goodies it has to offer, such as haml, erb, halt, and so on.

Not able to Found

When a Sinatra::NotFound exception is thrown, or the status code of the response is 404, the not found handler is called: 

not_found do
  'This is nowhere to be found.'
end

Error 

When a route block or a filter raises an exception, the error handler is called. However, in development, it will only run if the show exceptions option is set to: after handler:

set :show_exceptions, :after_handler.


The exception object is accessible via the Sinatra.error Rack variable:

error do 
'Error Found -'+ env['sinatra.error']. 

message
end

 

Errors in customization: 

error MyCustomError do
  'Kind of ' + env['sinatra.error'] . message
end


Then, if this occurs: 

get '/' do
  raise MyCustomError, 'bad'
end

 

You will receive the following: 

Kind of bad


You can also install an error handler for a status code: 

error 403 do
  'Access forbidden'
end

get '/secret' do
  403
end

 

Range of:

error 400..510 do
  'Boom'
end

Frequently Asked Questions

Why should you always use Sinatra over rails? 

Sinatra is much lighter, requires fewer resources, and does fewer things out of the box. Rails, on the other hand, is jam-packed with features, comes with a tonne of code, and makes it very simple to build complex web applications in a short period of time if you know how to use it.

What exactly is Rack middleware? 

Rack middleware is a method of filtering requests and responses that come into your application. A middleware component sits between the client and the server, processing inbound, and outbound requests and responses. Still, it is more than just an interface that can communicate with the web server.

What exactly is config ru? 

Rack configuration file config.ru ( ru stands for "rackup"). Rack is a simple interface that connects web servers that support Ruby and Ruby frameworks. It's similar to a Ruby implementation of a CGI, which provides a standard protocol for web servers to use when running programs.

Describe Ruby rack.

Ruby programmers created Rack, a modular interface between web servers and web applications. Application programming interfaces (APIs) for middleware and web frameworks are encapsulated in a single method call for HTTP requests and responses using Rack.

Ruby Webserver Interface called Rack.

Rails with Sinatra: what is it?

Sinatra is a server-side HTTP library, whereas Rails is a framework for developing model-driven web applications. Sinatra is the best tool if you think in terms of HTTP requests and responses. Rails are the way to go if you require complete integration and as little boilerplate as possible.

Conclusion

This article has gone through the Environment in Sinatra. Understand the Elastic Beanstalk environment. Also deployed our application and last understand the concept of Error handling. I suppose it is clear to you. Still, have doubt? Then please comment.

Are you eager to read more articles on Routes in Sinatra? Coding Ninjas cover you, so don't worry. View more topics on Coding ninjas.

Please refer to our guided pathways on Code studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.
 

If you find any problems, please comment. We will love to answer your questions.

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Ninja, have a great time learning.

thank you

Live masterclass