提交 8c3a5436 编写于 作者: J Joshua Peek

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`
上级 c4023cbe
......@@ -155,6 +155,8 @@ def process
initialize_framework_settings
initialize_framework_views
initialize_metal
add_support_load_paths
load_gems
......@@ -533,6 +535,12 @@ def initialize_i18n
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 @@ def default_load_paths
# Followed by the standard includes.
paths.concat %w(
app
app/metal
app/models
app/controllers
app/helpers
......@@ -933,6 +942,7 @@ def default_load_once_paths
def default_eager_load_paths
%w(
app/metal
app/models
app/controllers
app/helpers
......
......@@ -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
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
Description:
Cast some metal!
Examples:
`./script/generate metal poller`
This will create:
Metal: app/metal/poller.rb
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
# 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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册