Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Building with Rack
Search
Alexander Clark
February 23, 2016
Programming
2
310
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
Off the Rails: Patterns for fixing fat models, out-of-controllers, and other Rails problems
alexanderclark
0
300
Docker: What It Is and Why You Should Care
alexanderclark
0
320
Other Decks in Programming
See All in Programming
Agentic Applications with Symfony
el_stoffel
2
270
php-fpm がリクエスト処理する仕組みを追う / Tracing-How-php-fpm-Handles-Requests
shin1x1
5
2.9k
Chrome Extension Techniques from Hell
moznion
1
160
サービスクラスのありがたみを発見したときの思い出 #phpcon_odawara
77web
4
630
Compose Hot Reload is here, stop re-launching your apps! (Android Makers 2025)
zsmb
1
470
Kamal 2 – Get Out of the Cloud
aleksandrov
1
170
地域ITコミュニティの活性化とAWSに移行してみた話
yuukis
0
230
Enterprise Web App. Development (1): Build Tool Training Ver. 5
knakagawa
1
110
メモリウォールを超えて:キャッシュメモリ技術の進歩
kawayu
0
1.9k
Day0 初心者向けワークショップ実践!ソフトウェアテストの第一歩
satohiroyuki
0
830
ベクトル検索システムの気持ち
monochromegane
31
9.8k
Defying Front-End Inertia: Inertia.js on Rails
skryukov
0
460
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
71
10k
Why Our Code Smells
bkeepers
PRO
336
57k
BBQ
matthewcrist
88
9.6k
It's Worth the Effort
3n
184
28k
The Cult of Friendly URLs
andyhume
78
6.3k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Visualization
eitanlees
146
16k
What's in a price? How to price your products and services
michaelherold
245
12k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.1k
Gamification - CAS2011
davidbonilla
81
5.2k
Adopting Sorbet at Scale
ufuk
76
9.3k
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