rack
DESCRIPTION
Rack IntroTRANSCRIPT
RACK
Sowjanya Mudunuri
RACK
Disclaimer:
This notes is composed from various resources which are listed at the end of this presentation. This presentation is used to help my friends get some basic knowledge about RACK
What is Rack?
Developed by Christian Neukirchen
A modular ruby webserver interface for developing web applications in Ruby
Available as a Ruby Gem
The source code can be found at https://github.com/rack/rack
Rack Application
A Rack Application is a Ruby Object that responds to Call method
It takes one argument the environment hash and returns an array of three elements. (HTTP response code, a hash of headers, the response body, which responds to each)
The Gem provides handlers to connect to all kinds of web servers to rack. Mongrel, Thin, Webrick, Puma …
Basic Rack Appmy_first.rb
require 'rack’
my_app = Proc.new do |env|
["200", {'Content-Type' => 'text/html'}, ["Sowju's First rack app"]]
end
#Starts on port 8080
Rack::Handler::WEBrick.run my_app
Alternative Rack App
require 'rack’
class HelloWorld
def call(env)
[200, {"Content-Type" => "text/html"}, "Hello Rack!"]
end
end
Rack::Handler::Thin.run HelloWorld.new, :Port => 9292
Steps needed to deploy a rack app to Heroku
Create your .rvmrc file with the following contents(Preferred)
rvm_install_on_use_flag=1
rvm_trust_rvmrcs_flag=1
rvm_gemset_create_on_use_flag=1
rvm use ruby-2.1.0@first-rack create
bundle init
Add gem "rack” to the Gemfile
bundle check
bundle install
bundle show rack
Steps needed to deploy a rack app to Heroku
Create a file named config.ru with the following contents(required)
require './my_app'run MyApp.new
MyApp.new is the Rack App
create a heroku app and add the remotes using
git remote add heroku <link_to_the_heroku_app>
git push heroku master
ApplicationsRack Middleware in Sinatra and Rails
Rack::ShowExceptions
Rack::CommonLogger
Rack::UrlMap
Sinatra and Rails ride on top of Rack
Rails.application is the primary Rack application object
Any rack compliant web server should be using Rails.application object to serve a Rails application
Easy to combine web applications(just a ruby object) and run them on a single web server without external configuration
References & More Resources
http://rack.github.io/
https://devcenter.heroku.com/articles/rack
http://rubydoc.info/github/rack/rack/master/file/SPEC
https://github.com/rack/rack/wiki/Rack-app-with-uri-and-HTTP-specific-responses
https://github.com/rack/rack/wiki/Tutorials
http://m.onkey.org/ruby-on-rack-1-hello-rack
http://guides.rubyonrails.org/rails_on_rack.html
http://m.onkey.org/ruby-on-rack-2-the-builder
http://rubylearning.com/blog/a-quick-introduction-to-rack/