Rack in 5 minutes

What is Rack

  • uses Ruby’s built-in WEBrick web server
  • provides a web server interface
  • a protocol for building composing web apps
  • collection of middleware utilities

Web server interface

Rack provides an interface so that you don’t have to deal with some elements of the server. Such as listening on a port, accepting connections, parsing HTTP responses and requests.
This gives you more time to spend on writing what the application does and providing value.

Protocol

Rack protocol is simply a Ruby object with a call method, being a proc it has a “call” method. The method arguments are the environment variables which describe the incoming request. The return value is an array with [status, headers, body].

  • status: The HTTP status code
  • headers: hash of HTTP headers for the response
  • body: Body of the response.
    A simple HelloWorld app in Rack
#config.ru
class HelloWorld
 def self.call(env)
 [ 200,
 {"Content-Type" => "text/plain"},
 ["Hello World! "]
 ]
 end
end

Entering ‘rackup’ in the terminal will start our rack up

Accessing the up and running website by going to “localhost:9292”

And That’s it!

Within moments we have an app running.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s