Slide 1

Slide 1 text

RailsとRack 第10回 OSSパッチ会 - 谷内

Slide 2

Slide 2 text

Rackとは?

Slide 3

Slide 3 text

Rackとは? Rack provides a minimal interface between webservers that support Ruby and Ruby frameworks. ref: http://rack.github.io/ ・WebサーバーとRubyやRubyフレームワークとの間のインターフェースを  ミニマルにしてくれるもの ・WebサーバーとRubyフレームワークをつなぐ規約を定義している

Slide 4

Slide 4 text

Rackに準拠したアプリケーションの規約 To use Rack, provide an "app": an object that responds to the `call` method, taking the environment hash as a parameter, and returning an Array with three elements: - The HTTP response code - A Hash of headers - The response body, which must respond to each ref: http://rack.github.io/ ・1つのHashオブジェクトを引数に取る `call` というメソッドが実装されていること ・`call` メソッドは、「ステータスコード, ヘッダーを表現したHash, eachに反応するオブ ジェクト」の3つの要素を持つ配列を返すこと

Slide 5

Slide 5 text

Simple Rack Application

Slide 6

Slide 6 text

RackとRails

Slide 7

Slide 7 text

rails sしたときのRack関連の処理 Rails::Server#start - Rails::Server は Rack::Server を継承している - Rails::Server#startの中でsuperが呼ばれる

Slide 8

Slide 8 text

rails sしたときのRack関連の処理 - Rack::Server#start - 後述するが server は Rack::Handler が返ってくる - Rack::Handler::Webrick.run や Rack::Handler::Puma.run 等が呼ばれる

Slide 9

Slide 9 text

rails sしたときのRack関連の処理 Rack::Server#server - optionをもとに Rack::Handler をgetして返す

Slide 10

Slide 10 text

rails sしたときのRack関連の処理 Rack::Server#wrapped_app - middlewareをeachして呼び出し、appにwrapしている

Slide 11

Slide 11 text

rails sしたときのRack関連の処理 Rack::Server#app - options[:config]を元にappをビルド - Rack::Builder はRackのDSLが実装されているクラス - options[:config] のところに config.ru が指定されている

Slide 12

Slide 12 text

Railsのconfig.ru - Rails::Application を継承した自身のRailsアプリ(例: MyApp::Application) が Rails.applicationとして定義されている

Slide 13

Slide 13 text

まとめ - Rails::ServerはRack::Serverを継承しており、その中でRack::Middlewareがラッ プされたアプリケーションをビルドしたり、Webサーバーの呼び出しを行っている - RailtieというよりRackの説明になってしまった(Rack shaving)

Slide 14

Slide 14 text

参考 - rack/rack: a modular Ruby webserver interface - Rack: a Ruby Webserver Interface - Rails on Rack — Ruby on Rails Guides - 翻訳 - Rackとは何か - Qiita - Rails開発におけるwebサーバーとアプリケーションサーバーの違い(翻訳) - Qiita - Rack解説 - Rackの構造とRack DSL - Qiita - パーフェクト Ruby on Rails