rackr
A Ruby ​​web micro-framework made for study purposes 📖
Installation:
gem install rackrQuick Start
This is a pure Rack application:
# config.ru
run do |env|
  [200, {'content-type' => 'text/html'}, ["<h1>Hello!</h1>"]]
endThis 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 contentWork 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
Feel free to get the idea, fork, contribute and do whatever you want!
Contact me if you have any issue:
I will be always open for tips, improvements, new ideas and new contributors!
