Skip to content

Commit 61a4115

Browse files
committed
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
1 parent 97a178b commit 61a4115

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

railties/lib/initializer.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,7 @@ def initialize_i18n
536536
end
537537

538538
def initialize_metal
539-
Dir["#{configuration.root_path}/app/metal/*.rb"].each do |file|
540-
configuration.middleware.use(File.basename(file, '.rb').camelize)
541-
end
539+
configuration.middleware.use Rails::Rack::Metal
542540
end
543541

544542
# Initializes framework-specific settings for each of the loaded frameworks

railties/lib/rails/rack/metal.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
module Rails
22
module Rack
33
class Metal
4+
def self.new(app)
5+
apps = Dir["#{Rails.root}/app/metal/*.rb"].map do |file|
6+
File.basename(file, '.rb').camelize.constantize
7+
end
8+
apps << app
9+
::Rack::Cascade.new(apps)
10+
end
11+
412
NotFound = lambda { |env|
513
[404, {"Content-Type" => "text/html"}, "Not Found"]
614
}
7-
8-
def self.call(env)
9-
new(NotFound).call(env)
10-
end
11-
12-
def initialize(app)
13-
@app = app
14-
end
15-
16-
def call(env)
17-
@app.call(env)
18-
end
1915
end
2016
end
2117
end
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Allow the metal piece to run in isolation
22
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
33

4-
class <%= class_name %> < Rails::Rack::Metal
5-
def call(env)
4+
class <%= class_name %>
5+
def self.call(env)
66
if env["PATH_INFO"] =~ /^\/<%= file_name %>/
7-
[200, {"Content-Type" => "text/html"}, "Hello, World!"]
7+
[200, {"Content-Type" => "text/html"}, ["Hello, World!"]]
88
else
9-
super
9+
[404, {"Content-Type" => "text/html"}, ["Not Found"]]
1010
end
1111
end
1212
end

0 commit comments

Comments
 (0)