Class: Rage::Configuration

Inherits:
Object
  • Object
show all
Includes:
Hooks
Defined in:
lib/rage/configuration.rb

Overview

Rage.configure can be used to adjust the behavior of your Rage application:

Rage.configure do
  config.logger = Rage::Logger.new(STDOUT)
  config.server.workers_count = 2
end

General Configuration

config.logger

The logger that will be used for Rage.logger and any related Rage logging. Custom loggers should implement Ruby’s Logger interface.

config.log_formatter

The formatter of the Rage logger. Built in options include Rage::TextFormatter and Rage::JSONFormatter. Defaults to an instance of Rage::TextFormatter.

config.log_level

Defines the verbosity of the Rage logger. This option defaults to :debug for all environments except production, where it defaults to :info. The available log levels are: :debug, :info, :warn, :error, :fatal, and :unknown.

config.secret_key_base

The secret_key_base is used as the input secret to the application’s key generator, which is used to encrypt cookies. Rage will fall back to the SECRET_KEY_BASE environment variable if this is not set.

config.fallback_secret_key_base

Defines one or several old secrets that need to be rotated. Can accept a single key or an array of keys. Rage will fall back to the FALLBACK_SECRET_KEY_BASE environment variable if this is not set.

config.after_initialize

Schedule a block of code to run after Rage has finished loading the application code. Use this to reference application-level constants during the initialization process.

Rage.config.after_initialize do
  SUPER_USER = User.find_by!(super: true)
end

Middleware Configuration

config.middleware.use

Adds a middleware to the top of the middleware stack. This is the recommended way of adding a middleware.

config.middleware.use Rack::Cors do
  allow do
    origins "*"
    resource "*", headers: :any
  end
end

config.middleware.insert_before

Adds middleware at a specified position before another middleware. The position can be either an index or another middleware.

❗️Heads up: By default, Rage always uses the Rage::FiberWrapper middleware, which wraps every request in a separate fiber. Make sure to always have this middleware in the top of the stack. Placing other middlewares in front may lead to undefined behavior.

config.middleware.insert_before Rack::Head, Magical::Unicorns
config.middleware.insert_before 0, Magical::Unicorns

config.middleware.insert_after

Adds middleware at a specified position after another middleware. The position can be either an index or another middleware.

config.middleware.insert_after Rack::Head, Magical::Unicorns

Server Configuration

• config.server.max_clients

Limits the number of simultaneous connections the server can accept. Defaults to the maximum number of open files.

❗️Heads up: Decreasing this number is almost never a good idea. Depending on your application specifics, you are encouraged to use other methods to limit the number of concurrent connections:

  1. If your application is exposed to the public, you may want to use a cloud rate limiter, like Cloudflare WAF or Fastly WAF.

  2. Otherwise, consider using tools like Rack::Attack or connection_pool.

# Limit the amount of connections your application can accept
config.middleware.use Rack::Attack
Rack::Attack.throttle("req/ip", limit: 300, period: 5.minutes) do |req|
  req.ip
end
#
# Limit the amount of connections to a specific resource
HTTP = ConnectionPool.new(size: 5, timeout: 5) { Net::HTTP }
HTTP.with do |conn|
  conn.get("/my-resource")
end

config.server.port

Specifies what port the server will listen on.

config.server.workers_count

Specifies the number of server processes to run. Defaults to 1 in development and to the number of available CPU cores in other environments.

config.server.timeout

Specifies connection timeout.

Static file server

config.public_file_server.enabled

Configures whether Rage should serve static files from the public directory. Defaults to false.

Cable Configuration

config.cable.protocol

Specifies the protocol the server will use. Supported values include :actioncable_v1_json and :raw_websocket_json. Defaults to :actioncable_v1_json.

config.cable.allowed_request_origins

Restricts the server to only accept requests from specified origins. The origins can be instances of strings or regular expressions, against which a check for the match will be performed.

config.cable.disable_request_forgery_protection

Allows requests from any origin.

OpenAPI Configuration

config.openapi.tag_resolver

Specifies the proc to build tags for API operations. The proc accepts the controller class, the symbol name of the action, and the default tag built by Rage.

config.openapi.tag_resolver = proc do |controller, action, default_tag|
   # ...
end

Deferred Configuration

