Day 6 : #100DaysofCode - Setting up a Sinatra App : Part 2 - Config.ru
Tags: ruby, 100daysofcode, codenewbie, womenintech
Published: 2020-06-07 03:56:33

In my last post I mentioned that your Sinatra app file tree should look something like this: 

Directory structure:
```
├── config.ru
├── Gemfile
├── Gemfile.lock
├── Rakefile
├── README
├──── app
│   ├──── controllers
│   │   └──── application_controller.rb
│   ├──── models
│   └──── views
│       ├──── layout.erb
│       └──── index.erb
├──── config
│   ├──── initializers
│   └──── environment.rb
├──── db
│   ├──── migrate
│   └──── seeds.rb
├──── lib
│   └──── .gitkeep
└──── public
|   ├──── images
|   ├──── javascripts
|   └──── stylesheets
|       └───── main.css
└──── spec
    ├──── application_controller_spec.rb
    └──── spec_helper.rb
```

For the next few days I will go over each file/ directory in a sinatra app. Today we will review: 

<h1> config.ru </h1>

When you google what is config.ru you will get answers like this:

<i> config.ru (the .ru stands for "rackup") is a Rack configuration file. Rack provides a minimal interface between webservers that support Ruby and Ruby frameworks. It's like a Ruby implementation of a CGI which offers a standard protocol for web servers to execute programs. Rack's run here means for requests to the server, make Sinatra::Application the execution context from which Sinatra's DSL could be used. All DSL methods on the main are then delegated to this class. <i>

<b> If you are like me and prefer English, then in simple terms. Config.ru is where your application runs. It is what rack searches for to run your controllers and environment.</b>

For a simple application, a config.ru usually looks similar to this: 

```
require './config/environment'

use Rack::MethodOverride
use YourController
run ApplicationController
```

<h1> use versus run </h1>
<i> ``use`` is for middlewares and ``run`` takes an argument that responds to call and returns a final Rack response with a HTTP Response code like 200. <i>

This can be pretty confusing but in short, ``run`` makes a call and receives a response and you do not want to ``run`` a controller multiple times. So in most cases, you will ``run`` your application controller and ``use`` any other controllers that you create for simple applications. 

<h1> What is Rack::MethodOverride? </h1>

<i> Rack::MethodOverride allows the method to be overridden if params[:_method] is set. This is the middleware which supports the PUT and DELETE HTTP method types.</i>

I know, I know -- English. In short, this function helps to support PUT, DELETE, and PATCH request by allowing the application to "trick" the method into thinking it is a GET or POST request. 

This will be explained further in a future post when I review CRUD and the views directory, but for know just know that it helps to make PUT DELETE and PATCH requests. 

Resources

<a href="https://guides.rubyonrails.org/rails_on_rack.html"> Rails on Rack</a>

<a href="https://stackoverflow.com/questions/55072891/what-is-this-file-config-ru-and-what-is-it-for#:~:text=config.ru%20(the%20.,web%20servers%20to%20execute%20programs."> Stack Overflow</a>

<a href="https://stackoverflow.com/questions/20726190/difference-between-use-and-run-in-rack#:~:text=Difference%20between%20use%20%26%20run,-Now%20with%20this&text=so%20use%20will%20only%20invoke,response%20with%20HTTP%20status%20code">Stack Overflow </a>