跳过到内容 跳过到搜索

一个负责协调整个启动过程的 Engine

初始化

Rails::Application 负责执行所有 railties 和 engines 的初始化器。它还会在其他所有初始化器执行后执行一些启动初始化器(查看 Rails::Application::Bootstrap)和完成初始化器(查看 Rails::Application::Finisher)。

配置

除了提供与 Rails::EngineRails::Railtie 相同的配置之外,应用程序对象还有一些特定的配置,例如 enable_reloadingconsider_all_requests_localfilter_parameterslogger 等等。

查看 Rails::Application::Configuration 以查看它们。

路由

应用程序对象还负责保存路由并在开发中每次文件更改时重新加载路由。

中间件

Application 还负责构建中间件堆栈。

启动过程

应用程序还负责设置和执行启动过程。从你在应用程序中调用 config/application.rb 的那一刻起,启动过程如下所示:

  1. require "config/boot.rb" 用于设置加载路径。

  2. require railties 和 engines。

  3. Rails.application 定义为 class MyApp::Application < Rails::Application

  4. 运行 config.before_configuration 回调。

  5. 加载 config/environments/ENV.rb

  6. 运行 config.before_initialize 回调。

  7. 运行 railties、engines 和应用程序定义的 Railtie#initializer。每个引擎都会一个接一个地设置其加载路径和路由,并运行其 config/initializers/* 文件。

  8. 执行由 railties、engines 和应用程序添加的自定义 Railtie#initializers

  9. 构建中间件堆栈并运行 to_prepare 回调。

  10. 如果 eager_loadtrue,则运行 config.before_eager_loadeager_load!

  11. 运行 config.after_initialize 回调。

命名空间
方法
C
D
E
F
G
I
K
M
N
R
S

属性

[RW] assets
[R] autoloaders
[W] config
[W] credentials
[R] executor
[R] reloader
[R] reloaders
[RW] sandbox
[RW] sandbox?

类公共方法

create(initial_variable_values = {}, &block)

# File railties/lib/rails/application.rb, line 84
def create(initial_variable_values = {}, &block)
  new(initial_variable_values, &block).run_load_hooks!
end

find_root(from)

# File railties/lib/rails/application.rb, line 88
def find_root(from)
  find_root_with_flag "config.ru", from, Dir.pwd
end

inherited(base)

# File railties/lib/rails/application.rb, line 71
def inherited(base)
  super
  Rails.app_class = base
  # lib has to be added to $LOAD_PATH unconditionally, even if it's in the
  # autoload paths and config.add_autoload_paths_to_load_path is false.
  add_lib_to_load_path!(find_root(base.called_from))
  ActiveSupport.run_load_hooks(:before_configuration, base)
end

instance()

# File railties/lib/rails/application.rb, line 80
def instance
  super.run_load_hooks!
end

new(initial_variable_values = {}, &block)

# File railties/lib/rails/application.rb, line 109
def initialize(initial_variable_values = {}, &block)
  super()
  @initialized       = false
  @reloaders         = []
  @routes_reloader   = nil
  @app_env_config    = nil
  @ordered_railties  = nil
  @railties          = nil
  @key_generators    = {}
  @message_verifiers = nil
  @deprecators       = nil
  @ran_load_hooks    = false

  @executor          = Class.new(ActiveSupport::Executor)
  @reloader          = Class.new(ActiveSupport::Reloader)
  @reloader.executor = @executor

  @autoloaders = Rails::Autoloaders.new

  # are these actually used?
  @initial_variable_values = initial_variable_values
  @block = block
end

实例公共方法

config_for(name, env: Rails.env)

用于加载当前 Rails 环境下的 config/foo.yml 的便捷方法。例如

# config/exception_notification.yml:
production:
  url: http://127.0.0.1:8080
  namespace: my_app_production

development:
  url: https://127.0.0.1:3001
  namespace: my_app_development

# config/environments/production.rb
Rails.application.configure do
  config.middleware.use ExceptionNotifier, config_for(:exception_notification)
end

你还可以将配置存储在共享部分,这些共享部分将与环境配置合并

# config/example.yml
shared:
  foo:
    bar:
      baz: 1

development:
  foo:
    bar:
      qux: 2

