Hanami supports different environments based on a HANAMI_ENV environment variable.

Setting HANAMI_ENV allows your code to act differently, depending on the environment.

If HANAMI_ENV is not set, Hanami will fallback to checking RACK_ENV. If neither variable is set, the environment defaults to :development.

By convention, Hanami expects HANAMI_ENV to be either development, test, or production.

Environment helpers

Use the following helpers if your code needs behave differently in different environments.

Hanami.env

Hanami.env returns a symbol representing the current environment.

  1. # HANAMI_ENV=development
  2. Hanami.env
  3. => :development
  1. # HANAMI_ENV=test
  2. Hanami.env
  3. => :test
  1. # HANAMI_ENV=production
  2. Hanami.env
  3. => :production

Hanami.env?

Hanami.env?(*names) returns true if the given name(s) match the current environment.

  1. # HANAMI_ENV=development
  2. Hanami.env?(:development)
  3. => true
  4. Hanami.env?(:test)
  5. => false
  6. Hanami.env?(:production)
  7. => false

You can match on more than one environment:

  1. # HANAMI_ENV=development
  2. Hanami.env?(:development, :test)
  3. => true
  1. # HANAMI_ENV=test
  2. Hanami.env?(:development, :test)
  3. => true
  1. # HANAMI_ENV=production
  2. Hanami.env?(:development, :test)
  3. => false

Environment specific app config

Config options that are environment specific can be set on the app class. For example:

  1. # config/app.rb
  2. module Bookshelf
  3. class App < Hanami::App
  4. environment(:production) do
  5. # Production specific config or initialization
  6. config.middleware.use ProductionOnlyMiddleware
  7. end
  8. end
  9. end

See the app config guide for information on supported config options.

Production deployments

When deploying your application to production, set the HANAMI_ENV environment variable to production.

In production, Hanami logs to standard out by default, using a structured JSON format with a log level of :info rather than :debug, which is used in development and test. See the logger guide for more detail.