Setting and resolving environment configurations

We've already alluded to the fact that you can set configuration for environ to consume from either environment variables or from Java system properties. This covers about 100% of real-world usage. However, during development, we can also set environment configuration using the lein-environ plugin (which is also packaged as part of a Luminus-generated application).

When running the development server using lein ring server, the lein-environ plugin will fetch environment configuration from the Leiningen project map, as well as an optional profiles.clj file, and merge the two together into the .lein-env file. This .lein-env file is the first go-to place for environment settings, but only when running the development server.

Note

It is recommended that the profiles.clj and .lein-env files not be committed into your version control, as every developer's environment is likely to be somewhat different. Plus, despite the fact that .lein-env supports fetching configuration from ~/.lein/profiles.clj, I consider that a nasty practice; an application's configuration changes from environment to environment, and configurable requirements change from application to application. So, setting what amounts to a global developer configuration for all applications has a pretty bad smell to it.

In the case of our :db-user example, we could put the following in a profiles.clj file located in hipstr's project folder:

{:dev {:env {:db-user "bunny"}}}

We aren't restricted to defining a single environment in the profiles.clj file. We can define multiple environments by doing the following:

{:dev {:env {:db-user "bunny"}}
 :test {:env {:db-user "test-bunny"}}}

We tell our development server which profile to use when we launch the development server, using the with-profile argument:

# lein with-profile dev ring server

The lein ring server command defaults to :dev if no with-profile is declared. Hence, consider the following code:

# lein with-profile test ring server

On executing the above code, our (env :db-user) would resolve to test-bunny.

Resolving environment configuration

The environ library will check three different places for a matching configuration key:

  • The .lein-env file
  • Exported environment variables
  • Java system properties

If the requested configuration key isn't found in .lein-env, then environ will check any exported environment variables, and, if still not found, it will check the Java system properties. If the configuration key is not found, environ will ultimately return nil.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.3.167