# development environment
Rails.application.config_for(:example)[:foo][:bar]
# => { baz: 1, qux: 2 }
# File railties/lib/rails/application.rb, line 288
def config_for(name, env: Rails.env)
  yaml = name.is_a?(Pathname) ? name : Pathname.new("#{paths["config"].existent.first}/#{name}.yml")

  if yaml.exist?
    require "erb"
    all_configs    = ActiveSupport::ConfigurationFile.parse(yaml).deep_symbolize_keys
    config, shared = all_configs[env.to_sym], all_configs[:shared]

    if shared
      config = {} if config.nil? && shared.is_a?(Hash)
      if config.is_a?(Hash) && shared.is_a?(Hash)
        config = shared.deep_merge(config)
      elsif config.nil?
        config = shared
      end
    end

    if config.is_a?(Hash)
      config = ActiveSupport::OrderedOptions.new.update(config)
    end

    config
  else
    raise "Could not load configuration. No such file - #{yaml}"
  end
end

console(&blk)

将新应用程序实例中调用的任何控制台发送到 Rails::Railtie 中定义的 console 方法。

# File railties/lib/rails/application.rb, line 371
def console(&blk)
  self.class.console(&blk)
end

credentials()

返回 ActiveSupport::EncryptedConfiguration 的实例,用于 config.credentials.content_path 指定的凭据文件。

默认情况下,config.credentials.content_path 将指向当前环境下的 config/credentials/#{environment}.yml.enc(例如,config/credentials/production.yml.enc 用于 production 环境),或者指向 config/credentials.yml.enc(如果该文件不存在)。

加密密钥从 ENV["RAILS_MASTER_KEY"]config.credentials.key_path 指定的文件中获取。默认情况下,config.credentials.key_path 将指向当前环境下的 config/credentials/#{environment}.key,或者指向 config/master.key(如果该文件不存在)。

# File railties/lib/rails/application.rb, line 492
def credentials
  @credentials ||= encrypted(config.credentials.content_path, key_path: config.credentials.key_path)
end

deprecators()

一个受管理的弃用器集合 (ActiveSupport::Deprecation::Deprecators)。集合的配置方法会影响集合中的所有弃用器。此外,集合的 silence 方法会在给定块的持续时间内使集合中的所有弃用器静音。

# File railties/lib/rails/application.rb, line 244
def deprecators
  @deprecators ||= ActiveSupport::Deprecation::Deprecators.new.tap do |deprecators|
    deprecators[:railties] = Rails.deprecator
  end
end

eager_load!()

急切地加载应用程序代码。

# File railties/lib/rails/application.rb, line 550
def eager_load!
  Rails.autoloaders.each(&:eager_load)
end

encrypted(path, key_path: "config/master.key", env_key: "RAILS_MASTER_KEY")

返回 ActiveSupport::EncryptedConfiguration 的实例,用于加密文件。默认情况下,加密密钥从 ENV["RAILS_MASTER_KEY"]config/master.key 文件中获取。

my_config = Rails.application.encrypted("config/my_config.enc")

my_config.read
# => "foo:\n  bar: 123\n"

my_config.foo.bar
# => 123

可以使用 bin/rails encrypted:edit 命令编辑加密文件。(有关更多信息,请参见 bin/rails encrypted:edit --help 的输出。)

# File railties/lib/rails/application.rb, line 511
def encrypted(path, key_path: "config/master.key", env_key: "RAILS_MASTER_KEY")
  ActiveSupport::EncryptedConfiguration.new(
    config_path: Rails.root.join(path),
    key_path: Rails.root.join(key_path),
    env_key: env_key,
    raise_if_missing_key: config.require_master_key
  )
end

env_config()

存储一些 Rails 初始环境参数,这些参数将被中间件和引擎用于配置自身。

