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
ちょっとわかるrack
Search
Leonard Chin
May 24, 2012
Programming
10
1.2k
ちょっとわかるrack
初心者向けrackの簡単な紹介
・rackとは何か
・rackアプリの作り方
・rack middlewareの作り方・使い方
※2012/5/14の会社内LTで使用した資料
Leonard Chin
May 24, 2012
Tweet
Share
More Decks by Leonard Chin
See All by Leonard Chin
生鮮ECのタイムセールを 耐え抜いてきた話 / Handling high traffic sale days at Cookpad Mart
lchin
3
12k
Performance as a Product Feature
lchin
5
1.9k
Database Performance for Ruby on Rails Applications
lchin
7
1.2k
Database Performance for Rails Applications
lchin
5
660
How to Survive and thrive as an engineer in a foreign land
lchin
4
1.2k
#eachを使ったら負け
lchin
7
1.6k
Other Decks in Programming
See All in Programming
Ethereum_.pdf
nekomatu
0
460
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
ヤプリ新卒SREの オンボーディング
masaki12
0
130
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
200
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
Arm移行タイムアタック
qnighy
0
310
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
470
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
100
Featured
See All Featured
Code Review Best Practice
trishagee
64
17k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Agile that works and the tools we love
rasmusluckow
327
21k
It's Worth the Effort
3n
183
27k
The Cult of Friendly URLs
andyhume
78
6k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Being A Developer After 40
akosma
86
590k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Transcript
ͪΐͬͱΘ͔Δ
Web Server App Server IUUQ DBMMSVCZ DBMMBQQ Your App
Web Server App Server IUUQ DBMMSVCZ DBMMBQQ Your App BQBDIFOHJOYFUD
QBTTFOHFSNPOHSFMUIJOFUD
Web Server App Server IUUQ DBMMSVCZ DBMMBQQ Your App BQBDIFOHJOYFUD
QBTTFOHFSNPOHSFMUIJOFUD ͜ͷ*'ɺͲ͏͢Δʁ
Web Server App Server IUUQ DBMMSVCZ DBMMBQQ Your App BQBDIFOHJOYFUD
QBTTFOHFSNPOHSFMUIJOFUD ڞ௨*'3BDL 3BDL"QQMJDBUJPO
call(env)
def call(env) ... end 3FRVFTUΛ͞ΕΔ 3FTQPOTFΛฦ͢ [status, headers, response_body]
ৄࡉ41&$Ͱ http://rack.rubyforge.org/doc/SPEC.html
4JOBUSBͷ߹
# Rack call interface. def call(env) dup.call!(env) end attr_accessor :env,
:request, :response, :params def call!(env) # :nodoc: @env = env @request = Request.new(env) @response = Response.new @params = indifferent_params(@request.params) template_cache.clear if settings.reload_templates force_encoding(@params) @response['Content-Type'] = nil invoke { dispatch! } invoke { error_block!(response.status) } unless @response['Content-Type'] if Array === body and body[0].respond_to? :content_type content_type body[0].content_type else content_type :html end end @response.finish end
SBDLVQ
rackup config.ru ΞϓϦͷೖΓޱΛఆٛ Rack::Builder.new { <config.ru> }.to_app ʹ͢
#config.ru require ‘rack’ require ‘myapp’ run MyApp.new 3BDL"QQMJDBUJPO JFJNQMFNFOUTcall(env)
# Minimum config.ru require ‘rack’ run lambda { |env| [200,
{ "Content-Type" => "text/plain" }, ["OK"] ] } TUBUVT IFBEFST CPEZ
3BDL.JEEMFXBSF
Web Server App Server IUUQ DBMMSVCZ Your App BQBDIFOHJOYFUD QBTTFOHFSNPOHSFMUIJOFUD
3BDL*' 3BDL"QQMJDBUJPO Middleware Middleware 3BDL*' 3BDL*' .JEEMFXBSFΛૠೖ ^
w $43' w 3FEJSFDUJPO w -PHHJOH w 4FTTJPO.BOBHFNFOU w "VUIFOUJDBUJPO
w &NCFE(PPHMF"OBMZUJDT FUD
class Middleware def initialize(app) @app = app end def call(env)
# do something response = @app.call(env) # do something end end ࣍ʹݺͿ"QQ.JEEMFXBSF 3FRVFTUΛ͍͡Δ 3FTQPOTFΛ͍͡Δ
&YBNQMFSBDL@DTSG
def call(env) unless env['rack.session'] raise SessionUnavailable.new('Rack::Csrf depends on session middleware')
end self.class.token(env) req = Rack::Request.new(env) untouchable = skip_checking(req) || !@http_methods.include?(req.request_method) || req.params[self.class.field] == env['rack.session'][self.class.key] || req.env[self.class.rackified_header] == env['rack.session'][self.class.key] if untouchable @app.call(env) else raise InvalidCsrfToken if @raisable [403, {'Content-Type' => 'text/html', 'Content-Length' => '0'}, []] end end $43'νΣοΫࣦഊͨ͠Βɺ ͙͢ʹ3FTQPOTFฦ͢ 3FRVFTUʹ$43'ରࡦUPLFOΛνΣοΫ ௨ͬͨΒɺͦͷ··࣍
6TJOH.JEEMFXBSF
#config.ru require ‘rack’ require ‘myapp’ class Middleware def initialize(app) @app
= app end def call(env) env["rack.some_header"] = "setting an example" @app.call(env) end end use Middleware run MyApp.new 3BDL෦Ͱ!VTFྻʹૠೖ 3BDL#VJMEFSʹCJOE͞ΕͯΔ