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

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, 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



187
188
189
# File 'lib/rage/configuration.rb', line 187

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.



168
169
170
# File 'lib/rage/configuration.rb', line 168

def log_formatter
  @log_formatter
end

#log_levelObject

Returns the value of attribute log_level.



168
169
170
# File 'lib/rage/configuration.rb', line 168

def log_level
  @log_level
end

#loggerObject

Returns the value of attribute logger.



167
168
169
# File 'lib/rage/configuration.rb', line 167

def logger
  @logger
end

#secret_key_baseObject



183
184
185
# File 'lib/rage/configuration.rb', line 183

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

Instance Method Details

#after_initialize(&block) ⇒ Object



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

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

#cableObject



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

def cable
  @cable ||= Cable.new
end

#configObject

used in DSL



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

def config = self

#internalObject



211
212
213
# File 'lib/rage/configuration.rb', line 211

def internal
  @internal ||= Internal.new
end

#middlewareObject



195
196
197
# File 'lib/rage/configuration.rb', line 195

def middleware
  @middleware ||= Middleware.new
end

#openapiObject



207
208
209
# File 'lib/rage/configuration.rb', line 207

def openapi
  @openapi ||= OpenAPI.new
end

#public_file_serverObject



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

def public_file_server
  @public_file_server ||= PublicFileServer.new
end

#run_after_initialize!Object



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

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

#serverObject



191
192
193
# File 'lib/rage/configuration.rb', line 191

def server
  @server ||= Server.new
end