Skip to main content

Environment

Environment.developmentMode

Reads a variable DEVELOPMENT_MODE from the receiver Environment which can be set on your local machine when running the application locally to enable hot reload capabilities (browser refresh loads template edits or static asset edits).

Environment.developmentMode defaults to false so in a production environment its absence implicitly enables classpath loading for views or static assets.

val devMode = Environment.ENV.developmentMode
val appHandler = Invirt(InvirtConfig(developmentMode = Environment.ENV.developmentMode)).then(
routes(
"/static/${assetsVersion}" bind staticAssets(devMode)
)
)

Environment.withDotEnv()

Loads environment variables from .env files and returns a new Environment with the combined variables from receiver environment and the .env file. The variables in the receiver Environment override the ones in the .env files.

An optional directory path argument (defaulting to ./) can be passed to specify the location where to look up the .env files.

Invirt uses the dotenv-kotlin library underneath. Please see next section for customising Dotenv loading.

// Environment containing system env vars combined with the ones in ./.env
val env = Environment.ENV.withDotEnv()

// Environment containing system env vars combined with the ones in /home/user/.env
val env = Environment.ENV.withDotEnv("/home/user")

Environment.withDotEnv(Dotenv)

Allows overriding the settings the dotenv-kotlin uses to load .env files. You must add the dotenv-kotlin dependency to your project to use this.

implementation("io.github.cdimascio:dotenv-kotlin:6.4.1")
val dotEnv = dotenv {
directory = "../../"
ignoreIfMissing = false
systemProperties = true
}

val env = Environment.ENV.withDotEnv(dotEnv)

gitCommitId()

Returns the Git commit id, read from a property named git.commit.id in a git.properties file in the classpath. The call fails if git.properties cannot be found and returns null if the file exists but doesn't contain a git.commit.id property.

A git.propreties can be created by your application's build process, or more commonly by using a Gradle plugin.

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