BACKSHOP from wikipedia

" A backshop or back-shop[citation needed] is a specialized store or workshop found in service industries, such as locomotive and aircraft repair. Most repairs are carried out in small workshops, except where an industrial service is needed. "


Nilton & Sérgio

Thank you !

Mr. Backshop is awesome


Hacking with Rack


What the heck Rack is ?

Github tdantas / Twitter @thiagochapa

Agenda



         $git tag -ln
            V1.0  before rack
            V1.1  before rack with handler abstractions
            V2.0  rack in action
            V2.1  rack with callable 'Method' example
            V2.2  DIY sample descriptions
            V2.3  sample 1 test RED
            V2.4  sample1 test GREEN
            V2.5  sample 2 test RED
            V2.6  sample 2 test GREEN
            V3.0  lets rack like a boss [ rackup]
            V3.10 rackup file read by Rack::Builder DSL
            V3.2  rackup default options
            V3.3  rackup routing
            V3.4  rackup reloader without middleware DSL
            V3.5  rackup reloader with middleware DSL
            V3.6  rackup show exception middleware
            V3.7  rack request for better code
            V3.8  rack response for better code
            V3.9  bieber framework \BAM/
            V4.0  middleware response time
            V5.0  mimicking sinatra
            V5.1  sinatra 'get' done
            


Checkout the code




      git clone https://github.com/tdantas/backshop-rack.git                
    

What Rack try to solve ?

Christian Neukirchen

"Dabbling in my own web framework experiments, I noticed that there is a lot of code duplication among frameworks since they essentially all do the same things. And still, every Ruby web framework developer is writing his own handlers for every webserver he wants to use. Hopefully, the framework users are satisfied with that choice. However, in essence, dealing with HTTP is rather easy. In the end, you get a request and return a response."

Checkout

Checkout V1.0

'The Pain'



                       git checkout -b V1.0 tags/V1.0
    

Checkout


V1.1 ' The Pain with some abstraction ( Handlers )'

decoupling your app from web server



                     git checkout -b V1.1 tags/V1.1
 

RACK #FTW

Interface for web applications servers to talk with web applications



        
      [Thin, WEBRick, Passenger, Mongrel ] < RACK > [ YOUR APP ]

      [WEB SERVER] < RACK > [ RAILS  SINATRA  PADRINO ]

      [WEB SERVER] < RACK > [ APP THAT IMPLEMENTS RACK INTERFACE ]
          

 

Checkout


V2.0 'RACK FOR RESCUE (CLI)'

Using IRB 



                      git checkout -b V2.0 tags/V2.0
 

Checkout


V2.1 'RACK FOR RESCUE (CLI)'

Callable bounded methods



                  
                      git checkout -b V2.1 tags/V2.1

              

Rack

Really thin and super fast


lets benchmark super dummy app


                 ab -C 50 -n 1000 http://localhost:3000
                 #=> ~ 2300 req/s  
                 

Goliath

Postrank labs

In practice, Goliath was designed with a "as close to the metal" API for a reason: we run endpoints which need to handle continuous load for one specific service. Hence, a process is responsible for a single route, and more often than not, we would have multiple goliaths load-balanced behind haproxy for redundancy and failover. We implemented the "routing" at haproxy level, which also allowed us to easily manage and scale out the clusters.

Goliath


"From experience, when you're running larger services ...  you *want* the process isolation where each cluster serves a single purpose  . As such, we never had a need for an internal router. "

RECAP

Rack Contract / Interface


  • respond_to :call
  • return array
  • [ status, headers, body ]
  • status - integer
  • headers - hash
  • body - respond_to :each

DIY

Show me your code

From RED to GREEN


                  
             git checkout -b V2.3 tags/V2.3

              $ tree
              .
              ├── cli-commands.rb
              └── samples
                  ├── sample_1.rb  
                  ├── sample_2.rb
                  └── test
                      ├── sample1_test.rb <-- It's your Duty
                      └── test_helper.rb

              

DIY

Show me your code

From RED to GREEN


              git checkout -b V2.5 tags/V2.5
              
              $ tree
                .
                ├── cli-commands.rb
                └── samples
                    ├── sample_1.rb
                    ├── sample_2.rb
                    └── test
                        ├── sample1_test.rb
                        ├── sample2_test.rb  <-- It's your Duty
                        └── test_helper.rb

              

Rack Structure


https://github.com/rack/rack

            ├── COPYING
            ├── KNOWN-ISSUES
            ├── README.rdoc
            ├── Rakefile
            ├── SPEC
            ├── bin
            │   └── rackup  <-- will build/pipeline your RACK App
             ... 
          

Rackup


Coordinating your Rack App

DSL to build your Rack app easily

Agnostic Server



                     git checkout -b V3.0 tags/V3.0
              

Rackup


Route Map



                       git checkout -b V3.3 tags/V3.3
              

Dev mode


Reloading your code



                      git checkout -b V3.4 tags/V3.4
                

Recap




              REQ('/ruby') -> WS('mongrel') -> RACK -> RELOADER -> APP
                                            |-----------------------|
                                                     app_ruby
            

Rackup middleware DSL





                     git checkout -b V3.5 tags/3.5
            

Rack::Request




                      git checkout -b V3.7 tags/V3.7
            

Rack::Response




                        git checkout -b V3.8 tags/V3.8
            

Frameworking



                       git checkout -b V3.9 tags/V3.9
            

Bieber FRAMEWORK

#irresponsibleruby


                      git checkout -b V3.10 tags/V3.10
Disclaimer: Just one abstraction over rack to your code.

Response Time middleware



                      git checkout -b V4.0 tags/V4.0
            

DIY

Duty: create your own middleware

  • Logging ( ip , timestamp, path, params )
  • API Rate Limit ( install redis )
  • Authentication Middleware
  • ...

Sinatra

Next backshop we will talk about sinatra.

Let's try mimicking sinatra's syntax ( only GET )


Your duty is create all HTTP Verbs syntax


                    git checkout -b V5.1 tags/V5.1
            

THE END

Thank you

Rack Backshop

By tdantas

Rack Backshop

Rack. Understanding how to create rack applications

  • 532