diff options
author | Joshua Peek <josh@joshpeek.com> | 2008-12-16 13:11:51 -0600 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2008-12-16 13:15:06 -0600 |
commit | 8c3a54366435eebc2c8aa63b63e1349ce74a7b38 (patch) | |
tree | 547451f6c639994a948f8cba6505022458aedeac | |
parent | c4023cbe206415cf3ca1ca92cd9980a4aa4aed00 (diff) | |
download | rails-8c3a54366435eebc2c8aa63b63e1349ce74a7b38.tar.gz rails-8c3a54366435eebc2c8aa63b63e1349ce74a7b38.tar.bz2 rails-8c3a54366435eebc2c8aa63b63e1349ce74a7b38.zip |
Introduce Rails Metal
# app/metal/poller.rb
class Poller < Rails::Rack::Metal
def call(env)
if env["PATH_INFO"] =~ /^\/poller/
[200, {"Content-Type" => "application/json"}, Message.recent.to_json]
else
super
end
end
end
* There is a generator to help you get started
`script/generate metal poller`
* Also, metal bits can be ran standalone with rackup
`rackup app/metal/poller.rb`
6 files changed, 60 insertions, 0 deletions
diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index 56e8ce95ab..f22e34c7dc 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -155,6 +155,8 @@ module Rails initialize_framework_settings initialize_framework_views + initialize_metal + add_support_load_paths load_gems @@ -533,6 +535,12 @@ Run `rake gems:install` to install the missing gems. end end + def initialize_metal + Dir["#{configuration.root_path}/app/metal/*.rb"].each do |file| + configuration.middleware.use(File.basename(file, '.rb').camelize) + end + end + # Initializes framework-specific settings for each of the loaded frameworks # (Configuration#frameworks). The available settings map to the accessors # on each of the corresponding Base classes. @@ -915,6 +923,7 @@ Run `rake gems:install` to install the missing gems. # Followed by the standard includes. paths.concat %w( app + app/metal app/models app/controllers app/helpers @@ -933,6 +942,7 @@ Run `rake gems:install` to install the missing gems. def default_eager_load_paths %w( + app/metal app/models app/controllers app/helpers diff --git a/railties/lib/rails/rack.rb b/railties/lib/rails/rack.rb index 90535674e9..f721abddc8 100644 --- a/railties/lib/rails/rack.rb +++ b/railties/lib/rails/rack.rb @@ -2,6 +2,7 @@ module Rails module Rack autoload :Debugger, "rails/rack/debugger" autoload :Logger, "rails/rack/logger" + autoload :Metal, "rails/rack/metal" autoload :Static, "rails/rack/static" end end diff --git a/railties/lib/rails/rack/metal.rb b/railties/lib/rails/rack/metal.rb new file mode 100644 index 0000000000..8dfbedad90 --- /dev/null +++ b/railties/lib/rails/rack/metal.rb @@ -0,0 +1,21 @@ +module Rails + module Rack + class Metal + 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/USAGE b/railties/lib/rails_generator/generators/components/metal/USAGE new file mode 100644 index 0000000000..123ec6c03f --- /dev/null +++ b/railties/lib/rails_generator/generators/components/metal/USAGE @@ -0,0 +1,8 @@ +Description: + Cast some metal! + +Examples: + `./script/generate metal poller` + + This will create: + Metal: app/metal/poller.rb diff --git a/railties/lib/rails_generator/generators/components/metal/metal_generator.rb b/railties/lib/rails_generator/generators/components/metal/metal_generator.rb new file mode 100644 index 0000000000..64f49d929d --- /dev/null +++ b/railties/lib/rails_generator/generators/components/metal/metal_generator.rb @@ -0,0 +1,8 @@ +class MetalGenerator < Rails::Generator::NamedBase + def manifest + record do |m| + m.directory 'app/metal' + m.template 'metal.rb', File.join('app/metal', "#{file_name}.rb") + 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 new file mode 100644 index 0000000000..39487263df --- /dev/null +++ b/railties/lib/rails_generator/generators/components/metal/templates/metal.rb @@ -0,0 +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) + if env["PATH_INFO"] =~ /^\/<%= file_name %>/ + [200, {"Content-Type" => "text/html"}, "Hello, World!"] + else + super + end + end +end |