Eksa Framework v4.0.0 ๐
Eksa Framework is a modern, lightweight Ruby web framework built upon the Eks Interface standard. It is engineered for speed, high-level security, and flexibility without the bloat of massive external dependencies. With v4.0.0, Eksa Framework now supports advanced architectural features such as URL Mapping and Application Cascading.
โจ Key Features in v4.0.0
- ๐ค Modern Routing DSL: Intuitive route definition with dynamic parameters (
:name), namespaces, and execution control (halt). - ๐บ URL Mapping & Cascading: Run multiple independent applications under a single server based on sub-paths (path mapping) or automatic fallback.
- ๐ Security First: HMAC-SHA256 encrypted sessions, automatic XSS protection, built-in security headers, and parameter limiting (DoS protection).
- ๐ Standard Middleware Suite:
Runtime: Monitor performance with theX-Runtimeheader.MethodOverride: UsePUT/DELETEfrom standard HTML forms.Head: Automatic handling ofHEADrequests.Session,Logger,Static,ShowExceptions, etc.- ๐จ Smart Templating: ERB integration with an Auto-Layout system,
@req/@resobject injection, and secure@hhelper for HTML escaping. - ๐งช First-Class Testing: Built-in testing infrastructure using
MockRequestandMockResponse. - โก Production Ready: Full integration with Eksa-Server (Cluster mode & Workers) and the
ekscentupCLI tool.
๐ Quick Start (v4 Style)
Create a config.eks file to define your application:
# 1. Global Middlewares
use Eksa::Middleware::Runtime
use Eksa::Middleware::MethodOverride
use Eksa::Middleware::Session
use Eksa::Middleware::Logger
# 2. API Application Mapping
map "/api" do
api = Eksa::Router.new do
get '/status' do |req, res|
res.content_type = 'application/json'
res.write({ status: 'online', version: Eksa::VERSION }.to_json)
end
end
run api
end
# 3. Main Web Application
router = Eksa::Router.new do
get '/' do |req, res|
req.session['visits'] ||= 0
req.session['visits'] += 1
res.write "<h1>Main Web v#{Eksa::VERSION}</h1>"
res.write "<p>Your visits: #{req.session['visits']}</p>"
end
end
run router
Run it using the command:
ekscentup -R --port 3000
๐ค Routing Documentation
Router DSL
You can define routes using standard HTTP methods (get, post, put, delete, patch, options, any).
router = Eksa::Router.new do
get '/user/:id' do |req, res|
id = req.params['id']
res.write "User ID: #{id}"
end
namespace '/admin' do
get '/dashboard' do |req, res|
# Accessed via /admin/dashboard
end
end
# Execution Control
get '/secret' do |req, res|
halt(403, "Access Denied") unless req.session['admin']
res.write "Secret Data"
end
end
URL Mapping & Cascade
Use map to split large applications into smaller sub-apps. Use cascade if you want to try multiple applications sequentially until one responds (other than a 404).
๐ฆ Built-in Middleware
| Middleware | Description |
|---|---|
Eksa::Middleware::Runtime |
Adds an X-Runtime header with execution time. |
Eksa::Middleware::MethodOverride |
Allows overriding HTTP methods via the _method parameter. |
Eksa::Middleware::Session |
Secure, HMAC-based cookie session management. |
Eksa::Middleware::Logger |
Logs requests to STDOUT or a log file. |
Eksa::Middleware::Static |
Serves static files from a specific directory (e.g., public). |
Eksa::Middleware::ShowExceptions |
Displays an informative error page when a crash occurs. |
Eksa::Middleware::ContentSecurity |
Adds standard security headers (X-Content-Type, X-Frame-Options). |
Eksa::Middleware::Head |
Automatically empties the body for HEAD requests. |
๐จ Templating & Layout
Place your .erb files in the views/ directory. By default, the framework will look for views/layout.erb as the main wrapper.
views/layout.erb:
<html>
<body>
<header>My App</header>
<%= @content %> <!-- Rendered content will be injected here -->
</body>
</html>
Within the Router:
res.render 'index', title: "Hello World"
Context Injection: The @req (request) and @res (response) objects, as well as the @h helper (HTML escape), are always available within templates.
๐ API Guide (Request & Response)
Eksa::Request (req)
req.params: Retrieve query, POST, or route parameters.req.session: Access session data (Read/Write).req.request_method: Get the HTTP method (GET, POST, etc.).req.path: Get the current URL path.req.user_agent: Get browser information.
Eksa::Response (res)
res.write(string): Append content to the response body.res.set_header(key, value): Set an HTTP header.res.content_type = 'type': Shortcut to set Content-Type.res.status = code: Set status code (default: 200).res.redirect(path): Perform a URL redirect.res.render(template, locals): Render an ERB template.
๐งช Testing
Use the built-in testing suite to ensure your application behaves correctly:
require 'test/unit'
require 'eksa'
class MyAppTest < Test::Unit::TestCase
def test_homepage
app = Eksa.load('config.eks')
mock = Eksa::MockRequest.new(app)
res = mock.get('/')
assert res.ok?
assert_match "Welcome", res.body_content
end
end
๐ก Security Limits (Eks Limits)
You can set limits on parameter parsing via environment variables to prevent DoS attacks:
EKS_QUERY_PARSER_PARAMS_LIMIT: Maximum number of parameters (default: 1000).EKS_QUERY_PARSER_DEPTH_LIMIT: Maximum nested parameter depth.EKS_MULTIPART_TOTAL_PART_LIMIT: Maximum parts in a multipart form.
๐ License
Eksa Framework v4.0.0 is published under the MIT License.