Configuration and environment

Defaults in code

A program that needs a setting should run without one. The defaults are a frozen hash in the code, and everything else is a layer on top of it:

DEFAULTS = { "APP_MODE" => "development", "PORT" => "3000" }

The order is fixed: defaults, then the file, then the environment. The environment wins because that is what a container and a CI job can set without editing a file, and it is the layer a deploy changes.

A .env file

A .env file is a convenience for a laptop: one KEY=VALUE per line, comments with #. A dozen lines of Ruby parse it, which is less than a dependency:

def load_dotenv(path) return {} unless File.exist?(path) File.readlines(path).filter_map do |line| line = line.strip next if line.empty? || line.start_with?("#") key, value = line.split("=", 2) [key, value] end.to_h end

split("=", 2) is the detail that makes it work: a password or a URL with an equals sign keeps everything after the first one. The real dotenv gem adds quoting and interpolation; for the three settings a script has, this is enough.

The file the second run uses holds three settings:

# .env APP_MODE=production PORT=8080 TOKEN=secret

The loader

The complete app.rb below puts the three layers together and keeps the one line of output that proves which value won:

DEFAULTS = { "APP_MODE" => "development", "PORT" => "3000" } def load_dotenv(path) return {} unless File.exist?(path) File.readlines(path).filter_map do |line| line = line.strip next if line.empty? || line.start_with?("#") key, value = line.split("=", 2) [key, value] end.to_h end settings = DEFAULTS.merge(load_dotenv(".env")).merge(ENV) token = settings.key?("TOKEN") ? "set" : "missing" puts "mode=#{settings["APP_MODE"]} port=#{settings["PORT"]} token=#{token}"

The token is printed as set or missing, never as its value. A script that prints the secret once puts it in a shell history, a log and a screenshot.

Running it

Without a file, the defaults answer:

ruby app.rb

mode=development port=3000 token=missing

With a .env next to the script, the file wins over the defaults:

ruby app.rb

mode=production port=8080 token=set

And the environment wins over the file, which is what a container sets:

APP_MODE=test PORT=9999 ruby app.rb

mode=test port=9999 token=set

The same three commands are the whole twelve-factor idea for a small script: the build is the same everywhere, and the environment decides which configuration it gets.

What to watch for

Do not commit .env. Commit .env.example with the keys and empty values, and add .env to .gitignore; the example is the documentation and the secret stays out.

ENV values are strings. A port read from the environment and used as a number needs Integer(ENV.fetch("PORT")), or the first comparison fails with a message about comparing a String with an Integer.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? DEFAULTS = { "APP_MODE" => "development", "PORT" => "3000" } file = { "APP_MODE" => "production" } settings = DEFAULTS.merge(file) puts settings["APP_MODE"] puts settings["PORT"]

    Answer

    production / 3000

    merge replaces only the key the file holds, so the port keeps its default.

  2. What does this program print? DEFAULTS = { "APP_MODE" => "development" } file = { "APP_MODE" => "production", "PORT" => "8080" } env = { "APP_MODE" => "test" } settings = DEFAULTS.merge(file).merge(env) puts settings["APP_MODE"] puts settings["PORT"]

    Answer

    test / 8080

    The environment is merged last, so it wins for APP_MODE, and the port comes from the file.

  3. What does this program print? line = "TOKEN=abc=def" key, value = line.split("=", 2) puts key puts value

    Answer

    TOKEN / abc=def

    The limit 2 splits on the first equals sign only, so a value may contain another one.

  4. What order do the three configuration layers follow?

    Answer

    Defaults, file, environment

    Each layer overrides the one before it, and the environment is the last word.

  5. Why does the loader print set or missing instead of the token?

    Answer

    Because a printed secret lands in a shell history, a log and a screenshot

    The output proves the setting exists without publishing its value.

  6. What does this program print? settings = { "TOKEN" => "secret" } puts settings.key?("TOKEN") ? "set" : "missing"

    Answer

    set

    key? answers whether the name is present, and the ternary chooses the word to print.

  7. Which file should be committed when a project reads a .env?

    Answer

    A .env.example with the keys and empty values

    The example documents the keys, while the real file stays out of the repository.

  8. What type does ENV.fetch("PORT") return?

    Answer

    A String

    The environment is text, so a number needs Integer() before arithmetic.

  9. Why is split("=", 2) better than split("=") for a .env line?

    Answer

    It keeps the rest of the value after the first equals sign

    A URL or a password may contain an equals sign, and the limit keeps it in one value.

  10. When the layers are merged as defaults, then file, then environment, which value of APP_MODE does the program use?

    Answer

    The value the environment holds

    The merge order decides the winner, and the deploy sets the environment without editing a file.

Article author: Arthur Isaev