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.
- Example
- Declaration
val devMode = Environment.ENV.developmentMode
Invirt.configure(developmentMode = devMode)
val Environment.developmentMode: Boolean
get() = EnvironmentKey.boolean().defaulted("DEVELOPMENT_MODE", false)(this)
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).
- Example
- Declaration
val env = Environment.ENV.withDotEnv(".env")
val devEnv = Environment.ENV.withDotEnv("config/.env.local")
fun Environment.withDotEnv(dotEnvFile: String): Environment
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.
- Example
- Declaration
val dotEnv = dotenv {
directory = "../../"
ignoreIfMissing = false
systemProperties = true
}
val env = Environment.ENV.withDotEnv(dotEnv)
fun Environment.withDotEnv(dotEnv: Dotenv): Environment
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"
}
- Example
- Declaration
val assetsVersion = gitCommitId()
fun gitCommitId(): String? = EnvironmentKey.optional("git.commit.id")(Environment.fromResource("git.properties"))