Table of contents
1.
Introduction
2.
CONFIGURING SETTINGS
3.
The setting, enabling and disabling 
3.1.
Deferring evaluation
3.2.
Configuring multiple settings
3.3.
Using the enable and disable buttons to set multiple boolean values 
4.
Configuration  
5.
Configuring attack protection
6.
Frequently Asked Questions
6.1.
What exactly is Sinatra Framework?  
6.2.
How would you defend your business from a denial of service assault? 
6.3.
What flaw can result in a denial of service attack? 
6.4.
Describe Ruby rack.
6.5.
Rails with Sinatra: what is it?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Configuration in Sinatra

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

Introduction

Sinatra is a Ruby-based domain-specific language and free and open source web application library. It serves as an alternative to Ruby on Rails, Merb, Nitro, and Camping, as well as other Ruby web application frameworks. The Rack web server interface is required. Frank Sinatra, a musician, inspired its name.

Introduction

Blake Mizerany created and developed Sinatra, which is compact and adaptable. The standard model-view-controller pattern utilized by other frameworks, such as Ruby on Rails, is not followed by this one. Sinatra, on the other hand, concentrates on "quickly and simply building web applications in Ruby."It is also known as a "micro-framework" due to its considerably smaller size when compared to Ruby on Rails.

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.

CONFIGURING SETTINGS

Sinatra provides a variety of built-in options that determine whether or not specific functionalities are activated. Settings are application-level variables that can be changed using one of the set, enable, or disable methods and are accessible within the request context via the settings object. Applications are free to establish additional settings in addition to the framework's basic, built-in parameters.

CONFIGURING SETTINGS

The setting, enabling and disabling 

The set method adds an attribute to the application using the setting name and value in its most basic form. Through the settings object, requests can access settings: 

set : foo, 'bar'

get '/foo' do
  "foo is set to " + settings.

foo
end

Deferring evaluation

Every time a setting value that is a Proc is read, an evaluation is carried out so that other settings may be used to calculate the value: 

set : foo, 'bar'
set:baz, Proc. new { "Hello " + foo }

get '/baz' do
  "baz is set to " + settings.

baz
end

 

If the foo option is left alone, the /baz response should read "baz is set to Hello bar."

Configuring multiple settings

By passing a Hash to a set, several settings can be changed. The previous illustration might be changed to read: 

Set: foo => 'bar', : baz => Proc.new { "HI " + foo }

Using the enable and disable buttons to set multiple boolean values 

Sugar for setting a collection of settings to true or false, respectively, is the enable and disable methods. The two codes shown below are comparable: 

enable and disable buttons to set multiple boolean values
enable  :sessions, :logging
disable :dump_errors, :some_custom_option

 

Using set:

set :sessions, true
set :logging, true
set :some_custom_option, false
set :dump_errors, false

Configuration  

Run just once at startup, anywhere: 

configure do
  # setting one option
  set :option, 'value'

  # setting multiple options
  set :a => 1, :b => 2

  # same as `set :option, true`
  enable :option

  # same as `set :option, false`
  disable :option

  # you can also have dynamic settings with blocks
  set(:css_dir) { File.join(views, 'CSS') }
end

 

Run only when the environment (APP_ENV environment variable) is set to 

configure :production do
  …….
end

 

Run in either the production or test environment: 

configure :production, :test do
……
end

 

These choices are available through settings: 

configure do
  set :foo, 'bar'
end

get '/' do
  settings.foo? # => true
  settings.foo  # => 'bar'
  ...
end

Configuring attack protection

Sinatra protects your application from frequent, opportunistic attacks with Rack::Protection. This behavior, which exposes your application to a tonne of widely used vulnerabilities, may be easily disabled:

Configuring attack protection

disable: protection

 

Set protection to an options hash to skip one layer of defense:

Set: protection, : except =>:path_traversal


You can also hand in an array to disable a list of protections:

setting "protection, unless" [:path traversal,:session hijacking] 

 

If sessions have been enabled, Sinatra will by default only put up session-based protection. Consult "Using Sessions." Occasionally, you might wish to create sessions "outside" of the Sinatra application, perhaps in config.ru or with a different Rack::Builder instance. In that instance, passing the "session option" will still allow you to set up session-based protection.

set :protection, : session => true

Frequently Asked Questions

What exactly is Sinatra Framework?  

Web application library and domain-specific language Sinatra are free and open source pieces of Ruby software. It is a Ruby web application framework in place of Ruby on Rails, Merb, Nitro, and Camping. On the Rack web server interface is dependent. It bears Frank Sinatra's name as a tribute.

How would you defend your business from a denial of service assault? 

Use firewalls and other network security tools, or think about using DoS prevention services that your service provider might provide. Get in touch with your Internet or cloud service provider as soon as you can. Recover. Look for any other malicious behavior that might have occurred during the DoS assault.

What flaw can result in a denial of service attack? 

On rare occasions, a DoS attack makes use of a flaw in software or website to forcibly make use of its resources or network connections, resulting in a denial of service. DoS assaults can also be launched by some malware.

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

In this article, we have gone through an understanding of the Configuration in Sinatra. Under it’s setting, how can we multiple configure? And also understand the concept of configuration attack protection. 

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