Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate `hanami-router` `v2.0.0.alpha2` #1032

wants to merge 10 commits into
base: unstable


Copy link

jodosha commented Mar 6, 2020


Integrate hanami-router v2.0.0.alpha2.

Technical details


The latest hanami-router (the standalone gem) expects Rack endpoints to be passed in the route definition. It doesn't long support the syntax "home#index" to define an action.

That gem now has the concept of resolver that can be passed to Hanami::Router#initialize. Third-party gems (like hanami) can pass their own initializer to resolve endpoints starting from a path (e.g. "/admin/users") and an endpoint identifier (e.g. "users#index").

This is the role of the new class: Hanami::Application::Routing::Resolver.

Main slice

Hanami 2 will support multiple slices (formerly known as "apps") in the same app. When a developer will generate a new app with hanami new we will generate a default slice named Main.

The definition of the routes of each slice must be grouped by the Hanami::Application::Router#slice block. Example:

# config/routes.rb
Hanami.application.routes do
  slice :main, at "/" do
    root to: "home#index"

  slice :admin, at: "/admin" do
    get "/users", to: "users#index"

Application Router

We can't use directly Hanami::Router, but inherit from it, because we need more features for the full stack experience.

Rack middleware

There are two ways to add a Rack middleware to the application stack:

  1. Add it to the application configuration. This will affect all the endpoints of the app.
  2. Add it to the router for fine-grained control.

Application configuration

# config/application.rb
module Soundeck
  class Application < Hanami::Application
    config.use MyGlobalRackMiddleware

In this case MyGlobalRackMiddleware will be used by all the endpoints of the app.

This is important if third-party gems want to add their own Rack middleware to the application stack.


# config/routes.rb
Hanami.application.routes do
  use ElapsedTimeMiddleware

  get "/login", to: "sessions#new"

  scope "api" do
    use APIAuthentication

    get "/", to: "api#index"

Rack Middleware in routes is hierarchical, similar to variable definitions. The outer the declaration, the broader the scope.

In this example, ElapsedTimeMiddleware is declared in the top-level scope, so it will be applied to all the routes. This is equivalent to MyGlobalRackMiddleware of the previous section.

APIAuthentication is declared in the "api" scope, so it will be used only by that narrowed section of the routes. For instance, it will be applied to "api#index", but not to "sessions#create"

@jodosha jodosha added this to the v2.0.0 milestone Mar 6, 2020
@jodosha jodosha self-assigned this Mar 6, 2020
@jodosha jodosha requested review from timriley and hanami/core Mar 26, 2020
jodosha added 10 commits Mar 6, 2020
…e slices in router.
…cept only one argument
@jodosha jodosha force-pushed the hanami-router-2-alpha2 branch from 0b85bbe to d76a0cb Mar 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

1 participant
You can’t perform that action at this time.