Upload
duc-nguyen-manh
View
222
Download
0
Embed Size (px)
Citation preview
7/31/2019 Rack in Rails 3 Presentation
1/38
7/31/2019 Rack in Rails 3 Presentation
2/38
Ryan Tomayko
Rack (Core Team)
Rack::Contrib (Maintainer)
Sinatra (Committer)
Rack::Cache (Maintainer)
Shotgun (Maintainer)
GitHub
http://twitter.com/rtomaykohttp://twitter.com/rtomayko7/31/2019 Rack in Rails 3 Presentation
3/38
"Get a better logo, guys."-- Tim Bray
7/31/2019 Rack in Rails 3 Presentation
4/38
The Rack SPEC
"Just" a document.
Conventions for expressing HTTP in Ruby.
Interface, Contract, Agreement, Protocol.
Based on Python's WSGI.
Fits on a single slide.
http://rack.rubyforge.org/doc/SPEC.htmlhttp://rack.rubyforge.org/doc/SPEC.html7/31/2019 Rack in Rails 3 Presentation
5/38
7/31/2019 Rack in Rails 3 Presentation
6/38
Expressing HTTPin Rub
7/31/2019 Rack in Rails 3 Presentation
7/38
GET/helloHTTP/1.1Host:localhost:9292
Connection:keepaliveUserAgent:Mozilla/5.0(Macinto...Accept:application/xml,applicati...AcceptEncoding:gzip,deflate,sdchAcceptLanguage:enUS,en;q=0.8AcceptCharset:UTF8,*;q=0.5CacheControl:maxage=0Cookie:foo=bar
HTTP Request
7/31/2019 Rack in Rails 3 Presentation
8/38
GET/helloHTTP/1.1Host:localhost:9292
Connection:keepaliveUserAgent:Mozilla/5.0(Macinto...Accept:application/xml,applicati...AcceptEncoding:gzip,deflate,sdchAcceptLanguage:enUS,en;q=0.8AcceptCharset:UTF8,*;q=0.5CacheControl:maxage=0Cookie:foo=bar
HTTP Request{"REQUEST_METHOD"=>"GET","PATH_INFO"=>"/hello",
"SCRIPT_NAME"=>"","SERVER_NAME"=>"localhost","SERVER_PORT"=>"9292","REMOTE_ADDR"=>"127.0.0.1","QUERY_STRING"=>"","HTTP_HOST"=>"localhost:9292","HTTP_CONNECTION"=>"keepalive","HTTP_USER_AGENT"=>"Mozilla/5.0(...","HTTP_ACCEPT"=>"application/xml,a...","HTTP_ACCEPT_ENCODING"=>"gzip,def...","HTTP_ACCEPT_LANGUAGE"=>"enUS,en...","HTTP_ACCEPT_CHARSET"=>"UTF8,*;q=0.5","HTTP_CACHE_CONTROL"=>"maxage=0",
"HTTP_COOKIE"=>"foo=bar","rack.url_scheme"=>"http","rack.input"=>#,"rack.errors"=>#,"rack.version"=>[1,1],"rack.run_once"=>false,"rack.multithread"=>true,"rack.multiprocess"=>false}
Rack "env" Hash
7/31/2019 Rack in Rails 3 Presentation
9/38
HTTP/1.1200OKConnection:close
Date:Tue,16Feb201023:49:18GMTContentType:text/plainContentLength:11CacheControl:maxage=60
HelloWorld
HTTP Response
7/31/2019 Rack in Rails 3 Presentation
10/38
HTTP/1.1200OKConnection:close
Date:Tue,16Feb201023:49:18GMTContentType:text/plainContentLength:11CacheControl:maxage=60
HelloWorld
HTTP Response
[
200,
{'ContentType'=>'text/plain','ContentLength'=>'11','CacheControl'=>'maxage=60'},["HelloWorld"]]
Rack Response
7/31/2019 Rack in Rails 3 Presentation
11/38
HTTP/1.1200OKConnection:close
Date:Tue,16Feb201023:49:18GMTContentType:text/plainContentLength:11CacheControl:maxage=60
HelloWorld
HTTP Response
[
200,
{'ContentType'=>'text/plain','ContentLength'=>'11','CacheControl'=>'maxage=60'},["HelloWorld"]]
Rack Response
[200,{'ContentType'=>'text/plain',
'ContentLength'=>File.size('hello.txt').to_s,'CacheControl'=>'maxage=60'},File.open('hello.txt','rb')]
OR
7/31/2019 Rack in Rails 3 Presentation
12/38
7/31/2019 Rack in Rails 3 Presentation
13/38
classHelloWorldAppdefcall(env)
[200,{'ContentType'=>'text/plain'},["HelloWorld"]]end
endapp=HelloWorldApp.new
7/31/2019 Rack in Rails 3 Presentation
14/38
classHelloWorldAppdefcall(env)
[200,{'ContentType'=>'text/plain'},["HelloWorld"]]end
endapp=HelloWorldApp.new
app=lambda{|env|[200,{'ContentType'=>'text/plain'},["HelloWorld"]]}
OR
7/31/2019 Rack in Rails 3 Presentation
15/38
We haven't usedany Rack APIs yet.
7/31/2019 Rack in Rails 3 Presentation
16/38
Interoperability
WEBrick
CGIFastCGI
Mongrel
ThinPassenger
Unicorn
Heroku
Rails
SinatraMerb
Ramaze
CampingCoset
Rango
Just Rack
Servers Frameworks
Rack
SPEC
7/31/2019 Rack in Rails 3 Presentation
17/38
Interoperability
WEBrick
CGIFastCGI
Mongrel
ThinPassenger
Unicorn
Heroku
Rails
SinatraMerb
Ramaze
CampingCoset
Rango
Just Rack
Servers Frameworks
Rack
SPEC
7/31/2019 Rack in Rails 3 Presentation
18/38
Architecture ofIntermediaries
7/31/2019 Rack in Rails 3 Presentation
19/38
HTTP
Intermediaries
CompanyProxy
Alice
Bob
Carol
HTTP
HTTP
HTTP
HTTP
HTTP
Auth+Logging
Compression
R
egionalProxy
LoadBalancing App 1
App 2
HTTP
HTTP
HTTP
HTTP
HTTP
HTTP
separate process
inside example.com firewall
7/31/2019 Rack in Rails 3 Presentation
20/38
HTTP
Intermediaries
CompanyProxy
Alice
Bob
Carol
HTTP
HTTP
HTTP
HTTP
HTTP
Auth+Logging
Compression
R
egionalProxy
LoadBalancing App 1
App 2
HTTP
HTTP
HTTP
HTTP
HTTP
HTTP
separate process
inside example.com firewall
7/31/2019 Rack in Rails 3 Presentation
21/38
classShoutMiddlewaredefinitialize(app)@app=append
defcall(env)
status,headers,[email protected](env)parts=[]body.each{|part|parts
7/31/2019 Rack in Rails 3 Presentation
22/38
classShoutMiddlewaredefinitialize(app)@app=append
defcall(env)
status,headers,[email protected](env)parts=[]body.each{|part|parts8080)
7/31/2019 Rack in Rails 3 Presentation
23/38
Rack::ETagrequire'digest/md5'moduleRackclassETagdefinitialize(app)@app=append
defcall(env)status,headers,[email protected](env)if!headers.has_key?('ETag')parts=[]body.each{|part|parts
7/31/2019 Rack in Rails 3 Presentation
24/38
Rack Middleware
Rack::Chunked
Rack::ContentLength
Rack::ConditionalGetRack::ContentType
Rack::Deflater
Rack::ETag
Rack::Head
Rack::MethodOverrideRack::Runtime
Rack::Sendfile
Rack::ShowStatus
Rack::CommonLogger
Rack::Lint
Rack::LockRack::Reloader
Rack::CascadeRack::Recursive
Rack::Static
Rack::URLMap
Routing
BehavioralContent Modifying
http://github.com/rack/rack/tree/master/lib/rackhttp://github.com/rack/rack/tree/master/lib/rack7/31/2019 Rack in Rails 3 Presentation
25/38
Rack::Contrib
Rack::AcceptFormat
Rack::Access
Rack::Backstage
Rack::Callbacks
Rack::Config
Rack::Cookies
Rack::CSSHTTPRequest
Rack::Deflect
Rack::EvilRack::HostMeta
Rack::JSONP
Rack::LighttpdScriptNameFix
Rack::Locale
Rack::MailExceptions
Rack::NestedParams
Rack::NotFound
Rack::ProcTitle
Rack::Profiler
Rack::ResponseCache
Rack::ResponseHeaders
Rack::RelativeRedirect
Rack::SignalsRack::StaticCache
Rack::SimpleEndpoint
Rack::TimeZone
http://github.com/rack/rack-contribhttp://github.com/rack/rack-contrib7/31/2019 Rack in Rails 3 Presentation
26/38
http://coderack.org/http://coderack.org/7/31/2019 Rack in Rails 3 Presentation
27/38
http://coderack.org/http://coderack.org/7/31/2019 Rack in Rails 3 Presentation
28/38
7/31/2019 Rack in Rails 3 Presentation
29/38
ActionDispatch
Middleware extracted from ActionController
ActionDispatch::MiddlewareStack
Request/Response objects
Routing
Integration Testing Support
http://github.com/rails/rails/tree/master/actionpack/lib/action_dispatchhttp://github.com/rails/rails/tree/master/actionpack/lib/action_dispatch7/31/2019 Rack in Rails 3 Presentation
30/38
$rakemiddlewareuseActionDispatch::Static
useRack::Lock
useRack::Runtime
useRails::Rack::Logger
useActionDispatch::ShowExceptionsuseActionDispatch::Callbacks
useActionDispatch::Cookies
useActionDispatch::Session::CookieStore
useActionDispatch::Flash
useActionDispatch::ParamsParser
useRack::MethodOverrideuseActionDispatch::Head
useActiveRecord::ConnectionAdapters::ConnectionManagement
useActiveRecord::QueryCache
runHelloworld::Application.routes
Default Middleware
7/31/2019 Rack in Rails 3 Presentation
31/38
AD::MiddlewareStackrequire'rack/cache'require'rack/contrib'
moduleHelloworldclassApplicationtrue
#addsthenewmiddlewarebeforeanexistingmiddleware
config.middleware.insert_beforeRack::Lock,Rack::ProcTitle
#addsthenewmiddlewareafteranexistingmiddleware
config.middleware.insert_afterRack::Head,Rack::CSSHTTPRequest
#swaponemiddlewareforanother
config.middleware.swapActionDispatch::ShowExceptions,SuperShowExceptions
#removeamiddlewarefromthestack
config.middleware.deleteRack::MethodOverrideend
end
config/application.rb
7/31/2019 Rack in Rails 3 Presentation
32/38
Middleware Plugins
require"rack/cache"
moduleCacheclassRailtie"file:#{Rails.root+"cache/rack/meta"}",:entitystore=>"file:#{Rails.root+"cache/rack/body"}",:verbose=>trueendendend
## Bundle the gems you use:gem "rails-cache", :require=>"rails/cache"
rails-cache.gem/lib/rails/cache.rb
Gemfile
http://boldr.net/upgrade-plugins-gems-rails-3http://boldr.net/upgrade-plugins-gems-rails-37/31/2019 Rack in Rails 3 Presentation
33/38
Rack::Mount
require'rack/mount'
Routes=Rack::Mount::RouteSet.newdo|set|#add_routetakesarackapplicationandconditionstomatchwith
#conditionsmaybestringsorregexps#SeeRack::Mount::RouteSet#add_routeformoreoptions.
set.add_routeFooApp,:method=>'get',:path=>%{/foo}end
#Theroutesetitselfisasimplerackappyoumount
runRoutes
config.ru
http://github.com/josh/rack-mounthttp://github.com/josh/rack-mount7/31/2019 Rack in Rails 3 Presentation
34/38
Rack::Mount
require'rack/mount'
Routes=Rack::Mount::RouteSet.newdo|set|#add_routetakesarackapplicationandconditionstomatchwith
#conditionsmaybestringsorregexps#SeeRack::Mount::RouteSet#add_routeformoreoptions.
set.add_routeFooApp,:method=>'get',:path=>%{/foo}end
#Theroutesetitselfisasimplerackappyoumount
runRoutes
config.ru
http://github.com/josh/rack-mounthttp://github.com/josh/rack-mount7/31/2019 Rack in Rails 3 Presentation
35/38
Rack::Mount
require'rack/mount'
Routes=Rack::Mount::RouteSet.newdo|set|#add_routetakesarackapplicationandconditionstomatchwith
#conditionsmaybestringsorregexps#SeeRack::Mount::RouteSet#add_routeformoreoptions.
set.add_routeFooApp,:method=>'get',:path=>%{/foo}end
#Theroutesetitselfisasimplerackappyoumount
runRoutes
config.ru
http://github.com/josh/rack-mounthttp://github.com/josh/rack-mount7/31/2019 Rack in Rails 3 Presentation
36/38
Actions = Rack Apps
$railsconsoleLoadingdevelopmentenvironment(Rails3.0.0.beta)
>>action=ApplicationController.action(:index)=>#
>>action.respond_to?(:call)=>true
7/31/2019 Rack in Rails 3 Presentation
37/38
Routing to Rack
classTwitterAppTwitterAppend
lib/twitter_app.rb
config/routes.rb
http://lindsaar.net/2010/2/7/rails_3_routing_with_rackhttp://lindsaar.net/2010/2/7/rails_3_routing_with_rack7/31/2019 Rack in Rails 3 Presentation
38/38
More Rack + Rails
RailsGuides: Rails on Rack (Updated for Rails 3)
Scaling Rails: Rack & Metal (Screencast)
Rack API Documentation
Rack SPEC (Seriously)
Rack Source (On The GitHub)
http://github.com/rack/rackhttp://github.com/rack/rackhttp://rack.rubyforge.org/doc/SPEC.htmlhttp://rack.rubyforge.org/doc/SPEC.htmlhttp://rack.rubyforge.org/doc/http://rack.rubyforge.org/doc/http://railslab.newrelic.com/2009/06/05/episode-14-rack-metalhttp://railslab.newrelic.com/2009/06/05/episode-14-rack-metalhttp://guides.rails.info/rails_on_rack.htmlhttp://guides.rails.info/rails_on_rack.html