Slide 1

Slide 1 text

Minimalist Rails Rack, Middleware, Minimal Stacks Wednesday, 28 March 12

Slide 2

Slide 2 text

Technical Director at Rumble Labs David Rice Wednesday, 28 March 12

Slide 3

Slide 3 text

is a Web Software & Experience Design laboratory in Belfast, Northern Ireland http://rumblelabs.com Wednesday, 28 March 12

Slide 4

Slide 4 text

• Rails history • Rack • Rails —’s Middleware • Minimalist Rails Stack • Barebones Rails Stack Minimalist Rails Wednesday, 28 March 12

Slide 5

Slide 5 text

• Rails 2.3 introduced support for Rack • Rack spells the end of tying applications to a dispatcher / handler • Before then we had application dispatchers, CGI, FCGI • Communication between Rails and any web server is simpli ed Rails < 2.3 Wednesday, 28 March 12

Slide 6

Slide 6 text

Rails without CGI • Rails takes some time to boot, and CGI starts a new process for every request... • Your application needs to know about wether it runs under CGI, FCGI etc. • Applications are more portable Wednesday, 28 March 12

Slide 7

Slide 7 text

Rack • github.com/rack/rack • Rack is a modular web server interface • To connect ruby applications to the web • As of Rails 3 it and middleware are now a core concept Wednesday, 28 March 12

Slide 8

Slide 8 text

Hello Rack class HelloWorld def call(env) return [200, {}, ["Hello world!"]] end end Wednesday, 28 March 12

Slide 9

Slide 9 text

Supported Rack Servers •Mongrel •WEBrick •FCGI •CGI •SCGI •LiteSpeed •Thin •Ebb •Fuzed •Glass sh v3 Wednesday, 28 March 12

Slide 10

Slide 10 text

Deployment? • mod_rails (Apache/Nginx) • Reverse proxy (Apache/Nginx, HAProxy, Varnish) • JRuby (Jetty/Glass sh) Wednesday, 28 March 12

Slide 11

Slide 11 text

Middleware Wednesday, 28 March 12

Slide 12

Slide 12 text

Web Server Wednesday, 28 March 12

Slide 13

Slide 13 text

Wednesday, 28 March 12

Slide 14

Slide 14 text

Wednesday, 28 March 12

Slide 15

Slide 15 text

Rails —’s Middleware use ActionDispatch::Static use Rack::Lock use ActiveSupport::Cache::Strategy::LocalCache::Middleware use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use ActionDispatch::Head use Rack::ConditionalGet use Rack::ETag use ActionDispatch::BestStandardsSupport run MyRailsApplication::Application.routes Wednesday, 28 March 12

Slide 16

Slide 16 text

Moar Middleware https://github.com/rack/rack/wiki/List-of-Middleware http://coderack.org/middlewares • Rack::Rewrite • Warden • Rack::ReverseProxy Wednesday, 28 March 12

Slide 17

Slide 17 text

require 'rubygems' require 'bundler' Bundler.require app = proc do |env| Rack::Directory.new('.').call(env) end wee_rack_app = Rack::Builder.new do use Rack::ContentLength use Rack::ReverseProxy do reverse_proxy /^\/couchdb(.*)$/, 'http://127.0.0.1:5984$1' reverse_proxy /^\/api(.*)$/, 'http://127.0.0.1:4000$1' end run app end run wee_rack_app con g.ru Wednesday, 28 March 12

Slide 18

Slide 18 text

Minimal Rails Dependencies Wednesday, 28 March 12

Slide 19

Slide 19 text

require File.expand_path('../boot', __FILE__) # require 'rails/all' # # require "active_record/railtie" require "action_controller/railtie" #require "action_mailer/railtie" #require "active_resource/railtie" #require "rails/test_unit/railtie" if defined?(Bundler) # If you precompile assets before deploying to production, use this line # Bundler.require *Rails.groups(:assets => %w(development test)) # If you want your assets lazily compiled in production, use this line Bundler.require(:default, :assets, Rails.env) end module CouchbaseTinyurl class Application < Rails::Application # ... # Configure the default encoding used in templates for Ruby 1.9. config.encoding = "utf-8" # Configure sensitive parameters which will be filtered from the log file. config.filter_parameters += [:password] # Enable the asset pipeline config.assets.enabled = true end end application.rb Wednesday, 28 March 12

Slide 20

Slide 20 text

# This file is used by Rack-based servers to start the application. require ::File.expand_path('../config/environment', __FILE__) run CouchbaseTinyurl::Application con g.ru Wednesday, 28 March 12

Slide 21

Slide 21 text

Minimalist Rails App Wednesday, 28 March 12

Slide 22

Slide 22 text

require "rails" require "rails/all" class MyApp < Rails::Application routes.append do match "/hello/world" => "hello#world" end config.cache_classes = true # Enable cache classes. Production style. config.middleware.delete "Rack::Lock" config.middleware.delete "ActionDispatch::Flash" config.middleware.delete "ActionDispatch::BestStandardsSupport" config.secret_token = "49837489qkuweoiuoqwehisuakshdjksadhaisdy78o34y138974xyqp9rmye8yrpiokeuioqwzyoiuxftoyqiuxrhm3iou1hrzmjk" end class HelloController < ActionController::Metal include ActionController::Rendering def world render :text => "Hello world!" end end MyApp.initialize! run MyApp con g.ru Wednesday, 28 March 12

Slide 23

Slide 23 text

Minimal Middleware Stack Wednesday, 28 March 12

Slide 24

Slide 24 text

ActionController::Dispatcher.middleware = ActionController::MiddlewareStack.new do |m| m.use ActionController::Failsafe m.use ActiveRecord::QueryCache m.use Rack::Head end con g/initializers/stack.rb Wednesday, 28 March 12

Slide 25

Slide 25 text

The Future • Rails 4 will have an API only middleware stack con guration • RocketPants (https://github.com/ ltersquad/rocket_pants) is an interesting alternative now Wednesday, 28 March 12

Slide 26

Slide 26 text

• https://github.com/rails/rails • https://github.com/rack/rack • http://guides.rubyonrails.org/rails_on_rack.html • http://piotrsarnacki.com/2010/12/12/lightweight-controllers-with-rails3/ • http://lenary.co.uk/rails/2010/07/middleware-part-1/ • http://lenary.co.uk/rails/2010/07/middleware-part-2/ • https://gist.github.com/1942658 Further Reading Wednesday, 28 March 12

Slide 27

Slide 27 text

@davidjrice Thanks! Wednesday, 28 March 12