From 61a41154f7d50099da371e0d2f22fd25ab9113c2 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 17 Dec 2008 09:53:56 -0600 Subject: Make generated Metal bits a pure rack endpoint application (not middleware) Instead of calling super to pass the request on, return a 404. The modified app looks like this: # app/metal/poller.rb class Poller def self.call(env) if env["PATH_INFO"] =~ /^\/poller/ [200, {"Content-Type" => "text/html"}, "Hello, World!"] else [404, {"Content-Type" => "text/html"}, "Not Found"] end end end But you aren't locked in to just Rails: # app/metal/api.rb require 'sinatra' Sinatra::Application.default_options.merge!(:run => false, :env => :production) Api = Sinatra.application unless defined? Api get '/interesting/new/ideas' do 'Hello Sinatra!' end --- railties/lib/initializer.rb | 4 +--- railties/lib/rails/rack/metal.rb | 20 ++++++++------------ .../generators/components/metal/templates/metal.rb | 8 ++++---- 3 files changed, 13 insertions(+), 19 deletions(-) (limited to 'railties') diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index f22e34c7dc..637fe74313 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -536,9 +536,7 @@ Run `rake gems:install` to install the missing gems. end def initialize_metal - Dir["#{configuration.root_path}/app/metal/*.rb"].each do |file| - configuration.middleware.use(File.basename(file, '.rb').camelize) - end + configuration.middleware.use Rails::Rack::Metal end # Initializes framework-specific settings for each of the loaded frameworks diff --git a/railties/lib/rails/rack/metal.rb b/railties/lib/rails/rack/metal.rb index 8dfbedad90..77d00ab091 100644 --- a/railties/lib/rails/rack/metal.rb +++ b/railties/lib/rails/rack/metal.rb @@ -1,21 +1,17 @@ module Rails module Rack class Metal + def self.new(app) + apps = Dir["#{Rails.root}/app/metal/*.rb"].map do |file| + File.basename(file, '.rb').camelize.constantize + end + apps << app + ::Rack::Cascade.new(apps) + end + NotFound = lambda { |env| [404, {"Content-Type" => "text/html"}, "Not Found"] } - - def self.call(env) - new(NotFound).call(env) - end - - def initialize(app) - @app = app - end - - def call(env) - @app.call(env) - end end end end diff --git a/railties/lib/rails_generator/generators/components/metal/templates/metal.rb b/railties/lib/rails_generator/generators/components/metal/templates/metal.rb index 39487263df..e94982b69a 100644 --- a/railties/lib/rails_generator/generators/components/metal/templates/metal.rb +++ b/railties/lib/rails_generator/generators/components/metal/templates/metal.rb @@ -1,12 +1,12 @@ # Allow the metal piece to run in isolation require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails) -class <%= class_name %> < Rails::Rack::Metal - def call(env) +class <%= class_name %> + def self.call(env) if env["PATH_INFO"] =~ /^\/<%= file_name %>/ - [200, {"Content-Type" => "text/html"}, "Hello, World!"] + [200, {"Content-Type" => "text/html"}, ["Hello, World!"]] else - super + [404, {"Content-Type" => "text/html"}, ["Not Found"]] end end end -- cgit v1.2.3