A complete http router solution that fit well with pure Rack apps
gem install rackr
# config.ru
App =
Rackr.new.call do
# Returns [200, {"Content-Type" => "text/html"}, ["<h1> rack http_router </h1>"]]
get { html('<h1> rack http_router </h1>') }
r 'v1' do
r 'hi' do
get { html('<h1> rack http_router </h1>') }
end
end
# Build a r /v2
r 'v2' do
# get /v2/hello/somename
get 'hello/:name' do |req| # 'req' is an Rack::Request object
# Returns [200, {"Content-Type" => "application/json"}, [Oj.dump({name: 'somename'}, compat: true)]]
json({ name: req.params[:name] })
end
end
# The router can also receive a class that responds to call(req)
get 'my-controller', MyController::Index
# renders an index erb located in /views
get 'my-view' do
view 'index', { name: "Henrique" }
end
not_found do
html "Are you lost?"
end
# post
# patch
# delete
# options
end
run App
We can also transform a class in a "rackr action" including Rackr::Action module:
module MyController
class Index
include Rackr::Action
def call(req)
json({say: "hello"})
end
end
end
# config.ru
use Rack::Static, :urls => ["/public"] # Add paths for your public content
# 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.
use https://github.com/alexch/rerun
use https://github.com/baldowl/rack_csrf
Contact me if you have any issue:
I will be always open for tips, improvements, new ideas and new contributors!