15. Metal Endpoint
## app/metal/hello_metal.rb
class HelloMetal
def self.call(env)
if env[quot;PATH_INFOquot;] =~ /^/hello/metal/
[200, {quot;Content-Typequot; => quot;text/plainquot;}, [quot;Hello, Metal!quot;]]
else
[404, {quot;Content-Typequot; => quot;text/htmlquot;}, [quot;Not Foundquot;]]
end
end
end
http://soylentfoo.jnewland.com/articles/2008/12/16/rails-metal-a-micro-framework-with-the-power-of-rails-m
16. Equivalent Controller
## app/controllers/hello_rails_controller.rb
class HelloRailsController < ApplicationController
def show
headers['Content-Type'] = 'text/plain'
render :text => 'Hello, Rails!'
end
end
http://soylentfoo.jnewland.com/articles/2008/12/16/rails-metal-a-micro-framework-with-the-power-of-rails-m
17. Sinatra!
require 'sinatra'
Sinatra::Application.set(:run => false)
Sinatra::Application.set(:environment => :production)
HelloSinatra = Sinatra::Application.new unless defined? HelloSinatra
get '/hello/sinatra' do
response['Content-Type'] = 'text/plain'
'Hello, Sinatra!'
end
http://soylentfoo.jnewland.com/articles/2008/12/16/rails-metal-a-micro-framework-with-the-power-of-rails-m
19. Metal Endpoint
## app/metal/hello_metal.rb
class HelloMetal
def self.call(env)
if env[quot;PATH_INFOquot;] =~ /^/hello/metal/
[200, {quot;Content-Typequot; => quot;text/plainquot;}, [quot;Hello, Metal!quot;]]
else
[404, {quot;Content-Typequot; => quot;text/htmlquot;}, [quot;Not Foundquot;]]
end
end
end
20. Rack Middleware in Rails
Rails Dispatcher
call(env) [s,h,r]
Middleware
call(env) [s,h,r]
Middleware
call(env) [s,h,r]
Web Server
21. Rack Middleware in Rails
% rake middleware
use Rack::Lock
use ActionController::Failsafe
use ActionController::Session::CookieStore
use Rails::Rack::Metal
use ActionController::RewindableInput
use ActionController::ParamsParser
use Rack::MethodOverride
use Rack::Head
use ActiveRecord::QueryCache
run ActionController::Dispatcher.new
22. Rack Middleware in Rails
% rake middleware
use Rack::Lock
use ActionController::Failsafe
use ActionController::Session::CookieStore
use Rails::Rack::Metal
use ActionController::RewindableInput
use ActionController::ParamsParser
use Rack::MethodOverride
use Rack::Head
use ActiveRecord::QueryCache
run ActionController::Dispatcher.new
23. Django > Rails?
## lib/middleware/django_middleware.rb
class DjangoMiddleware
def initialize(app)
@app = app
end
def call(env)
status, headers, response = @app.call(env)
new_response = []
response.each do |part|
new_response << part.gsub(/Rails/, 'Django')
end
[status, headers, new_response]
end
end
## config/environment.rb
config.middleware.use DjangoMiddleware