No release in over a year
A complete http router solution that fit well with pure rack apps.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

~> 1.12
>= 2.0, < 4.0
~> 3.15
 Project Readme

rackr

Gem Version

A Ruby ​​web micro-framework made for study purposes 📖

Installation:

gem install rackr

Quick Start

This is a pure Rack application:

# config.ru
run do |env|
  [200, {'content-type' => 'text/html'}, ["<h1>Hello!</h1>"]]
end

This is a minimal Rackr application:

# config.ru
require 'rackr'

run (Rackr.new.call do
  get do |req|
    [200, {'content-type' => 'text/html'}, ["<h1>Hello!</h1>"]]
  end
end)

As you can imagine, there are function defined for other http methods: get, post, delete, put, trace, options, patch and all work in the same way. The block receives a param that is just a instance of Rack::Request (Rack::Request.new(env)). But is not obrigatory if we will not use it.

This is the same Rackr application using the render helper:

# config.ru
require 'rackr'

run (Rackr.new.call do
  get do |req|
    render html: "<h1>Hello!</h1>" # [200, {'content-type' => 'text/html'}, ["<h1>Hello!</h1>"]]
  end
end)

In addition to html:, there are other render options: json:, text: and also view:.

Now lets scope our get endpoint to a 'v1/hello' path:

# config.ru
require 'rackr'

run (Rackr.new.call do
  scope 'v1' do
    scope 'hello' do
      get do |req|
        render html: "<h1>Hello!</h1>"
      end
    end
  end
end)

And say hello to specific name in 'v1/hello/somename'

# config.ru
require 'rackr'

run (Rackr.new.call do
  scope 'v1' do
    scope 'hello' do
      scope :name do
        get do |req|
          # Rackr inject request params in the Rack::Request object
          render html: "<h1>Hello #{req.params[:name]}!</h1>"
        end
      end
    end
  end
end)

Maybe we can do it with fewer lines:

# config.ru
require 'rackr'

run (Rackr.new.call do
  get 'v1/hello/:name' do |req|
    render html: "<h1>Hello #{req.params[:name]}!</h1>"
  end
end)

If our app grows, we can create a "rackr action" including Rackr::Action module in a class and creating a call instance method:

# config.ru
require 'rackr'

class HelloAction
  include Rackr::Action

  def call(req)
    render html: "<h1>Hello #{req.params[:name]}!</h1>"
  end
end

run (Rackr.new.call do
  get 'v1/hello/:name', HelloAction
end)

Usefull resources

Rack Docs

How to

Serve static files:

# config.ru
use Rack::Static, :urls => ["/public"] # Add paths for your public content

Work with sessions:

# config.ru
use Rack::Session::Cookie,
    :key => 'rack.session', # Key for Rack 
    :expire_after => 2592000, # Expiration date
    :secret => ENV['MY_SECRET_KEY'] # Your secret key. 

Development: Auto refresh

use https://github.com/alexch/rerun

Development: Live reload

use https://github.com/jaredmdobson/rack-livereload

CRSF protection:

use https://github.com/baldowl/rack_csrf

HTTP Cache:

use https://github.com/rtomayko/rack-cache

Logs:

https://www.rubydoc.info/github/rack/rack/Rack/CommonLogger

Benchmarks (r10k)

Running in Ruby 3.3.0

rps_3

Feel free to get the idea, fork, contribute and do whatever you want!

Contact me if you have any issue:

hriqueft@gmail.com

I will be always open for tips, improvements, new ideas and new contributors!