Skip to content

Commit 8c3a543

Browse files
committed
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`
1 parent c4023cb commit 8c3a543

6 files changed

Lines changed: 60 additions & 0 deletions

File tree

railties/lib/initializer.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ def process
155155
initialize_framework_settings
156156
initialize_framework_views
157157

158+
initialize_metal
159+
158160
add_support_load_paths
159161

160162
load_gems
@@ -533,6 +535,12 @@ def initialize_i18n
533535
end
534536
end
535537

538+
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
542+
end
543+
536544
# Initializes framework-specific settings for each of the loaded frameworks
537545
# (Configuration#frameworks). The available settings map to the accessors
538546
# on each of the corresponding Base classes.
@@ -915,6 +923,7 @@ def default_load_paths
915923
# Followed by the standard includes.
916924
paths.concat %w(
917925
app
926+
app/metal
918927
app/models
919928
app/controllers
920929
app/helpers
@@ -933,6 +942,7 @@ def default_load_once_paths
933942

934943
def default_eager_load_paths
935944
%w(
945+
app/metal
936946
app/models
937947
app/controllers
938948
app/helpers

railties/lib/rails/rack.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Rails
22
module Rack
33
autoload :Debugger, "rails/rack/debugger"
44
autoload :Logger, "rails/rack/logger"
5+
autoload :Metal, "rails/rack/metal"
56
autoload :Static, "rails/rack/static"
67
end
78
end

railties/lib/rails/rack/metal.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Rails
2+
module Rack
3+
class Metal
4+
NotFound = lambda { |env|
5+
[404, {"Content-Type" => "text/html"}, "Not Found"]
6+
}
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
19+
end
20+
end
21+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Description:
2+
Cast some metal!
3+
4+
Examples:
5+
`./script/generate metal poller`
6+
7+
This will create:
8+
Metal: app/metal/poller.rb
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class MetalGenerator < Rails::Generator::NamedBase
2+
def manifest
3+
record do |m|
4+
m.directory 'app/metal'
5+
m.template 'metal.rb', File.join('app/metal', "#{file_name}.rb")
6+
end
7+
end
8+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Allow the metal piece to run in isolation
2+
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
3+
4+
class <%= class_name %> < Rails::Rack::Metal
5+
def call(env)
6+
if env["PATH_INFO"] =~ /^\/<%= file_name %>/
7+
[200, {"Content-Type" => "text/html"}, "Hello, World!"]
8+
else
9+
super
10+
end
11+
end
12+
end

0 commit comments

Comments
 (0)