Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Elixir Cookbook

You're reading from   Elixir Cookbook Unleash the full power of programming in Elixir with over 60 incredibly effective recipes

Arrow left icon
Product type Paperback
Published in Feb 2015
Publisher Packt
ISBN-13 9781784397517
Length 236 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Paulo Pereira Paulo Pereira
Author Profile Icon Paulo Pereira
Paulo Pereira
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Command Line FREE CHAPTER 2. Data Types and Structures 3. Strings and Binaries 4. Modules and Functions 5. Processes and Nodes 6. OTP – Open Telecom Platform 7. Cowboy and Phoenix 8. Interactions A. Installation and Further Reading Index

Managing application configuration

Mix tasks run in a specific environment. The predefined environments are production, development, and test (prod, dev, and test). The default environment is dev. In this recipe, we will configure an application with different values for each environment. Invoking the same function will result in a different output based on the configuration.

How to do it…

To manage an application configuration, we follow these steps:

  1. Create a new application:
    > mix new config_example
    
  2. Go to the generated application directory and open config/config.exs.
  3. Replace all of the file's content with the following code:
    use Mix.Config
    
    config :config_example,
      message_one: "This is a shared message!"
    
    import_config "#{Mix.env}.exs"
  4. Create three more files under the config directory with the following code:
    • In config/dev.exs, add the following:
      use Mix.Config
      
      config :config_example,
        message_two: "I'm a development environment message!"
    • In config/prod.exs, add this code:
      use Mix.Config
      
      config :config_example,
        message_two: "I'm a production environment message!"
    • In config/test.exs, add the following:
      use Mix.Config
      
      config :config_example,
        message_two: "I'm a test environment message!"
  5. Define two module attributes in lib/config_example.ex to hold the values of message_one and message_two, as follows:
    @message_one Application.get_env(:config_example, :message_one)
    @message_two Application.get_env(:config_example, :message_two)
  6. Create a show_messages function in lib/config_example.ex, like this:
    def show_messages do
      IO.puts "Message one is: #{@message_one}"
      IO.puts "Message two is: #{@message_two}"
    end
  7. Start the application in the three different environments and see the output of the show_messages function:
    • For the development environment, start the application as follows:
      > MIX_ENV=dev iex –S mix
      iex(1)> ConfigExample.show_messages
      Message one is: This is a shared message!
      Message two is: I'm a development environment message!
      :ok
      iex(2)>
      
    • For the production environment, start the application like this:
      > MIX_ENV=prod iex –S mix
      iex(1)> ConfigExample.show_messages
      Message one is: This is a shared message!
      Message two is: I'm a production environment message!
      :ok
      iex(2)>
      
    • For the test environment, start the application as follows:
      > MIX_ENV=test iex –S mix
      iex(1)> ConfigExample.show_messages
      Message one is: This is a shared message!
      Message two is: I'm a test environment message!
      :ok
      iex(2)>
      

How it works…

When we include the last line in config.exs (import_config "#{Mix.env}.exs"), the Mix configuration is loaded from the files, in this case with the Mix environment as its name and .exs as its extension.

The configuration from the imported files will override any existing configuration (with the same key) in the config.exs file. In fact, Configuration values are merged recursively. See the example at https://github.com/alco/mix-config-example.

To access configuration values, we use Application.get_env(:app, :key).

You have been reading a chapter from
Elixir Cookbook
Published in: Feb 2015
Publisher: Packt
ISBN-13: 9781784397517
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime