Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up
for free
Building with Rack
Alexander Clark
February 23, 2016
Programming
2
39
Building with Rack
Let's build a Rack app!
Alexander Clark
February 23, 2016
Tweet
Share
More Decks by Alexander Clark
See All by Alexander Clark
alexanderclark
0
35
alexanderclark
0
26
Other Decks in Programming
See All in Programming
gernotstarke
0
380
kgmyshin
1
440
grapecity_dev
0
170
hanasuke
0
140
line_developers_tw2
0
570
nearmugi
0
170
bkuhlmann
4
270
legalforce
PRO
0
610
legalforce
PRO
1
690
bells17
0
360
takahi5
1
410
zwass
0
350
Featured
See All Featured
lara
590
61k
tenderlove
53
3.5k
kastner
54
1.9k
philnash
9
580
holman
288
130k
tmm1
61
9.2k
bermonpainter
342
26k
maggiecrowley
10
500
rocio
155
11k
geeforr
332
29k
keathley
20
700
danielanewman
200
20k
Transcript
Building with Rack Alexander Clark
What is Rack?
?
None
?
None
?
?
What does a Rack app look like? ?
What does a Rack app look like? GET /foo HTTP/1.1
Host: localhost:9292 Accept: text/html … ?
What does a Rack app look like? GET /foo HTTP/1.1
Host: localhost:9292 Accept: text/html … ? <!DOCTYPE html> <html> <head> …
run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']]
}
run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']]
} Responds to #call HTTP Status code Headers Response body Run
run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']]
} Responds to #call Much HTTP Status code Such Headers Response body Wow Run
Let’s build something!
None
<!DOCTYPE html> <html> <head> <title>Is it April Fools?</title> </head> <body>
<h1><%= @april_fools ? 'YES' : 'NO' %></h1> </body> </html>
require 'erb' class MyRackApp def initialize @april_fools = Time.now.strftime('%m%d') ==
'0401' end def call(_env) ['200', {'Content-Type' => 'text/html'}, view] end def view [ERB.new(template).result(binding)] end def template File.open('index.html.erb', 'r').read end end
require './my_rack_app' run MyRackApp.new
None
?
run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']]
} run MyRackApp.new ... def call(_env) ['200', {'Content-Type' => 'text/html'}, view] end
run MyRackApp.new run Rails.application
Middleware
TL;DL
Further Reading • https://rack.github.io/ • http://alexander-clark.com/blog/building-a-rack-app/ • http://www.justinweiss.com/articles/a-web-server-vs- an-app-server/ •
https://www.amberbit.com/blog/2011/07/13/ introduction-to-rack-middleware/
Thanks for Listening Alexander Clark atheclark alexander-clark.com alexander-clark