提交 6ce4b430 编写于 作者: J Jeremy Kemper

Cheaper metal cascade

上级 03f6ecc6
require 'active_support/ordered_hash'
module Rails
module Rack
# Try a request on several apps; return the first non-404 response.
class Cascade
attr_reader :apps
def initialize(apps)
@apps = ActiveSupport::OrderedHash.new
apps.each { |app| add app }
end
def call(env)
@apps.keys.each do |app|
result = app.call(env)
return result unless result[0].to_i == 404
end
Metal::NotFoundResponse
end
def add(app)
@apps[app] = true
end
def include?(app)
@apps.include?(app)
end
end
end
end
require 'rails/rack/cascade'
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
module Metal
NotFoundResponse = [404, {}, []].freeze
NotFound = lambda { NotFoundResponse }
class << self
def new(app)
Cascade.new(builtins + [app])
end
apps << app
::Rack::Cascade.new(apps)
end
NotFound = lambda { |env|
[404, {"Content-Type" => "text/html"}, "Not Found"]
}
def builtins
base = "#{Rails.root}/app/metal"
matcher = /\A#{Regexp.escape(base)}\/(.*)\.rb\Z/
Dir["#{base}/**/*.rb"].sort.map do |file|
file.sub!(matcher, '\1')
require file
file.classify.constantize
end
end
end
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册