Lennarb
Lennarb is a experimental lightweight, fast, and modular web framework for Ruby based on Rack.
image
Table of Contents
- About
- Installation
- Usage
- Routing
- Parameters
- Namespaces
- Middlewares
- Render HTML templates
- Render JSON
- TODO
- Routing
- Development
- Contributing
About
Lennarb is designed to be simple and easy to use, while still providing the power and flexibility of a full-featured web framework. Also, that's how I affectionately call my wife.
Installation
Add this line to your application's Gemfile:
gem 'lennarb'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install lennarb
Usage
After installing, you can begin using Lennarb to build your modular web applications with Ruby. Here is an example of how to get started:
app = Lenna::Base.new
app.get('/hello/:name') do |req, res|
name = req.params[:name]
res.html("Hello #{name}!")
end
app.listen(8000)
This example creates a new Lennarb application, defines a route for the /hello/:name
path, and then starts the application on port 8000. When a request is made to the /hello/:name
path, the application will respond with Hello <name>!
, where <name>
is the value of the :name
parameter in the request path.
Routing
Lennarb uses a simple routing system that allows you to define routes for your application. Routes are defined using the get
, post
, put
, patch
, delete
. These methods take three arguments: the path to match, an Array of the middlewares that can be apply on the current route and a block to execute when a request is made to the path. The block is passed two arguments: the request
object and the response
object. The request object contains information about the request, such as the request method, headers, and body. The response object contains methods for setting the response status code, headers, and body. Ex.
app.get('/hello') do |_req, res|
res.html('Hello World!')
end
The example above defines a route for the /hello
path. When a request is made to the /hello
path, the application will respond with Hello World!
.
Parameters
Lennarb allows you to define parameters in your routes. Parameters are defined using the :
character, followed by the name of the parameter. Parameters are passed to the route block as a hash in the request object's params
property.
app.get('/hello/:name') do |req, res|
name = req.params[:name]
res.html("Hello #{name}!")
end
The example above defines a route for the /hello/:name
path. When a request is made to the /hello/:name
path, the application will respond with Hello <name>!
, where <name>
is the value of the :name
parameter in the request path.
namespaces
Lennarb allows you to define namespaces in your routes. Namespaces are defined using the namespace
method on the application object. Namespaces are passed to the route block as a hash in the request object's params
property.
app.namespace('/api') do |router|
roter.get('/hello') do |_req, res|
res.html('Hello World!')
end
end
The example above defines a namespace for the /api
path. When a request is made to the /api/hello
path, the application will respond with Hello World!
.
Middlewares
The Lennarb application object has a use
method that allows you to add middleware to your application. Middleware is defined using the use
method on the application object. Ex.
app.get('/hello') do |_req, res|
res.html('Hello World!')
end
app.use(Lenna::Middleware::Logger)
You can also define middleware for specific route.
app.get('/hello', TimeMiddleware) do |_req, res|
res.html('Hello World!')
end
You can create your own middleware by creating a class that implements the call
method. This methods receive three
class TimeMiddleware
def call(req, res, next_middleware)
puts Time.now
req.headers['X-Time'] = Time.now
next_middleware.call
end
end
Or using a lambda functions.
TimeMiddleware = ->(req, res, next_middleware) do
puts Time.now
req.headers['X-Time'] = Time.now
next_middleware.call
end
So you can use it like this:
app.get('/hello', TimeMiddleware) do |_req, res|
res.html('Hello World!')
end
And you can use multiple middlewares on the same route.
app.get('/hello', [TimeMiddleware, LoggerMiddleware]) do |_req, res|
res.html('Hello World!')
end
Render HTML templates
Lennarb allows you to render HTML templates using the render
method on the response object. The render
method takes two arguments: the name of the template file, and a hash of variables to pass to the template.
app.get('/hello') do |_req, res|
res.render('hello', locals: { name: 'World' })
end
The example above renders the hello.html.erb
template, passing the name
variable to the template.
By default, Lennarb looks for templates in the views
directory in root path. You can change this specifying the path for views
in render method. Ex.
app.get('/hello') do |_req, res|
res.render('hello', path: 'app/web/templates', locals: { name: 'World' })
end
Render JSON
Lennarb allows you to render JSON using the json
method on the response object. The json
method takes one argument: the object to render as JSON.
app.get('/hello') do |_req, res|
res.json(data: { message: 'Hello World!' })
end
The example above renders the { message: 'Hello World!' }
object as JSON.
TODO
- Add support for mime types
- Add support for sessions
- Add support for websockets
- Add support for streaming
- Add support for CORS
- Add support for CSRF
- Add support for caching
- Add support for gzip compression
- Add support for SSL
- Add support for HTTP/2
- Add support for HTTP/3
Development
To set up the development environment after cloning the repo, run:
$ bin/setup
To run the tests, run:
$ rake test
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/aristotelesbr/lennarb.