Rage Framework Core Components Overview
Controller for handling HTTP requests and responses, Router for defining and managing application routes, and Configuration for managing application settings and environment variables.website·Indexed 32 days ago
https://rage-rb.devTechnical documentation for the Rage framework, including API references for classes and modules, telemetry, and an Inertia.js adapter. Includes guides on handling database connection errors in concurrent processing and system startup.
Controller for handling HTTP requests and responses, Router for defining and managing application routes, and Configuration for managing application settings and environment variables.component.entity.action (e.g., controller.action.process). Developers can create handlers to observe these spans and integrate with external monitoring tools.To use Rage for HTTP request handling while keeping Rails for ActiveRecord and migrations, follow these steps:
gem "rage-rb" to Gemfile.require "rage/rails" in config/application.rb after the application class definition.Rage.routes.draw in config/routes.rb.ApplicationController from RageController::API.config.ru to run Rage.application.bundle exec rage s to start the server.# config/application.rb
require "rage/rails"
# config/routes.rb
Rage.routes.draw do
resources :bookings, only: %i(index create)
end
# app/controllers/application_controller.rb
class ApplicationController < RageController::API
end
# config.ru
require_relative "config/environment"
run Rage.application@deprecated tag to mark specific actions or entire controllers as deprecated. When applied at the class level, it affects all actions within that controller and any child controllers.class Api::V1::UsersController < ApplicationController
# Mark entire controller as deprecated
# @deprecated
def index
end
# Mark specific action as deprecated
# @deprecated
def create
end
enddelay (seconds) or delay_until (Time object). Recurring tasks are configured in the global Rage configuration block using every.# Delayed execution
SayHello.enqueue(name: "World", delay: 10)
SayHello.enqueue(name: "World", delay_until: Time.now + 10)
# Recurring schedule
Rage.configure do
config.deferred.schedule do
every 5.minutes, task: ClearCache
end
endRage integrates with RSpec to provide Rails-style testing helpers for API endpoints. To set up request specs, install rspec and database_cleaner-active_record, initialize RSpec, and configure the database cleaner.
Critical Note: The transaction cleaning strategy is not supported because Rage executes requests in separate Fibers, which causes Active Record to use different connections for the test and the controller. Use the truncation strategy instead.
# Gemfile
group :test do
gem "rspec"
gem "database_cleaner-active_record"
end
# spec/spec_helper.rb
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.around(:each) do |example|
DatabaseCleaner.cleaning(&example)
end
endrescue_from method within a subscriber to centralize exception handling. For deferred subscribers, it is critical to re-raise the exception after handling it; otherwise, Rage will mark the subscriber as successful and will not trigger a retry.class CreateShipment
include Rage::Events::Subscriber
subscribe_to OrderCreated, deferred: true
rescue_from Net::HTTPError do |exception|
Rage.logger.with_context(exception:) do
Rage.logger.error "Shipment API is unavailable"
end
raise exception
end
endconfig/openapi_components.yml) with a root components key. Reference these components in controllers using JSON Pointer syntax (e.g., #/components/schemas/Name).# config/openapi_components.yml
components:
schemas:
User:
type: object
properties:
id: { type: integer }
name: { type: string }class Api::V1::UsersController < ApplicationController
# @response 200 #/components/schemas/User
# @response 404 #/components/schemas/Error
def show
end
endclass UsersController < ApplicationController
def create
# create a user
end
endbefore_action callback runs code before a controller action executes. If render or head is called within a before_action, Rage halts request processing and skips the main controller action. This is commonly used for authentication and authorization.class UserStatusesController < ApplicationController
before_action do
@user = User.find_by(params[:id])
head :bad_request unless @user
end
def create
@user.activate!
end
endRage::SSE.stream to attach a client to a named stream and Rage::SSE.broadcast to send data to all connected clients. Streams can be identified by strings or arrays (composite keys).# Controller: Establish connection
render sse: Rage::SSE.stream([:notifications, params[:user_id]])
# Anywhere: Broadcast message
Rage::SSE.broadcast([:notifications, user.id], "New notification!")
# Anywhere: Close all connections on a stream
Rage::SSE.close_stream([:notifications, user.id])