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
 Dependencies

Runtime

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

rackr

A complete http router solution that fit well with pure Rack apps

Overview

Installation:

gem install rackr

Use example:

# 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

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. 

Auto refresh the server for development:

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

CRSF protection:

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

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!