# File railties/lib/rails/application.rb, line 317
def env_config
  @app_env_config ||= super.merge(
      "action_dispatch.parameter_filter" => filter_parameters,
      "action_dispatch.redirect_filter" => config.filter_redirect,
      "action_dispatch.secret_key_base" => secret_key_base,
      "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions,
      "action_dispatch.show_detailed_exceptions" => config.consider_all_requests_local,
      "action_dispatch.log_rescued_responses" => config.action_dispatch.log_rescued_responses,
      "action_dispatch.debug_exception_log_level" => ActiveSupport::Logger.const_get(config.action_dispatch.debug_exception_log_level.to_s.upcase),
      "action_dispatch.logger" => Rails.logger,
      "action_dispatch.backtrace_cleaner" => Rails.backtrace_cleaner,
      "action_dispatch.key_generator" => key_generator,
      "action_dispatch.http_auth_salt" => config.action_dispatch.http_auth_salt,
      "action_dispatch.signed_cookie_salt" => config.action_dispatch.signed_cookie_salt,
      "action_dispatch.encrypted_cookie_salt" => config.action_dispatch.encrypted_cookie_salt,
      "action_dispatch.encrypted_signed_cookie_salt" => config.action_dispatch.encrypted_signed_cookie_salt,
      "action_dispatch.authenticated_encrypted_cookie_salt" => config.action_dispatch.authenticated_encrypted_cookie_salt,
      "action_dispatch.use_authenticated_cookie_encryption" => config.action_dispatch.use_authenticated_cookie_encryption,
      "action_dispatch.encrypted_cookie_cipher" => config.action_dispatch.encrypted_cookie_cipher,
      "action_dispatch.signed_cookie_digest" => config.action_dispatch.signed_cookie_digest,
      "action_dispatch.cookies_serializer" => config.action_dispatch.cookies_serializer,
      "action_dispatch.cookies_digest" => config.action_dispatch.cookies_digest,
      "action_dispatch.cookies_rotations" => config.action_dispatch.cookies_rotations,
      "action_dispatch.cookies_same_site_protection" => coerce_same_site_protection(config.action_dispatch.cookies_same_site_protection),
      "action_dispatch.use_cookies_with_metadata" => config.action_dispatch.use_cookies_with_metadata,
      "action_dispatch.content_security_policy" => config.content_security_policy,
      "action_dispatch.content_security_policy_report_only" => config.content_security_policy_report_only,
      "action_dispatch.content_security_policy_nonce_generator" => config.content_security_policy_nonce_generator,
      "action_dispatch.content_security_policy_nonce_directives" => config.content_security_policy_nonce_directives,
      "action_dispatch.permissions_policy" => config.permissions_policy,
    )
end

generators(&blk)

将新应用程序实例中调用的任何生成器发送到 Rails::Railtie 中定义的 generators 方法。

# File railties/lib/rails/application.rb, line 377
def generators(&blk)
  self.class.generators(&blk)
end

initialized?()

如果应用程序已初始化,则返回 true。

# File railties/lib/rails/application.rb, line 134
def initialized?
  @initialized
end

initializer(name, opts = {}, &block)

将初始化器发送到 Rails::Initializable 模块中定义的 initializer 方法。每个 Rails::Application 类都有自己的一组初始化器,如 Initializable 模块所定义。

# File railties/lib/rails/application.rb, line 359
def initializer(name, opts = {}, &block)
  self.class.initializer(name, opts, &block)
end

isolate_namespace(mod)

isolate_namespace方法传递给类方法。

# File railties/lib/rails/application.rb, line 388
def isolate_namespace(mod)
  self.class.isolate_namespace(mod)
end

key_generator(secret_key_base = self.secret_key_base)

返回一个密钥生成器 (ActiveSupport::CachingKeyGenerator),用于指定的secret_key_base。返回值被缓存,所以使用相同secret_key_base的后续调用将返回相同的密钥生成器实例。

# File railties/lib/rails/application.rb, line 172
def key_generator(secret_key_base = self.secret_key_base)
  # number of iterations selected based on consultation with the google security
  # team. Details at https://github.com/rails/rails/pull/6952#issuecomment-7661220
  @key_generators[secret_key_base] ||= ActiveSupport::CachingKeyGenerator.new(
    ActiveSupport::KeyGenerator.new(secret_key_base, iterations: 1000)
  )
end

message_verifier(verifier_name)

返回一个消息验证器对象。

这个验证器可以用于在应用程序中生成和验证签名消息。

建议不要对不同的用途使用相同的验证器,因此您可以通过传递verifier_name参数获得不同的验证器。

例如,ActiveStorage::Blob.signed_id_verifier是使用此功能实现的,它确保 ID 字符串没有被篡改,可以在查找器中安全使用。

有关更多信息,请参阅 ActiveSupport::MessageVerifier 文档。

