Lucky Logo

# Default Lucky environments

Often times your configuration will change based on the environment Lucky is running in.

The default environments in a Lucky app are:

  • development
  • test
  • production

# Get the current environment

LuckyEnv.environment will return the currently running environment

You can also conditionally check the environment with LuckyEnv.development?, LuckyEnv.test?, or LuckyEnv.production?

# Set the current environment

LUCKY_ENV=environment_name to set the environment. By default it is development.

# Add custom environment

If you need a custom environment like staging, for example, you can add this option in config/ This will give you the helper method LuckyEnv.staging? for use in your app.

LuckyEnv.add_env :staging

Make sure config/ accounts for the new environment. Otherwise your app may not start properly.

# Configuring Lucky

By default there are some files in the config/ folder that are used to configure Lucky. Look in config/ to configure things like server port, database host and password, etc.

For example, to change the database config, look in config/

# Where to put new settings

All application configuration is found in the config/ folder.

If you want to add additional config, create a new file in the config/ folder. Lucky will require these files for you automatically.

# Configuring your own code

Lucky ships with Habitat. A module you can use to configure your application in a type safe way. For a thorough look at how to use Habitat, check out the Habitat documentation.

As a quick example, if you had a class for handling payments through Stripe, you may want to include an API token. Here’s how you would do that.

# src/models/
class MyStripeProcessor
  Habitat.create do
    setting api_token : String

Then you can configure it:

# config/
MyStripeProcessor.configure do |settings|
  settings.api_token = "super secret"

# Using ENV variables for configuration

In reality you would likely want to use an ENV variable for this. To do that, add a .env file to your project root and change your configuration to use the ENV variable:

# .env
# this will be loaded in development when you call `lucky dev`

And in your config file:

# config/
MyStripeProcessor.configure do |settings|
  settings.api_token = ENV.fetch("STRIPE_API_TOKEN")

Remember to set the ENV variable in production and you’ll be set!

See a problem? Have an idea for improvement? Edit this page on GitHub