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

Minimalist Rails

Minimalist Rails

A talk about Rack, Middleware and Minimalist Rails Stacks given at [Belfast Ruby](http://belfastruby.com)


David Rice

March 28, 2012

More Decks by David Rice

Other Decks in Technology


  1. is a Web Software & Experience Design laboratory in Belfast,

    Northern Ireland http://rumblelabs.com Wednesday, 28 March 12
  2. • Rails history • Rack • Rails —’s Middleware •

    Minimalist Rails Stack • Barebones Rails Stack Minimalist Rails Wednesday, 28 March 12
  3. • 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
  4. 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
  5. 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
  6. 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
  7. 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(.*)$/, '$1' reverse_proxy /^\/api(.*)$/, '$1' end run app end run wee_rack_app con g.ru Wednesday, 28 March 12
  8. 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
  9. # 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
  10. 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
  11. 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