参数

  • verifier_name - 消息验证器的名称。

示例

message = Rails.application.message_verifier('my_purpose').generate('data to sign against tampering')
Rails.application.message_verifier('my_purpose').verify(message)
# => 'data to sign against tampering'
# File railties/lib/rails/application.rb, line 236
def message_verifier(verifier_name)
  message_verifiers[verifier_name]
end

message_verifiers()

返回一个消息验证器工厂 (ActiveSupport::MessageVerifiers)。这个工厂可以用作配置和创建消息验证器 (ActiveSupport::MessageVerifier) 的中心点,用于您的应用程序。

默认情况下,由该工厂创建的消息验证器将使用默认的 ActiveSupport::MessageVerifier 选项生成消息。您可以使用 ActiveSupport::MessageVerifiers#clear_rotationsActiveSupport::MessageVerifiers#rotate 的组合来覆盖这些选项。但是,这必须在构建任何消息验证器实例之前完成。例如,在before_initialize块中

# Use `url_safe: true` when generating messages
config.before_initialize do |app|
  app.message_verifiers.clear_rotations
  app.message_verifiers.rotate(url_safe: true)
end

由该工厂创建的消息验证器在生成消息时始终使用从 secret_key_base 派生的密钥。clear_rotations 不会影响这种行为。但是,可以轮换旧的secret_key_base值以验证消息

# Fall back to old `secret_key_base` when verifying messages
config.before_initialize do |app|
  app.message_verifiers.rotate(secret_key_base: "old secret_key_base")
end
# File railties/lib/rails/application.rb, line 208
def message_verifiers
  @message_verifiers ||=
    ActiveSupport::MessageVerifiers.new do |salt, secret_key_base: self.secret_key_base|
      key_generator(secret_key_base).generate_key(salt)
    end.rotate_defaults
end

name()

返回应用程序名称的连字符分隔形式。

MyApp::Application.new.name => "my-app"
# File railties/lib/rails/application.rb, line 141
def name
  self.class.name.underscore.dasherize.delete_suffix("/application")
end

rake_tasks(&block)

如果您尝试在实例上定义一组 Rake 任务,这些任务将被传递到应用程序类上定义的 Rake 任务。

# File railties/lib/rails/application.rb, line 352
def rake_tasks(&block)
  self.class.rake_tasks(&block)
end

reload_routes!()

无论路线是否改变,都重新加载应用程序路线。

# File railties/lib/rails/application.rb, line 160
def reload_routes!
  routes_reloader.reload!
end

runner(&blk)

将任何在新的应用程序实例中调用的运行程序传递给在 Rails::Railtie 中定义的runner方法。

# File railties/lib/rails/application.rb, line 365
def runner(&blk)
  self.class.runner(&blk)
end

secret_key_base()

secret_key_base 用作应用程序密钥生成器的输入密钥,而密钥生成器又用于创建所有 ActiveSupport::MessageVerifierActiveSupport::MessageEncryptor 实例,包括用于签名和加密 Cookie 的实例。

在开发和测试中,它是随机生成的,并存储在tmp/local_secret.txt中的临时文件中。

您还可以设置ENV["SECRET_KEY_BASE_DUMMY"]以触发使用随机生成的 secret_key_base,该密钥存储在临时文件中。当为生产环境预编译资产作为构建步骤的一部分时,这很有用,否则该构建步骤不需要访问生产密钥。

Dockerfile 示例:RUN SECRET_KEY_BASE_DUMMY=1 bundle exec rails assets:precompile

在所有其他环境中,我们首先在ENV["SECRET_KEY_BASE"]中查找它,然后在credentials.secret_key_base中查找它。对于大多数应用程序,将它存储在加密凭据文件中的位置是正确的。

# File railties/lib/rails/application.rb, line 474
def secret_key_base
  config.secret_key_base
end

server(&blk)

将任何在新的应用程序实例中调用的服务器传递给在 Rails::Railtie 中定义的server方法。

# File railties/lib/rails/application.rb, line 383
def server(&blk)
  self.class.server(&blk)
end

实例保护方法

ensure_generator_templates_added()

# File railties/lib/rails/application.rb, line 628
def ensure_generator_templates_added
  configured_paths = config.generators.templates
  configured_paths.unshift(*(paths["lib/templates"].existent - configured_paths))
end