{"title":"Configuration","description":"","section":"guides","version":"v1.5","path":"guides/configuration","canonical_url":"https://amberframework.org/docs/v1.5/guides/configuration","markdown_url":"https://amberframework.org/docs/v1.5/guides/configuration.md","inherited":true,"content_markdown":"# Configuration\n\nEvery Amber project has a `{project_name}/config` directory. This directory contains all the configuration files used by your Amber project.\n\n## Configuring Amber\n\nWithin the `config` directory of an Amber project, you will find several helpful configuration settings.\n\n### Environments\nEach Amber project ships with three default environments:\n\n* Development \n* Test\n* Production \n\nThe files for each environment are found under `{project_name}/config/environments`. Each Amber environment is configured in separate YAML files under this directory. \n\nAt runtime the framework will use the `AMBER_ENV` environment value to load the respective environment configuration into the application. For example if `AMBER_ENV=staging` the framework will expect a `config/environments/staging.yml` file to exist and attempt to load its configurations into the application.\n\n## Getting the current environment\n\nTo get the current Amber environment you can use the `Amber.env` method. \n\n```crystal\nAmber.env\n# returns => :production\n```\n\nAlternatively, you can use built in environment helpers such as `Amber.env.development?`, `Amber.env.test?`, and `Amber.env.production?` to check the environment value.\n\n## Setting the current environment\n\nAs mentioned previously, Amber sets the application environment based on the `AMBER_ENV` environment variable. By default, the environment is set to `development`. If you would like to set the environment to something else, you will need to configure the `AMBER_ENV` environment variable before running the server process.\n\nFor example to run Amber in the production environment, we can set the `AMBER_ENV` to `production`. \n\n```bash\nAMBER_ENV=production amber watch\n```\n\n### Initializers\n\nYou can use initializers to load configuration settings into various application components like mailers after all of the frameworks and shards are loaded. \n\nTo add a new initializer simply create a file and save it under `config/initializers` to load at runtime.\n\nFor example, to initialize a `Quartz` mailer:\n\n```crystal\n# config/initializers/mailer.cr\n\nrequire \"quartz_mailer\"\n\nQuartz.config do |c|\n  c.smtp_enabled = Amber.settings.smtp.enabled\n\n  c.smtp_address = ENV[\"SMTP_ADDRESS\"]? || Amber.settings.smtp.host\n  c.smtp_port = ENV[\"SMTP_PORT\"]? || Amber.settings.smtp.port\n  c.username = ENV[\"SMTP_USERNAME\"]? || Amber.settings.smtp.username\n  c.password = ENV[\"SMTP_PASSWORD\"]? || Amber.settings.smtp.password\n\n  c.use_authentication = !c.password.blank?\nend\n```\n\n### Application.cr\n\nThe main entry point for an Amber application is `config/application.cr`. This application file allows you to overwrite settings using dynamic values. It also allows you to use environment variables available to the application process to configure your application.\n\n```crystal\n# config/application.cr \n\nAmber::Server.configure do |app|\n  app.name = ENV[\"APP_NAME\"] if ENV[\"APP_NAME\"]?\n  app.host = ENV[\"HOSTNAME\"] if ENV[\"HOSTNAME\"]?\n  app.logger = ::Logger.new(STDOUT)\n  app.logger.level = ::Logger::INFO\nend\n```\n\n## Where do custom settings go?\n\nAmber makes it easy to manage custom settings in each of the environment YAML files. You can include new settings in any of the environment YAML file by specifying them in the secrets section.\n\n```yaml\n# config/environments/development.yml\n\ndatabase_url: postgres://postgres:@localhost:5432/test_development\nsecrets: \n  custom: secret value here\n```\n\n## How do I access my custom settings?\n\nIn your application, you can access settings from your environment YAML files using the `Amber.settings.secrets` Hash\n\n```crystal\nAmber.settings.secrets[\"custom\"] # =>  \"secret value here\"\n```\n\n## Encrypted Environment Settings\n\nWith Amber you can encrypt your environment setting using `amber encrypt {environment}`, this command will open your editor to allow make changes if needed and then encrypt the file `{environment}.enc`.\n\nA `.encryption_key` file is provided. It contains a `secret_key_base` that is used to decrypt your encrypted environment settings. This file is added to `.gitignore` to avoid inclusion in version control. \n\n{% hint style=\"danger\" %}\nWithout the encryption key, you won't be able to decrypt your environment settings.\n{% endhint %}\n\n{% hint style=\"danger\" %}\n**Never commit the encryption key!**\n\nSetup your production machine and use the variable `AMBER_ENCRYPTION_KEY`\n\nSee more on [encrypt section](../cli/encrypt.md)\n{% endhint %}"}