Skip to content

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

Notifications You must be signed in to change notification settings

henrique-ft/rackr

Repository files navigation

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:

[email protected]

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

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published