diff options
-rw-r--r-- | railties/lib/rails/commands/server.rb | 1 | ||||
-rw-r--r-- | railties/lib/rails/configuration.rb | 7 | ||||
-rw-r--r-- | railties/lib/rails/generators/rails/app/templates/config/environments/production.rb | 6 | ||||
-rw-r--r-- | railties/lib/rails/initializer.rb | 7 | ||||
-rw-r--r-- | railties/lib/rails/rack/static.rb | 4 | ||||
-rw-r--r-- | railties/test/application/load_test.rb | 1 | ||||
-rw-r--r-- | railties/test/rack_static_test.rb | 2 |
7 files changed, 21 insertions, 7 deletions
diff --git a/railties/lib/rails/commands/server.rb b/railties/lib/rails/commands/server.rb index a600e1e9db..c138cbc9bf 100644 --- a/railties/lib/rails/commands/server.rb +++ b/railties/lib/rails/commands/server.rb @@ -57,7 +57,6 @@ RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV) app = Rack::Builder.new { use Rails::Rack::LogTailer unless options[:detach] use Rails::Rack::Debugger if options[:debugger] - use Rails::Rack::Static run ActionDispatch::Utils.parse_config(options[:config]) }.to_app diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 2b362a9c50..f6159b9347 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -10,7 +10,7 @@ module Rails :log_path, :log_level, :logger, :preload_frameworks, :database_configuration_file, :cache_store, :time_zone, :view_path, :metals, :controller_paths, :routes_configuration_file, - :eager_load_paths, :dependency_loading, :paths + :eager_load_paths, :dependency_loading, :paths, :serve_static_assets def initialize set_root_path! @@ -35,6 +35,7 @@ module Rails @controller_paths = default_controller_paths @routes_configuration_file = default_routes_configuration_file @database_configuration_file = default_database_configuration_file + @serve_static_assets = default_serve_static_assets for framework in default_frameworks self.send("#{framework}=", Rails::OrderedOptions.new) @@ -225,6 +226,10 @@ module Rails i18n end + def default_serve_static_assets + true + end + # Adds a single Gem dependency to the rails application. By default, it will require # the library with the same name as the gem. Use :lib to specify a different name. # diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb index 1fc9f6b923..377b9207c7 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb @@ -17,6 +17,10 @@ config.action_controller.perform_caching = true # Use a different cache store in production # config.cache_store = :mem_cache_store +# Disable Rails's static asset server +# In production, Apache or nginx will already do this +config.serve_static_assets = false + # Enable serving of images, stylesheets, and javascripts from an asset server # config.action_controller.asset_host = "http://assets.example.com" @@ -24,4 +28,4 @@ config.action_controller.perform_caching = true # config.action_mailer.raise_delivery_errors = false # Enable threaded mode -# config.threadsafe!
\ No newline at end of file +# config.threadsafe! diff --git a/railties/lib/rails/initializer.rb b/railties/lib/rails/initializer.rb index 3065a8d111..e6f42f643c 100644 --- a/railties/lib/rails/initializer.rb +++ b/railties/lib/rails/initializer.rb @@ -269,6 +269,13 @@ module Rails end end + # Include middleware to serve up static assets + Initializer.default.add :initialize_static_server do + if configuration.frameworks.include?(:action_controller) && configuration.serve_static_assets + configuration.middleware.insert(0, Rails::Rack::Static, Rails.public_path) + end + end + Initializer.default.add :initialize_cache do unless defined?(RAILS_CACHE) silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(configuration.cache_store) } diff --git a/railties/lib/rails/rack/static.rb b/railties/lib/rails/rack/static.rb index f07c6beb5e..25b6cadbfc 100644 --- a/railties/lib/rails/rack/static.rb +++ b/railties/lib/rails/rack/static.rb @@ -5,9 +5,9 @@ module Rails class Static FILE_METHODS = %w(GET HEAD).freeze - def initialize(app) + def initialize(app, root) @app = app - @file_server = ::Rack::File.new(File.join(RAILS_ROOT, "public")) + @file_server = ::Rack::File.new(root) end def call(env) diff --git a/railties/test/application/load_test.rb b/railties/test/application/load_test.rb index 75dbf56a21..47e425a824 100644 --- a/railties/test/application/load_test.rb +++ b/railties/test/application/load_test.rb @@ -17,7 +17,6 @@ module ApplicationTests test "running Rails::Application.load on the path returns a (vaguely) useful application" do app_file "config.ru", <<-CONFIG require File.dirname(__FILE__) + '/config/environment' - use Rails::Rack::Static run ActionController::Dispatcher.new CONFIG diff --git a/railties/test/rack_static_test.rb b/railties/test/rack_static_test.rb index ad673f6f19..695b011d03 100644 --- a/railties/test/rack_static_test.rb +++ b/railties/test/rack_static_test.rb @@ -15,7 +15,7 @@ class RackStaticTest < ActiveSupport::TestCase DummyApp = lambda { |env| [200, {"Content-Type" => "text/plain"}, ["Hello, World!"]] } - App = Rails::Rack::Static.new(DummyApp) + App = Rails::Rack::Static.new(DummyApp, "#{RAILS_ROOT}/public") test "serves dynamic content" do assert_equal "Hello, World!", get("/nofile") |