2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-05-20 23:37:17 -04:00
|
|
|
# we want MessageBus to be close to the front
|
2015-12-08 19:48:41 -05:00
|
|
|
# this is important cause the vast majority of web requests go to it
|
|
|
|
# this allows us to avoid full middleware crawls each time
|
2020-03-30 07:01:06 -04:00
|
|
|
#
|
|
|
|
# We aren't manipulating the middleware stack directly because of
|
|
|
|
# https://github.com/rails/rails/pull/27936
|
2017-09-04 08:42:22 -04:00
|
|
|
|
2020-05-07 20:44:51 -04:00
|
|
|
Rails.configuration.middleware.unshift(MessageBus::Rack::Middleware)
|
2015-12-08 19:48:41 -05:00
|
|
|
|
|
|
|
# no reason to track this in development, that is 300+ redis calls saved per
|
|
|
|
# page view (we serve all assets out of thin in development)
|
|
|
|
if Rails.env != "development" || ENV["TRACK_REQUESTS"]
|
|
|
|
require "middleware/request_tracker"
|
|
|
|
Rails.configuration.middleware.unshift Middleware::RequestTracker
|
2019-06-05 02:08:11 -04:00
|
|
|
|
|
|
|
MethodProfiler.ensure_discourse_instrumentation! if GlobalSetting.enable_performance_http_headers
|
2015-12-08 19:48:41 -05:00
|
|
|
end
|
2020-03-30 07:01:06 -04:00
|
|
|
|
2021-11-11 11:44:58 -05:00
|
|
|
if Rails.env.test?
|
|
|
|
# In test mode we can't insert/remove middlewares
|
|
|
|
# Therefore we insert a small helper which effectively switches the multisite
|
|
|
|
# middleware on/off based on the Rails.configuration.multisite value
|
|
|
|
class TestMultisiteMiddleware < RailsMultisite::Middleware
|
|
|
|
def call(env)
|
|
|
|
return @app.call(env) if !Rails.configuration.multisite
|
|
|
|
super(env)
|
|
|
|
end
|
|
|
|
end
|
2024-02-15 03:36:12 -05:00
|
|
|
|
2021-11-11 11:44:58 -05:00
|
|
|
Rails.configuration.middleware.unshift TestMultisiteMiddleware,
|
|
|
|
RailsMultisite::DiscoursePatches.config
|
2024-02-15 03:36:12 -05:00
|
|
|
|
|
|
|
class BlockRequestsMiddleware
|
|
|
|
@@block_requests = false
|
|
|
|
|
|
|
|
def self.block_requests!
|
|
|
|
@@block_requests = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.allow_requests!
|
|
|
|
@@block_requests = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
if @@block_requests
|
|
|
|
[503, { "Content-Type" => "text/plain" }, ["Blocked by BlockRequestsMiddleware"]]
|
|
|
|
else
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Rails.configuration.middleware.unshift BlockRequestsMiddleware
|
2021-11-11 11:44:58 -05:00
|
|
|
elsif Rails.configuration.multisite
|
2020-09-17 05:11:57 -04:00
|
|
|
assets_hostnames = GlobalSetting.cdn_hostnames
|
|
|
|
|
|
|
|
if assets_hostnames.empty?
|
|
|
|
assets_hostnames = Discourse::Application.config.database_configuration[Rails.env]["host_names"]
|
|
|
|
end
|
|
|
|
|
|
|
|
RailsMultisite::ConnectionManagement.asset_hostnames = assets_hostnames
|
2020-09-15 05:07:03 -04:00
|
|
|
|
2020-05-07 20:44:51 -04:00
|
|
|
# Multisite needs to be first, because the request tracker and message bus rely on it
|
|
|
|
Rails.configuration.middleware.unshift RailsMultisite::Middleware,
|
|
|
|
RailsMultisite::DiscoursePatches.config
|
|
|
|
Rails.configuration.middleware.delete ActionDispatch::Executor
|
2020-06-15 04:23:24 -04:00
|
|
|
|
2020-06-15 23:03:47 -04:00
|
|
|
if defined?(RailsFailover::ActiveRecord) && Rails.configuration.active_record_rails_failover
|
2020-06-15 04:23:24 -04:00
|
|
|
Rails.configuration.middleware.insert_after(
|
|
|
|
RailsMultisite::Middleware,
|
|
|
|
RailsFailover::ActiveRecord::Middleware,
|
|
|
|
)
|
|
|
|
end
|
2022-06-27 15:58:33 -04:00
|
|
|
|
|
|
|
if Rails.env.development?
|
|
|
|
# Automatically allow development multisite hosts
|
|
|
|
RailsMultisite::ConnectionManagement.instance.db_spec_cache.each do |db, specification|
|
|
|
|
next if db == "default"
|
|
|
|
Rails.configuration.hosts.concat(specification.spec.configuration_hash[:host_names])
|
|
|
|
end
|
|
|
|
end
|
2020-06-15 23:03:47 -04:00
|
|
|
elsif defined?(RailsFailover::ActiveRecord) && Rails.configuration.active_record_rails_failover
|
2020-06-11 01:45:46 -04:00
|
|
|
Rails.configuration.middleware.insert_before(
|
|
|
|
MessageBus::Rack::Middleware,
|
|
|
|
RailsFailover::ActiveRecord::Middleware,
|
|
|
|
)
|
2020-06-04 05:13:59 -04:00
|
|
|
end
|