From 2d04bdd86fb4a9c69e1ca1ffe92188a9ca4f88c8 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Fri, 19 Dec 2014 14:16:29 -0800 Subject: [PATCH] Merge pull request #18100 from chancancode/serve_static_files Allow static asset serving from env variable (enhanced!) Conflicts: railties/CHANGELOG.md --- guides/source/asset_pipeline.md | 5 +- guides/source/configuring.md | 2 +- guides/source/rails_on_rack.md | 2 +- .../lib/rails/application/configuration.rb | 25 +++++++- .../application/default_middleware_stack.rb | 2 +- .../config/environments/production.rb.tt | 5 +- .../templates/config/environments/test.rb.tt | 4 +- railties/test/application/assets_test.rb | 2 +- .../test/application/configuration_test.rb | 58 ++++++++++++++++++- .../application/middleware/sendfile_test.rb | 2 +- railties/test/application/middleware_test.rb | 4 +- railties/test/railties/engine_test.rb | 2 +- 12 files changed, 95 insertions(+), 18 deletions(-) diff --git a/guides/source/asset_pipeline.md b/guides/source/asset_pipeline.md index 8764546873..156daf1bac 100644 --- a/guides/source/asset_pipeline.md +++ b/guides/source/asset_pipeline.md @@ -167,9 +167,8 @@ directory. Files in this directory are served by the Sprockets middleware. Assets can still be placed in the `public` hierarchy. Any assets under `public` will be served as static files by the application or web server when -`config.serve_static_assets` is set to true. You should use -`app/assets` for files that must undergo some pre-processing before they are -served. +`config.serve_static_files` is set to true. You should use `app/assets` for +files that must undergo some pre-processing before they are served. In production, Rails precompiles these files to `public/assets` by default. The precompiled copies are then served as static assets by the web server. The files diff --git a/guides/source/configuring.md b/guides/source/configuring.md index 7688962c01..ee46533e16 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -120,7 +120,7 @@ numbers. New applications filter out passwords by adding the following `config.f * `secrets.secret_key_base` is used for specifying a key which allows sessions for the application to be verified against a known secure key to prevent tampering. Applications get `secrets.secret_key_base` initialized to a random key present in `config/secrets.yml`. -* `config.serve_static_assets` configures Rails itself to serve static assets. Defaults to true, but in the production environment is turned off as the server software (e.g. NGINX or Apache) used to run the application should serve static assets instead. Unlike the default setting set this to true when running (absolutely not recommended!) or testing your app in production mode using WEBrick. Otherwise you won't be able use page caching and requests for files that exist regularly under the public directory will anyway hit your Rails app. +* `config.serve_static_files` configures Rails itself to serve static files. Defaults to true, but in the production environment is turned off as the server software (e.g. NGINX or Apache) used to run the application should serve static assets instead. Unlike the default setting set this to true when running (absolutely not recommended!) or testing your app in production mode using WEBrick. Otherwise you won't be able use page caching and requests for files that exist regularly under the public directory will anyway hit your Rails app. * `config.session_store` is usually set up in `config/initializers/session_store.rb` and specifies what class to use to store the session. Possible values are `:cookie_store` which is the default, `:mem_cache_store`, and `:disabled`. The last one tells Rails not to deal with sessions. Custom session stores can also be specified: diff --git a/guides/source/rails_on_rack.md b/guides/source/rails_on_rack.md index 0dec0e139b..042ebde9bb 100644 --- a/guides/source/rails_on_rack.md +++ b/guides/source/rails_on_rack.md @@ -233,7 +233,7 @@ Much of Action Controller's functionality is implemented as Middlewares. The fol **`ActionDispatch::Static`** -* Used to serve static assets. Disabled if `config.serve_static_assets` is `false`. +* Used to serve static files. Disabled if `config.serve_static_files` is `false`. **`Rack::Lock`** diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 268ef2c7aa..fdc741dd08 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -1,5 +1,7 @@ require 'active_support/core_ext/kernel/reporting' +require 'active_support/core_ext/string/filters' require 'active_support/file_update_checker' +require 'active_support/deprecation' require 'rails/engine/configuration' require 'rails/source_annotation_extractor' @@ -11,7 +13,7 @@ class Configuration < ::Rails::Engine::Configuration :eager_load, :exceptions_app, :file_watcher, :filter_parameters, :force_ssl, :helpers_paths, :logger, :log_formatter, :log_tags, :railties_order, :relative_url_root, :secret_key_base, :secret_token, - :serve_static_assets, :ssl_options, :static_cache_control, :session_options, + :serve_static_files, :ssl_options, :static_cache_control, :session_options, :time_zone, :reload_classes_only_on_change, :beginning_of_week, :filter_redirect, :x @@ -25,7 +27,7 @@ def initialize(*) @filter_parameters = [] @filter_redirect = [] @helpers_paths = [] - @serve_static_assets = true + @serve_static_files = true @static_cache_control = nil @force_ssl = false @ssl_options = {} @@ -139,6 +141,25 @@ def colorize_logging=(val) self.generators.colorize_logging = val end + # :nodoc: + SERVE_STATIC_ASSETS_DEPRECATION_MESSAGE = <<-MSG.squish + The configuration option `config.serve_static_assets` has been renamed + to `config.serve_static_files` to clarify its role (it merely enables + serving everything in the `public` folder and is unrelated to the asset + pipeline). The `serve_static_assets` alias will be removed in Rails 5.0. + Please migrate your configuration files accordingly. + MSG + + def serve_static_assets + ActiveSupport::Deprecation.warn SERVE_STATIC_ASSETS_DEPRECATION_MESSAGE + serve_static_files + end + + def serve_static_assets=(value) + ActiveSupport::Deprecation.warn SERVE_STATIC_ASSETS_DEPRECATION_MESSAGE + self.serve_static_files = value + end + def session_store(*args) if args.empty? case @session_store diff --git a/railties/lib/rails/application/default_middleware_stack.rb b/railties/lib/rails/application/default_middleware_stack.rb index d1789192ef..02eea82b0c 100644 --- a/railties/lib/rails/application/default_middleware_stack.rb +++ b/railties/lib/rails/application/default_middleware_stack.rb @@ -17,7 +17,7 @@ def build_stack middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header - if config.serve_static_assets + if config.serve_static_files middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control end diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt index ddc04d446c..677bb3b338 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt @@ -20,8 +20,9 @@ Rails.application.configure do # NGINX, varnish or squid. # config.action_dispatch.rack_cache = true - # Disable Rails's static asset server (Apache or NGINX will already do this). - config.serve_static_assets = false + # Disable serving static files from the `/public` folder by default since + # Apache or NGINX already handles this. + config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present? <%- unless options.skip_sprockets? -%> # Compress JavaScripts and CSS. diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt index 03a3568fbe..1c19f08b28 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt @@ -12,8 +12,8 @@ Rails.application.configure do # preloads Rails for running tests, you may have to set it to true. config.eager_load = false - # Configure static asset server for tests with Cache-Control for performance. - config.serve_static_assets = true + # Configure static file server for tests with Cache-Control for performance. + config.serve_static_files = true config.static_cache_control = 'public, max-age=3600' # Show full error reports and disable caching. diff --git a/railties/test/application/assets_test.rb b/railties/test/application/assets_test.rb index 8f091cfdbf..d58a27403e 100644 --- a/railties/test/application/assets_test.rb +++ b/railties/test/application/assets_test.rb @@ -225,7 +225,7 @@ class User < ActiveRecord::Base; raise 'should not be reached'; end test "assets do not require any assets group gem when manifest file is present" do app_file "app/assets/javascripts/application.js", "alert();" - add_to_env_config "production", "config.serve_static_assets = true" + add_to_env_config "production", "config.serve_static_files = true" ENV["RAILS_ENV"] = "production" precompile! diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 679190dad4..bf6c64b518 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -41,7 +41,7 @@ def app def setup build_app boot_rails - FileUtils.rm_rf("#{app_path}/config/environments") + supress_default_config end def teardown @@ -49,6 +49,15 @@ def teardown FileUtils.rm_rf(new_app) if File.directory?(new_app) end + def supress_default_config + FileUtils.mv("#{app_path}/config/environments", "#{app_path}/config/__environments__") + end + + def restore_default_config + FileUtils.rm_rf("#{app_path}/config/environments") + FileUtils.mv("#{app_path}/config/__environments__", "#{app_path}/config/environments") + end + test "Rails.env does not set the RAILS_ENV environment variable which would leak out into rake tasks" do require "rails" @@ -280,6 +289,53 @@ def assert_utf8 assert_equal Pathname.new(app_path).join("somewhere"), Rails.public_path end + test "In production mode, config.serve_static_files is off by default" do + restore_default_config + + with_rails_env "production" do + require "#{app_path}/config/environment" + assert_not app.config.serve_static_files + end + end + + test "In production mode, config.serve_static_files is enabled when RAILS_SERVE_STATIC_FILES is set" do + restore_default_config + + with_rails_env "production" do + switch_env "RAILS_SERVE_STATIC_FILES", "1" do + require "#{app_path}/config/environment" + assert app.config.serve_static_files + end + end + end + + test "In production mode, config.serve_static_files is disabled when RAILS_SERVE_STATIC_FILES is blank" do + restore_default_config + + with_rails_env "production" do + switch_env "RAILS_SERVE_STATIC_FILES", " " do + require "#{app_path}/config/environment" + assert_not app.config.serve_static_files + end + end + end + + test "config.serve_static_assets is deprecated" do + require "#{app_path}/config/application" + + assert_deprecated(/serve_static_assets/) do + app.config.serve_static_assets = false + end + + assert_not app.config.serve_static_files + assert_deprecated(/serve_static_assets/) { assert_not app.config.serve_static_assets } + + app.config.serve_static_files = true + + assert app.config.serve_static_files + assert_deprecated(/serve_static_assets/) { assert app.config.serve_static_assets } + end + test "Use key_generator when secret_key_base is set" do make_basic_app do |app| app.secrets.secret_key_base = 'b3c631c314c0bbca50c1b2843150fe33' diff --git a/railties/test/application/middleware/sendfile_test.rb b/railties/test/application/middleware/sendfile_test.rb index eb791f5687..dc96480d6d 100644 --- a/railties/test/application/middleware/sendfile_test.rb +++ b/railties/test/application/middleware/sendfile_test.rb @@ -61,7 +61,7 @@ def index test "files handled by ActionDispatch::Static are handled by Rack::Sendfile" do make_basic_app do |app| app.config.action_dispatch.x_sendfile_header = 'X-Sendfile' - app.config.serve_static_assets = true + app.config.serve_static_files = true app.paths["public"] = File.join(rails_root, "public") end diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb index caef39d16f..c64fe082f3 100644 --- a/railties/test/application/middleware_test.rb +++ b/railties/test/application/middleware_test.rb @@ -113,8 +113,8 @@ def app assert !middleware.include?("Rack::Lock") end - test "removes static asset server if serve_static_assets is disabled" do - add_to_config "config.serve_static_assets = false" + test "removes static asset server if serve_static_files is disabled" do + add_to_config "config.serve_static_files = false" boot! assert !middleware.include?("ActionDispatch::Static") end diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 260ee0eda9..91cdc60bd1 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -1210,7 +1210,7 @@ def foo test "engine can be properly mounted at root" do add_to_config("config.action_dispatch.show_exceptions = false") - add_to_config("config.serve_static_assets = false") + add_to_config("config.serve_static_files = false") @plugin.write "lib/bukkits.rb", <<-RUBY module Bukkits -- GitLab