config.deferred.backend

Specifies the backend for deferred tasks. Supported values are :disk, which uses disk storage, or nil, which disables persistence of deferred tasks. The :disk backend accepts the following options:

  • :path - the path to the directory where deferred tasks will be stored. Defaults to storage.

  • :prefix - the prefix for the deferred task files. Defaults to deferred-.

  • :fsync_frequency - the frequency of fsync calls in seconds. Defaults to 0.5.

config.deferred.backend = :disk, { path: "storage" }

config.deferred.backpressure

Enables the backpressure for deferred tasks. The backpressure is used to limit the number of pending tasks in the queue. It accepts a hash with the following options:

  • :high_water_mark - the maximum number of pending tasks in the queue. Defaults to 1000.

  • :low_water_mark - the minimum number of pending tasks in the queue before the backpressure is released. Defaults to high_water_mark * 0.8.

  • :timeout - the timeout for the backpressure in seconds. Defaults to 2.

config.deferred.backpressure = { high_water_mark: 1000, low_water_mark: 800, timeout: 2 }

Additionally, you can set the backpressure value to true to use the default values:

config.deferred.backpressure = true

Transient Settings

The settings described in this section should be configured using environment variables and are either temporary or will become the default in the future.

RAGE_DISABLE_IO_WRITE

Disables the io_write hook to fix the “zero-length iov” error on Ruby < 3.3.

RAGE_DISABLE_AR_POOL_PATCH

Disables the ActiveRecord::ConnectionPool patch and makes Rage use the original ActiveRecord implementation.

RAGE_DISABLE_AR_WEAK_CONNECTIONS

Instructs Rage to not reuse Active Record connections between different fibers.

Defined Under Namespace

Classes: Cable, Deferred, Middleware, OpenAPI, PublicFileServer, Server

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hooks

#hooks, #push_hook, #run_hooks_for!

Instance Attribute Details

#fallback_secret_key_baseObject



219
220
221
# File 'lib/rage/configuration.rb', line 219

def fallback_secret_key_base
  Array(@fallback_secret_key_base || ENV["FALLBACK_SECRET_KEY_BASE"])
end

#log_formatterObject

Returns the value of attribute log_formatter.



200
201
202
# File 'lib/rage/configuration.rb', line 200

def log_formatter
  @log_formatter
end

#log_levelObject

Returns the value of attribute log_level.



200
201
202
# File 'lib/rage/configuration.rb', line 200

def log_level
  @log_level
end

#loggerObject

Returns the value of attribute logger.



199
200
201
# File 'lib/rage/configuration.rb', line 199

def logger
  @logger
end

#secret_key_baseObject



215
216
217
# File 'lib/rage/configuration.rb', line 215

def secret_key_base
  @secret_key_base || ENV["SECRET_KEY_BASE"]
end

Instance Method Details

#after_initialize(&block) ⇒ Object



251
252
253
# File 'lib/rage/configuration.rb', line 251

def after_initialize(&block)
  push_hook(block, :after_initialize)
end

#cableObject



231
232
233
# File 'lib/rage/configuration.rb', line 231

def cable
  @cable ||= Cable.new
end

#configObject

used in DSL



204
# File 'lib/rage/configuration.rb', line 204

def config = self

#deferredObject



243
244
245
# File 'lib/rage/configuration.rb', line 243

def deferred
  @deferred ||= Deferred.new
end

#internalObject



247
248
249
# File 'lib/rage/configuration.rb', line 247

def internal
  @internal ||= Internal.new
end

#middlewareObject



227
228
229
# File 'lib/rage/configuration.rb', line 227

def middleware
  @middleware ||= Middleware.new
end

#openapiObject



239
240
241
# File 'lib/rage/configuration.rb', line 239

def openapi
  @openapi ||= OpenAPI.new
end

#public_file_serverObject



235
236
237
# File 'lib/rage/configuration.rb', line 235

def public_file_server
  @public_file_server ||= PublicFileServer.new
end

#run_after_initialize!Object



255
256
257
# File 'lib/rage/configuration.rb', line 255

def run_after_initialize!
  run_hooks_for!(:after_initialize, self)
end

#serverObject



223
224
225
# File 'lib/rage/configuration.rb', line 223

def server
  @server ||= Server.new
end