Share
Embed
Minimalist Rails
A talk about Rack, Middleware and Minimalist Rails Stacks given at [Belfast Ruby](http://belfastruby.com)
http://speakerrate.com/events/1290-belfast-ruby-2
David Rice
March 28, 2012
Transcript
27 slides
-
is a Web Software & Experience Design laboratory in Belfast,
Northern Ireland http://rumblelabs.com Wednesday, 28 March 12 -
• Rails history • Rack • Rails —’s Middleware •
Minimalist Rails Stack • Barebones Rails Stack Minimalist Rails Wednesday, 28 March 12 -
• 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 -
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 -
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 -
Hello Rack class HelloWorld def call(env) return [200, {}, ["Hello
world!"]] end end Wednesday, 28 March 12 -
Supported Rack Servers •Mongrel •WEBrick •FCGI •CGI •SCGI •LiteSpeed •Thin
•Ebb •Fuzed •Glass sh v3 Wednesday, 28 March 12 -
Deployment? • mod_rails (Apache/Nginx) • Reverse proxy (Apache/Nginx, HAProxy, Varnish)
• JRuby (Jetty/Glass sh) Wednesday, 28 March 12 -
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 -
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 -
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 -
# 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 -
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 -
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 -
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 -
• 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