Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ちょっとわかるrack

 ちょっとわかるrack

初心者向けrackの簡単な紹介
・rackとは何か
・rackアプリの作り方
・rack middlewareの作り方・使い方

※2012/5/14の会社内LTで使用した資料

70762059db0218c6f0ff48042ca0756a?s=128

Leonard Chin

May 24, 2012
Tweet

Transcript

  1. ͪΐͬͱΘ͔Δ

  2. Web Server App Server IUUQ DBMMSVCZ DBMMBQQ Your App

  3. Web Server App Server IUUQ DBMMSVCZ DBMMBQQ Your App BQBDIFOHJOYFUD

    QBTTFOHFSNPOHSFMUIJOFUD
  4. Web Server App Server IUUQ DBMMSVCZ DBMMBQQ Your App BQBDIFOHJOYFUD

    QBTTFOHFSNPOHSFMUIJOFUD ͜ͷ*'ɺͲ͏͢Δʁ
  5. Web Server App Server IUUQ DBMMSVCZ DBMMBQQ Your App BQBDIFOHJOYFUD

    QBTTFOHFSNPOHSFMUIJOFUD ڞ௨*'3BDL 3BDL"QQMJDBUJPO
  6. call(env)

  7. def call(env) ... end 3FRVFTUΛ౉͞ΕΔ 3FTQPOTFΛฦ͢ [status, headers, response_body]

  8. ৄࡉ͸41&$Ͱ http://rack.rubyforge.org/doc/SPEC.html

  9. 4JOBUSBͷ৔߹

  10. # 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
  11. SBDLVQ

  12. rackup config.ru ΞϓϦͷೖΓޱΛఆٛ Rack::Builder.new { <config.ru> }.to_app ʹ౉͢

  13. #config.ru require ‘rack’ require ‘myapp’ run MyApp.new 3BDL"QQMJDBUJPO JFJNQMFNFOUTcall(env)

  14. # Minimum config.ru require ‘rack’ run lambda { |env| [200,

    { "Content-Type" => "text/plain" }, ["OK"] ] } TUBUVT IFBEFST CPEZ
  15. 3BDL.JEEMFXBSF

  16. Web Server App Server IUUQ DBMMSVCZ Your App BQBDIFOHJOYFUD QBTTFOHFSNPOHSFMUIJOFUD

    3BDL*' 3BDL"QQMJDBUJPO Middleware Middleware 3BDL*' 3BDL*' .JEEMFXBSFΛૠೖ ^
  17. w $43' w 3FEJSFDUJPO w -PHHJOH w 4FTTJPO.BOBHFNFOU w "VUIFOUJDBUJPO

    w &NCFE(PPHMF"OBMZUJDT FUD
  18. 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Λ͍͡Δ
  19. &YBNQMFSBDL@DTSG

  20. 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ΛνΣοΫ ௨ͬͨΒɺͦͷ··࣍΁
  21. 6TJOH.JEEMFXBSF

  22. #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͞ΕͯΔ