Skip to main content

Environment

Helpers for reading configuration from environment variables and .env files via http4k's Environment.

Environment.developmentMode

Reads a boolean from the DEVELOPMENT_MODE environment variable, defaulting to false. Used by Invirt.configure to switch between hot-reload and classpath loading for templates and static assets.

val devMode = Environment.ENV.developmentMode
Invirt.configure(developmentMode = devMode)

Environment.withDotEnv(dotEnvFile)

Returns a new Environment overlaid with the values from the given .env file path. If the file doesn't exist the receiver is returned unchanged. The receiver's values take precedence over the file (i.e. an explicit env var beats a .env entry of the same name).

val env = Environment.ENV.withDotEnv(".env")
val devEnv = Environment.ENV.withDotEnv("config/.env.local")

Uses dotenv-kotlin underneath. Pass systemProperties = false internally, so loading a .env file does not pollute system properties.

Environment.withDotEnv(Dotenv)

Overload for callers who want to control the underlying Dotenv instance themselves — for example to load from a custom directory or to set systemProperties = true. Requires the dotenv-kotlin dependency.

val dotEnv = dotenv {
directory = "../../"
ignoreIfMissing = false
systemProperties = true
}
val env = Environment.ENV.withDotEnv(dotEnv)

gitCommitId

Returns the Git commit id, read from a git.commit.id property in a classpath git.properties file. Returns null if the file exists but the property is missing. Fails if the file itself cannot be found.

A git.properties file can be generated by your build — commonly with the gradle-git-properties plugin:

plugins {
id("com.gorylenko.gradle-git-properties") version "2.4.2"
}
val assetsVersion = gitCommitId()