A lightweight, fast, and modular web framework for Ruby based on Rack. Lennarb supports Ruby (MRI) 3.4+
Table of Contents
Features
-
Lightweight and modular architecture
-
High-performance routing system
-
Simple and intuitive API
-
Support for middleware
-
Flexible configuration options
-
Two implementation options:
-
Lennarb::App
: Minimalist approach for single applications -
Lennarb::Base
: Extended version for mounting multiple applications
Installation
Add this line to your application’s Gemfile:
gem 'lennarb'
Or install it directly:
gem install lennarb
Quick Start
Create a simple application with routes:
require "lennarb"
app = Lennarb::App.new do
get("/") do |req, res|
res.html("<h1>Welcome to Lennarb!</h1>")
end
get("/hello/:name") do |req, res|
name = req.params[:name]
res.html("Hello, #{name}!")
end
end
app.initialize!
run app # In config.ru
Start with: rackup
Basic Usage
Creating a Simple Application
The Lennarb::App
class is the core of the framework:
require "lennarb"
class MyApp < Lennarb::App
# Define configuration
config do
mandatory :database_url, string
optional :port, int, 9292
end
get("/") do |req, res|
res.html("<h1>Welcome!</h1>")
end
post("/users") do |req, res|
# Access request data
data = req.body
res.json({status: "created", data: data})
end
end
# Define hooks
before do |req, res|
# Run before every request
puts "Processing request: #{req.path}"
end
after do |req, res|
# Run after every request
puts "Completed request: #{req.path}"
end
# Define helper methods
helpers do
def format_date(date)
date.strftime("%Y-%m-%d")
end
end
end
run MyApp.new.initialize!
Response Types
Lennarb provides various response methods:
# HTML response
res.html("<h1>Hello World</h1>")
# JSON response
res.json({message: "Hello World"})
# Plain text response
res.text("Plain text response")
# Redirect
res.redirect("/new-location")
# Custom status code
res.json({error: "Not found"}, status: 404)
Mounting Applications
For larger applications, use Lennarb::Base
to mount multiple apps:
class API < Lennarb::App
get("/users") do |req, res|
res.json([{id: 1, name: "Alice"}, {id: 2, name: "Bob"}])
end
end
class Admin < Lennarb::App
get("/dashboard") do |req, res|
res.html("<h1>Admin Dashboard</h1>")
end
end
class Application < Lennarb::Base
# Add common middleware
middleware do
use Rack::Session::Cookie, secret: "your_secret"
end
# Mount applications at specific paths
mount(API, at: "/api")
mount(Admin, at: "/admin")
end
run Application.new.initialize!
Documentation
For more detailed information, please see:
-
Getting Started - Setup and first steps
-
Response - Response handling
-
Mounting Applications - Working with multiple apps
-
Performance - Benchmarks showing Lennarb’s routing algorithm efficiency
Contributing
-
Fork the repository
-
Create your feature branch (
git checkout -b feature/amazing-feature
) -
Commit your changes (
git commit -am 'Add amazing feature'
) -
Push to the branch (
git push origin feature/amazing-feature
) -
Open a Pull Request
This project uses the Developer Certificate of Origin and is governed by the Contributor Covenant.
License
MIT License - see the LICENSE file for details.