SlideShare a Scribd company logo
8 minutes on
Rack
Dan Webb (dan@danwebb.net)
Web Framework
Web Server
Library
A Convention
If you have a
Ruby object...
that has a call method which
takes one argument...
app.call(env)
and that method returns an
array with 3 elements...
[200, { 'Content-Type' => 'text/plain' }, 'Hello World!']
then you can connect it to any
web server that supports Rack
require 'thin'
Rack::Handler::Thin.run(app, :Port => 4000)
and you've got
yourself a web
application
Fin
app = Proc.new do |env|
[200, { 'Content-Type' => 'text/plain' },
'Hello World!']
end
require 'rubygems'
require 'thin'
Rack::Handler::Thin.run(app, :Port => 4000)
class HelloWorld
def initialize(name)
@name = name
end
def call(env)
[200, { 'Content-Type' => 'text/plain' },
"Hello #{@name}!"]
end
end
require 'rubygems'
require 'rack'
Rack::Handler::Mongrel.run(HelloWorld.new("Dan"),
:Port => 4000)
// Courtesy of Pratik Naik!
#include "ruby.h"
VALUE method_call(VALUE self, VALUE env) {
VALUE response = rb_ary_new();
VALUE headers = rb_hash_new();
rb_hash_aset(headers,
rb_str_new2("Content-Type"), rb_str_new2("text/plain"));
rb_ary_push(response, INT2NUM(200));
rb_ary_push(response, headers);
rb_ary_push(response, rb_str_new2("Hello World!"));
return response;
}
void Init_rock() {
VALUE Rock = rb_define_class("Rock", rb_cObject);
rb_define_method(Rock, "call", method_call, 1);
}
// run.rb
require 'rock'
require 'thin'
Rack::Handler::Thin.run Rock.new, :Port => 4000
def call(env)
{
"SERVER_NAME"=>"localhost",
"HTTP_ACCEPT_ENCODING"=>"gzip,deflate",
"HTTP_USER_AGENT"=>"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-
GB; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4",
"PATH_INFO"=>"/",
"SCRIPT_NAME"=>"",
"SERVER_PROTOCOL"=>"HTTP/1.1",
"HTTP_ACCEPT_LANGUAGE"=>"en-gb,en;q=0.5",
"HTTP_HOST"=>"localhost:4000",
"REMOTE_ADDR"=>"127.0.0.1",
"HTTP_KEEP_ALIVE"=>"300",
"REQUEST_PATH"=>"/",
"SERVER_SOFTWARE"=>"thin 0.8.2 codename Double Margarita",
"HTTP_ACCEPT_CHARSET"=>"ISO-8859-1,utf-8;q=0.7,*;q=0.7",
"HTTP_VERSION"=>"HTTP/1.1",
"REQUEST_URI"=>"/",
"SERVER_PORT"=>"4000",
"QUERY_STRING"=>"",
"GATEWAY_INTERFACE"=>"CGI/1.2",
"HTTP_ACCEPT"=>"text/html,application/xhtml+xml,application/xml;q=0.9,
*/*;q=0.8",
"HTTP_CONNECTION"=>"keep-alive",
"REQUEST_METHOD"=>"GET"
}
[200, { 'Content-Type' => 'text/plain' }, "Hello #{@name}!"]
Status Code
[200, { 'Content-Type' => 'text/plain' }, "Hello #{@name}!"]
HTTP Headers
[200, { 'Content-Type' => 'text/plain' }, "Hello #{@name}!"]
Response Body
Response body can be any
object that respond_to?(:each)
file = File.new('myfile.xml')
[200, { 'Content-Type' => 'application/xml' }, file]
class StreamingFile
def initialize(file)
@file = file
end
def length
File.size(@file)
end
def last_modified
File.mtime(@file).rfc822
end
def each
File.open(@file, "rb") do |file|
while part = file.read(8192)
yield part
end
File.delete(@file)
end
end
[200, {
'Content-Type' => 'audio/mp3',
'Content-Length' => file.length.to_s
}, file]
Duck typing!
• Streaming
• Clean up after response sent
• Complex responses
• Loads more...
Why?
Common interface
• Passenger
• Mongrel
• CGI
• SCGI
• FastCGI
• Thin
• Ebb
• Fuzed
• Webrick
• Litespeed
Write once,
serve however...
Convienient way
to write
micro apps
Example:
Development
Server
8 Minutes On Rack
class StaticOrRedirect
def initialize(options={})
@redirect_base = options[:redirect_base]
root = options[:root] || Dir.pwd
@file_server = Rack::File.new(root)
end
def call(env)
path = env["PATH_INFO"]
resp = @file_server.call(env)
if resp.first == 404
[302, {
'Content-Type' => 'text/plain',
'Location' => "#{@redirect_base}#{env['PATH_INFO']}"
}, 'Not here!']
else
resp
end
end
end
Rack::Handler::Thin.run(
StaticOrRedirect.new( :redirect_base => 'http://bbc.co.uk' )
)
There's more...
• The Rack Gem
• Middleware
• Rack and Passenger
• rackup
rack.rubyforge.org
danwebb.net
Questions?

More Related Content

PDF
Rack Middleware
LittleBIGRuby
 
PDF
Psr-7
Marco Perone
 
PDF
Psr 7 symfony-day
Marco Perone
 
PDF
Rack
Sarah Allen
 
PDF
Ruby HTTP clients comparison
Hiroshi Nakamura
 
PPT
Real time server
thepian
 
PDF
用Tornado开发RESTful API运用
Felinx Lee
 
PDF
Introduction to Flask Micro Framework
Mohammad Reza Kamalifard
 
Rack Middleware
LittleBIGRuby
 
Psr 7 symfony-day
Marco Perone
 
Ruby HTTP clients comparison
Hiroshi Nakamura
 
Real time server
thepian
 
用Tornado开发RESTful API运用
Felinx Lee
 
Introduction to Flask Micro Framework
Mohammad Reza Kamalifard
 

What's hot (20)

KEY
Mojo as a_client
Marcus Ramberg
 
PDF
Ruby HTTP clients
Zoran Majstorovic
 
PPTX
Webrtc mojo
bpmedley
 
PDF
Flask patterns
it-people
 
PDF
Symfony2 Components - The Event Dispatcher
Sarah El-Atm
 
KEY
Using and scaling Rack and Rack-based middleware
Alona Mekhovova
 
PDF
Perl web frameworks
diego_k
 
ODP
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
King Foo
 
PDF
Python RESTful webservices with Python: Flask and Django solutions
Solution4Future
 
ZIP
Web Apps in Perl - HTTP 101
hendrikvb
 
PDF
Serverless Ballerina
Ballerina
 
PDF
Inside Bokete: Web Application with Mojolicious and others
Yusuke Wada
 
ODP
Integrating icinga2 and the HashiCorp suite
Bram Vogelaar
 
PDF
Pycon - Python for ethical hackers
Mohammad Reza Kamalifard
 
KEY
Keeping it small: Getting to know the Slim micro framework
Jeremy Kendall
 
PPT
Slim RedBeanPHP and Knockout
Vic Metcalfe
 
PDF
Lies, Damn Lies, and Benchmarks
Workhorse Computing
 
PDF
Developing apps using Perl
Anatoly Sharifulin
 
PDF
Bootstrapping multidc observability stack
Bram Vogelaar
 
ODP
Modern Web Development with Perl
Dave Cross
 
Mojo as a_client
Marcus Ramberg
 
Ruby HTTP clients
Zoran Majstorovic
 
Webrtc mojo
bpmedley
 
Flask patterns
it-people
 
Symfony2 Components - The Event Dispatcher
Sarah El-Atm
 
Using and scaling Rack and Rack-based middleware
Alona Mekhovova
 
Perl web frameworks
diego_k
 
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
King Foo
 
Python RESTful webservices with Python: Flask and Django solutions
Solution4Future
 
Web Apps in Perl - HTTP 101
hendrikvb
 
Serverless Ballerina
Ballerina
 
Inside Bokete: Web Application with Mojolicious and others
Yusuke Wada
 
Integrating icinga2 and the HashiCorp suite
Bram Vogelaar
 
Pycon - Python for ethical hackers
Mohammad Reza Kamalifard
 
Keeping it small: Getting to know the Slim micro framework
Jeremy Kendall
 
Slim RedBeanPHP and Knockout
Vic Metcalfe
 
Lies, Damn Lies, and Benchmarks
Workhorse Computing
 
Developing apps using Perl
Anatoly Sharifulin
 
Bootstrapping multidc observability stack
Bram Vogelaar
 
Modern Web Development with Perl
Dave Cross
 
Ad

Viewers also liked (8)

PDF
Restful API On Grape
Andy Wang
 
PDF
Grape Presentation
chrishein
 
PDF
The Grapes of Rapid (RubyConf 2010)
Michael Bleigh
 
PPTX
Building an API using Grape
visnu priya
 
PDF
Rapid-ruby-api-on-grape
Andy Wang
 
PPTX
Building RESTful APIs w/ Grape
Daniel Doubrovkine
 
PDF
The Mysteries Of JavaScript-Fu (RailsConf Ediition)
danwrong
 
PPTX
Ruby On Grape
Andrii Furmanets
 
Restful API On Grape
Andy Wang
 
Grape Presentation
chrishein
 
The Grapes of Rapid (RubyConf 2010)
Michael Bleigh
 
Building an API using Grape
visnu priya
 
Rapid-ruby-api-on-grape
Andy Wang
 
Building RESTful APIs w/ Grape
Daniel Doubrovkine
 
The Mysteries Of JavaScript-Fu (RailsConf Ediition)
danwrong
 
Ruby On Grape
Andrii Furmanets
 
Ad

Similar to 8 Minutes On Rack (20)

PDF
Ruby MVC from scratch with Rack
DonSchado
 
PDF
Communication is a Technical Skill
Sarah Allen
 
KEY
Rack
shaokun
 
PDF
Intro to Rack
Rubyc Slides
 
PDF
Ruby conf 2011, Create your own rails framework
Pankaj Bhageria
 
PDF
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
Alberto Perdomo
 
PDF
Rack
shen liu
 
PDF
Rack
Kerry Buckley
 
PDF
Ruby off Rails (english)
Stoyan Zhekov
 
PDF
Ruby off Rails (japanese)
Stoyan Zhekov
 
PPTX
What the rack
Luis Vasconcellos
 
PDF
From zero to almost rails in about a million slides...
david_e_worth
 
KEY
Wider than rails
Alexey Nayden
 
KEY
Rails Presentation (Anton Dmitriyev)
True-Vision
 
KEY
Something something rack
joren de groof
 
PDF
Web Clients for Ruby and What they should be in the future
Toru Kawamura
 
PDF
MeshU Thin & Rack
guestbac5dc
 
PDF
Sinatra Introduction
Yi-Ting Cheng
 
PDF
Building web framework with Rack
sickill
 
PDF
Using Sinatra to Build REST APIs in Ruby
LaunchAny
 
Ruby MVC from scratch with Rack
DonSchado
 
Communication is a Technical Skill
Sarah Allen
 
Rack
shaokun
 
Intro to Rack
Rubyc Slides
 
Ruby conf 2011, Create your own rails framework
Pankaj Bhageria
 
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
Alberto Perdomo
 
Rack
shen liu
 
Ruby off Rails (english)
Stoyan Zhekov
 
Ruby off Rails (japanese)
Stoyan Zhekov
 
What the rack
Luis Vasconcellos
 
From zero to almost rails in about a million slides...
david_e_worth
 
Wider than rails
Alexey Nayden
 
Rails Presentation (Anton Dmitriyev)
True-Vision
 
Something something rack
joren de groof
 
Web Clients for Ruby and What they should be in the future
Toru Kawamura
 
MeshU Thin & Rack
guestbac5dc
 
Sinatra Introduction
Yi-Ting Cheng
 
Building web framework with Rack
sickill
 
Using Sinatra to Build REST APIs in Ruby
LaunchAny
 

More from danwrong (8)

PDF
Building Non-shit APIs with JavaScript
danwrong
 
KEY
Loadrunner
danwrong
 
PDF
Bringing the Same-Origin Policy to its Knees
danwrong
 
KEY
Building @Anywhere (for TXJS)
danwrong
 
PDF
Taming The Beast
danwrong
 
PDF
Metaprogramming JavaScript
danwrong
 
PDF
The Mysteries Of JavaScript-Fu (@media Europe Edition)
danwrong
 
PDF
The Mysteries Of JavaScript-Fu (@media SF Edition)
danwrong
 
Building Non-shit APIs with JavaScript
danwrong
 
Loadrunner
danwrong
 
Bringing the Same-Origin Policy to its Knees
danwrong
 
Building @Anywhere (for TXJS)
danwrong
 
Taming The Beast
danwrong
 
Metaprogramming JavaScript
danwrong
 
The Mysteries Of JavaScript-Fu (@media Europe Edition)
danwrong
 
The Mysteries Of JavaScript-Fu (@media SF Edition)
danwrong
 

Recently uploaded (20)

PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Doc9.....................................
SofiaCollazos
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Doc9.....................................
SofiaCollazos
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
The Future of Artificial Intelligence (AI)
Mukul
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 

8 Minutes On Rack