Often times your configuration will change based on the environment Lucky is running in.
The default environments in a Lucky app are:
LuckyEnv.environment will return the currently running environment
You can also conditionally check the environment with
LUCKY_ENV=environment_name to set the environment. By default it is
If you need a custom environment like
staging, for example, you can add this option in
This will give you the helper method
LuckyEnv.staging? for use in your app.
By default there are some files in the
config/ folder that are used to configure Lucky.
config/ to configure things like server port, database host and password, etc.
For example, to change the database config, look in
All application configuration is found in the
If you want to add additional config, create a new file in the
Lucky will require these files for you automatically.
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
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/my_stripe_processor.cr class MyStripeProcessor Habitat.create do setting api_token : String end end
Then you can configure it:
# config/my_stripe_processor.cr MyStripeProcessor.configure do |settings| settings.api_token = "super secret" end
In reality you would likely want to use an ENV variable for this. To do that,
.env file to your project root and change your configuration to use the
# .env # this will be loaded in development when you call `lucky dev` STRIPE_API_TOKEN=123abc
And in your config file:
# config/my_stripe_processor.cr MyStripeProcessor.configure do |settings| settings.api_token = ENV.fetch("STRIPE_API_TOKEN") end
Remember to set the ENV variable in production and you’ll be set!