Rack environment

Actions offer a high level API built on top of Rack. To access the raw data from the Rack environment within an action, use request.env.

  1. module Bookshelf
  2. module Actions
  3. module Books
  4. class Show < Bookshelf::Action
  5. def handle(request, response)
  6. request.env
  7. request.env["REQUEST_METHOD"] #=> GET
  8. request.env["PATH_INFO"] #=> /books/1
  9. end
  10. end
  11. end
  12. end
  13. end

Rack middleware

Hanami mounts a thin default middleware stack. Additional middleware can be mounted either at application level, in the router, or for an individual action.

Inspecting the middleware stack

Use the hanami middleware command to inspect the Rack middleware stack.

  1. $ bundle exec hanami middleware
  2. / Dry::Monitor::Rack::Middleware (instance)

Application-level middleware

To add middleware at an application level, use the middleware config on your app:

  1. # config/app.rb
  2. module Bookshelf
  3. class App < Hanami::App
  4. config.middleware.use Rack::Auth::Basic
  5. end
  6. end
  1. $ bundle exec hanami middleware
  2. / Dry::Monitor::Rack::Middleware (instance)
  3. / Rack::Auth::Basic

Middleware will be included in the stack in the order in which it’s added.

  1. # config/app.rb
  2. module Bookshelf
  3. class App < Hanami::App
  4. config.middleware.use Rack::Auth::Basic
  5. config.middleware.use Rack::MethodOverride
  6. end
  7. end
  1. $ bundle exec hanami middleware
  2. / Dry::Monitor::Rack::Middleware (instance)
  3. / Rack::Auth::Basic
  4. / Rack::MethodOverride

If needed, you can use before: or after: to insert a middleware at a particular point in the stack:

  1. # config/app.rb
  2. module Bookshelf
  3. class App < Hanami::App
  4. config.middleware.use Rack::Auth::Basic
  5. config.middleware.use Rack::MethodOverride
  6. config.middleware.use Rack::ShowStatus, before: Rack::Auth::Basic
  7. end
  8. end
  1. $ bundle exec hanami middleware
  2. / Dry::Monitor::Rack::Middleware (instance)
  3. / Rack::ShowStatus
  4. / Rack::Auth::Basic
  5. / Rack::MethodOverride

Router middleware

Middleware can also be added via your routes configuration in config/routes.rb. Middleware added here is added after any middleware added via app config.

  1. # config/routes.rb
  2. module Bookshelf
  3. class Routes < Hanami::Routes
  4. use Rack::Runtime
  5. root { "Hello from Hanami" }
  6. end
  7. end

Adding middleware via your routes config lets you add middleware to specific routes.

  1. # config/routes.rb
  2. module Bookshelf
  3. class Routes < Hanami::Routes
  4. use Rack::Runtime
  5. root { "Hello from Hanami" }
  6. scope "restricted" do
  7. use Rack::Auth::Basic
  8. get "/page" do
  9. "Some restricted content"
  10. end
  11. end
  12. slice :admin, at: "/admin" do
  13. use Rack::Auth::Basic
  14. get "/books", to: "books.index"
  15. end
  16. end
  17. end
  1. $ bundle exec hanami middleware
  2. / Dry::Monitor::Rack::Middleware (instance)
  3. / Rack::Runtime
  4. /restricted Rack::Auth::Basic
  5. /admin Rack::Auth::Basic

Using config.ru

Middleware can also be implemented in config.ru. This middleware will not be part of the Hanami application’s middleware stack.

  1. # config.ru
  2. require "hanami/boot"
  3. use Rack::Static, :urls => ["/public"]
  4. run Hanami.app