mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Enable static asset server by default
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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.
|
||||
#
|
||||
|
||||
@@ -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!
|
||||
# config.threadsafe!
|
||||
|
||||
@@ -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) }
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user