Share
Embed
Transcript
83 slides
-
paulh Paul Hinze jbelser Jeff Belser liz Liz Abinante nomitch
Mike Nomitch jstern Jennifer Stern z Mike Ziwisky illstructure -
"Write programs to handle text streams, because that is a
universal interface." -
cat /var/log/system.log | grep '^Jan' | awk '{ print $5
}' | sort | uniq -c -
cat /var/log/system.log | grep '^Jan' | awk '{ print $5
}' | sort | uniq -c | sort -n -
an Object that responds to #call #call accepts one Hash
argument #call returns an Array with [status, headers, body] where and -
app = ->(env) { [200, {}, 'Hello, World!'] } !
Rack::Handler::Thin.run(app) It's Simple! -
app = ->(env) { [200, {}, 'Hello, World!'] } Rack::Handler.const_get(
%i[ Thin WEBrick Unicorn Puma ].sample ).run(app) Any Server -
class YellIt def initialize(app) @app = app end ! def
call(env) status, headers, body = @app.call(env) [status, headers, body.upcase] end end ! app = ->(env) {[200, {}, 'Hello, World!']} ! Rack::Handler::Thin.run(YellIt.new(app)) -
class YellIt def initialize(app) @app = app end ! def
call(env) status, headers, body = @app.call(env) [status, headers, body.upcase] end end ! app = ->(env) {[200, {}, 'Hello, World!']} ! Rack::Handler::Thin.run(YellIt.new(app)) -
class YellIt def initialize(app) @app = app end ! def
call(env) status, headers, body = @app.call(env) [status, headers, body.upcase] end end ! app = ->(env) {[200, {}, 'Hello, World!']} ! Rack::Handler::Thin.run(YellIt.new(app)) -
class YellIt def initialize(app) @app = app end ! def
call(env) status, headers, body = @app.call(env) [status, headers, body.upcase] end end ! app = ->(env) {[200, {}, 'Hello, World!']} ! Rack::Handler::Thin.run(YellIt.new(app)) -
class YellIt def initialize(app) @app = app end ! def
call(env) status, headers, body = @app.call(env) [status, headers, body.upcase] end end ! app = ->(env) {[200, {}, 'Hello, World!']} ! Rack::Handler::Thin.run(YellIt.new(app)) -
class YellIt def initialize(app) @app = app end ! def
call(env) status, headers, body = @app.call(env) [status, headers, body.upcase] end end ! app = ->(env) {[200, {}, 'Hello, World!']} ! Rack::Handler::Thin.run(YellIt.new(app)) -
class YellIt def initialize(app) @app = app end ! def
call(env) status, headers, body = @app.call(env) [status, headers, body.upcase] end end ! app = ->(env) {[200, {}, 'Hello, World!']} ! Rack::Handler::Thin.run(YellIt.new(app)) -
class Rack::Logger def initialize(app, logger=Logger.new(STDOUT)) @app = app @logger =
logger end ! def call(env) status, headers, body = @app.call(env) @logger.info("[#{Time.now}] #{status}") [status, headers, body] end end A logger -
class Rack::ContentLength def initialize(app) @app = app end ! def
call(env) status, headers, body = @app.call(env) ! if !headers['Content-Length'] headers['Content-Length'] = body.length end ! [status, headers, body] end end A header -
class Rack::Runtime def initialize(app) @app = app end ! def
call(env) start_time = Time.now status, headers, body = @app.call(env) request_time = Time.now - start_time ! headers['X-Runtime'] = "%0.6f" % request_time ! [status, headers, body] end end A timer -
class Rack::Auth::Basic def initialize(app, user, pass) @app, @user, @pass =
app, user, pass end ! def call(env) if authenticated?(env['HTTP_AUTHORIZATION']) @app.call(env) else [403, {}, "Go Away!"] end end ! def authenticated?(auth) return false unless auth _, token = auth.split(' ') user, pass = token.unpack('m*').first.split(':') (user == @user && pass == @pass) end end ! app = ->(env) {[200, {}, 'Hello, World!']} with_auth = Rack::Auth::Basic.new(app, 'paulh', 'hello') Rack::Handler::Thin.run(with_auth) A auth! -
class Rack::Auth::Basic def initialize(app, user, pass) @app, @user, @pass =
app, user, pass end ! def call(env) if authenticated?(env['HTTP_AUTHORIZATION']) @app.call(env) else [403, {}, "Go Away!"] end end ! def authenticated?(auth) return false unless auth _, token = auth.split(' ') user, pass = token.unpack('m*').first.split(':') (user == @user && pass == @pass) end end ! app = ->(env) {[200, {}, 'Hello, World!']} with_auth = Rack::Auth::Basic.new(app, 'paulh', 'hello') Rack::Handler::Thin.run(with_auth) -
class Rack::Auth::Basic def initialize(app, user, pass) @app, @user, @pass =
app, user, pass end ! def call(env) if authenticated?(env['HTTP_AUTHORIZATION']) @app.call(env) else [403, {}, "Go Away!"] end end ! def authenticated?(auth) return false unless auth _, token = auth.split(' ') user, pass = token.unpack('m*').first.split(':') (user == @user && pass == @pass) end end ! app = ->(env) {[200, {}, 'Hello, World!']} with_auth = Rack::Auth::Basic.new(app, 'paulh', 'hello') Rack::Handler::Thin.run(with_auth) -
class Rack::Auth::Basic def initialize(app, user, pass) @app, @user, @pass =
app, user, pass end ! def call(env) if authenticated?(env['HTTP_AUTHORIZATION']) @app.call(env) else [403, {}, "Go Away!"] end end ! def authenticated?(auth) return false unless auth _, token = auth.split(' ') user, pass = token.unpack('m*').first.split(':') (user == @user && pass == @pass) end end ! app = ->(env) {[200, {}, 'Hello, World!']} with_auth = Rack::Auth::Basic.new(app, 'paulh', 'hello') Rack::Handler::Thin.run(with_auth) -
class Rack::Auth::Basic def initialize(app, user, pass) @app, @user, @pass =
app, user, pass end ! def call(env) if authenticated?(env['HTTP_AUTHORIZATION']) @app.call(env) else [403, {}, "Go Away!"] end end ! def authenticated?(auth) return false unless auth _, token = auth.split(' ') user, pass = token.unpack('m*').first.split(':') (user == @user && pass == @pass) end end ! app = ->(env) {[200, {}, 'Hello, World!']} with_auth = Rack::Auth::Basic.new(app, 'paulh', 'hello') Rack::Handler::Thin.run(with_auth) -
class Rack::Auth::Basic def initialize(app, user, pass) @app, @user, @pass =
app, user, pass end ! def call(env) if authenticated?(env['HTTP_AUTHORIZATION']) @app.call(env) else [403, {}, "Go Away!"] end end ! def authenticated?(auth) return false unless auth _, token = auth.split(' ') user, pass = token.unpack('m*').first.split(':') (user == @user && pass == @pass) end end ! app = ->(env) {[200, {}, 'Hello, World!']} with_auth = Rack::Auth::Basic.new(app, 'paulh', 'hello') Rack::Handler::Thin.run(with_auth) -
class Rack::Auth::Basic def initialize(app, user, pass) @app, @user, @pass =
app, user, pass end ! def call(env) if authenticated?(env['HTTP_AUTHORIZATION']) @app.call(env) else [403, {}, "Go Away!"] end end ! def authenticated?(auth) return false unless auth _, token = auth.split(' ') user, pass = token.unpack('m*').first.split(':') (user == @user && pass == @pass) end end ! app = ->(env) {[200, {}, 'Hello, World!']} with_auth = Rack::Auth::Basic.new(app, 'paulh', 'hello') Rack::Handler::Thin.run(with_auth) Our app doesn't need to care about Authentication -
class Rack::Auth::Basic def initialize(app, user, pass) @app, @user, @pass =
app, user, pass end ! def call(env) if authenticated?(env['HTTP_AUTHORIZATION']) @app.call(env) else [403, {}, "Go Away!"] end end ! def authenticated?(auth) return false unless auth _, token = auth.split(' ') user, pass = token.unpack('m*').first.split(':') (user == @user && pass == @pass) end end ! app = ->(env) {[200, {}, 'Hello, World!']} with_auth = Rack::Auth::Basic.new(app, 'paulh', 'hello') Rack::Handler::Thin.run(with_auth) Amazing! -
class Rack::Builder def initialize(app, &block) @use = [] @app =
app instance_eval(&block) end ! def use(middleware) @use.push ->(app) { middleware.new(app) } end ! def call(env) @use.reverse.inject(@app) { |app, middleware_builder| middleware_builder.call(app) }.call(env) end end ! built = Rack::Builder.new(app) do use Rack::Logger use Rack::ContentLength use Rack::Runtime end ! Rack::Handler::Thin.run(built) -
def self.action_boot Vagrant::Action::Builder.new.tap do |b| b.use CheckAccessible b.use CleanMachineFolder b.use
SetName b.use ClearForwardedPorts b.use Provision b.use EnvSet, :port_collision_repair => true b.use PrepareForwardedPortCollisionParams b.use HandleForwardedPortCollisions b.use PrepareNFSValidIds b.use SyncedFolderCleanup b.use SyncedFolders b.use PrepareNFSSettings b.use ClearNetworkInterfaces b.use Network b.use ForwardPorts b.use SetHostname b.use SaneDefaults b.use Customize, "pre-boot" b.use Boot b.use Customize, "post-boot" b.use WaitForCommunicator, [:starting, :running] b.use CheckGuestAdditions end end -
def self.action_boot Vagrant::Action::Builder.new.tap do |b| b.use CheckAccessible b.use CleanMachineFolder b.use
SetName b.use ClearForwardedPorts b.use Provision b.use EnvSet, :port_collision_repair => true b.use PrepareForwardedPortCollisionParams b.use HandleForwardedPortCollisions b.use PrepareNFSValidIds b.use SyncedFolderCleanup b.use SyncedFolders b.use PrepareNFSSettings b.use ClearNetworkInterfaces b.use Network b.use ForwardPorts b.use SetHostname b.use SaneDefaults b.use Customize, "pre-boot" b.use Boot b.use Customize, "post-boot" b.use WaitForCommunicator, [:starting, :running] b.use CheckGuestAdditions end end Extensibility! -
def self.action_boot Vagrant::Action::Builder.new.tap do |b| b.use CheckAccessible b.use CleanMachineFolder b.use
SetName b.use ClearForwardedPorts b.use Provision b.use EnvSet, :port_collision_repair => true b.use PrepareForwardedPortCollisionParams b.use HandleForwardedPortCollisions b.use PrepareNFSValidIds b.use SyncedFolderCleanup b.use SyncedFolders b.use PrepareNFSSettings b.use ClearNetworkInterfaces b.use Network b.use ForwardPorts b.use SetHostname b.use SaneDefaults b.use Customize, "pre-boot" b.use Boot b.use Customize, "post-boot" b.use WaitForCommunicator, [:starting, :running] b.use CheckGuestAdditions end end 1. 2. 3. 4. 5. 6. wait... -
def self.action_boot Vagrant::Action::Builder.new.tap do |b| b.use CheckAccessible b.use CleanMachineFolder b.use
SetName b.use ClearForwardedPorts b.use Provision b.use EnvSet, :port_collision_repair => true b.use PrepareForwardedPortCollisionParams b.use HandleForwardedPortCollisions b.use PrepareNFSValidIds b.use SyncedFolderCleanup b.use SyncedFolders b.use PrepareNFSSettings b.use ClearNetworkInterfaces b.use Network b.use ForwardPorts b.use SetHostname b.use SaneDefaults b.use Customize, "pre-boot" b.use Boot b.use Customize, "post-boot" b.use WaitForCommunicator, [:starting, :running] b.use CheckGuestAdditions end end Complexity escalate quickly can -
B def self.action_boot Vagrant::Action::Builder.new.tap do |b| b.use CheckAccessible b.use CleanMachineFolder
b.use SetName b.use ClearForwardedPorts b.use Provision b.use EnvSet, :port_collision_repair => true b.use PrepareForwardedPortCollisionParams b.use HandleForwardedPortCollisions b.use PrepareNFSValidIds b.use SyncedFolderCleanup b.use SyncedFolders b.use PrepareNFSSettings b.use ClearNetworkInterfaces b.use Network b.use ForwardPorts b.use SetHostname b.use SaneDefaults b.use Customize, "pre-boot" b.use Boot b.use Customize, "post-boot" b.use WaitForCommunicator, [:starting, :running] b.use CheckGuestAdditions end end A