Skip to content

Commit

Permalink
feat: supports IMPOSTER_CONFIG_DIR environment variable in all distri…
Browse files Browse the repository at this point in the history
…butions.
  • Loading branch information
outofcoffee committed Sep 23, 2023
1 parent afbadad commit d3e3974
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ abstract class AbstractHandler<Request, Response>(
protected val logger: Logger = LogManager.getLogger(AbstractHandler::class.java)
protected val server: LambdaServer<Request, Response>

private val defaultLambdaBundleConfigDir = "/var/task/config"

init {
// lambda functions are only allowed write access to /tmp
System.setProperty("vertx.cacheDirBase", "/tmp/.vertx")
Expand All @@ -80,14 +78,15 @@ abstract class AbstractHandler<Request, Response>(

LambdaServerFactory.eventType = eventType

@Suppress("DEPRECATION")
val configDir = Settings.configDir ?: Settings.s3ConfigUrl ?: defaultLambdaBundleConfigDir

ImposterBuilderKt()
.withPluginClass(OpenApiPluginImpl::class.java)
.withPluginClass(RestPluginImpl::class.java)
.apply { if (Settings.metaInfScan) withPluginClass(MetaInfPluginDetectorImpl::class.java) }
.withConfigurationDir(configDir)
.apply {
if (Settings.metaInfScan) {
withPluginClass(MetaInfPluginDetectorImpl::class.java)
}
Settings.configDirs.forEach { withConfigurationDir(it) }
}
.withEngineOptions { options ->
options.serverFactory = LambdaServerFactory::class.qualifiedName
options.requestHandlingMode = RequestHandlingMode.SYNC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,34 @@

package io.gatehill.imposter.awslambda.config

import io.gatehill.imposter.config.util.ConfigUtil
import io.gatehill.imposter.config.util.EnvVars
import io.gatehill.imposter.util.splitOnCommaAndTrim

/**
* @author Pete Cornish
*/
object Settings {
val configDir: String? get() = EnvVars.getEnv("IMPOSTER_CONFIG_DIR")
private const val DEFAULT_BUNDLE_CONFIG_DIR = "/var/task/config"

val configDirs: Array<String>
get() {
ConfigUtil.parseConfigDirEnvVar().takeIf { it.isNotEmpty() }?.let {
return it
}
// deprecated environment variable
EnvVars.getEnv("IMPOSTER_S3_CONFIG_URL")?.let {
return arrayOf(it)
}
return arrayOf(DEFAULT_BUNDLE_CONFIG_DIR)
}

/**
* FQCN of [io.gatehill.imposter.plugin.PluginDiscoveryStrategy] implementation.
*/
val pluginDiscoveryStrategyClass: String? get() =
EnvVars.getEnv("IMPOSTER_PLUGIN_DISCOVERY_STRATEGY")

@Deprecated("Use configDir instead", ReplaceWith("Settings.configDir", "io.gatehill.imposter.awslambda.config.Settings"))
val s3ConfigUrl: String? get() =
EnvVars.getEnv("IMPOSTER_S3_CONFIG_URL")

val metaInfScan: Boolean get() =
EnvVars.getEnv("IMPOSTER_METAINF_SCAN")?.toBoolean() ?: false

Expand Down
11 changes: 9 additions & 2 deletions cmd/src/main/java/io/gatehill/imposter/cmd/ImposterLauncher.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2021.
* Copyright (c) 2016-2023.
*
* This file is part of Imposter.
*
Expand Down Expand Up @@ -42,6 +42,7 @@
*/
package io.gatehill.imposter.cmd

import io.gatehill.imposter.config.util.ConfigUtil
import io.gatehill.imposter.config.util.MetaUtil.readVersion
import io.gatehill.imposter.plugin.DynamicPluginDiscoveryStrategyImpl
import io.gatehill.imposter.plugin.internal.MetaInfPluginDetectorImpl
Expand Down Expand Up @@ -74,7 +75,11 @@ class ImposterLauncher(args: Array<String>) {
name = "--configDir",
aliases = ["-c"],
usage = "Directory containing mock configuration files",
required = true

/**
* If not provided, [ConfigUtil.parseConfigDirEnvVar] will be used.
*/
required = false
)
private var configDirs = arrayOf<String>()

Expand Down Expand Up @@ -180,6 +185,8 @@ class ImposterLauncher(args: Array<String>) {
if (tlsEnabled) DEFAULT_HTTPS_LISTEN_PORT else DEFAULT_HTTP_LISTEN_PORT
}

configDirs += ConfigUtil.parseConfigDirEnvVar()

if (configDirs.isEmpty()) {
LOGGER.error("No configuration directories were provided")
exitProcess(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import io.gatehill.imposter.plugin.config.ResourcesHolder
import io.gatehill.imposter.plugin.config.resource.BasePathHolder
import io.gatehill.imposter.util.MapUtil
import io.gatehill.imposter.util.ResourceUtil
import io.gatehill.imposter.util.splitOnCommaAndTrim
import org.apache.logging.log4j.LogManager
import java.io.File
import java.io.IOException
Expand Down Expand Up @@ -127,6 +128,12 @@ object ConfigUtil {
}.toSet()
}

/**
* Parses the `IMPOSTER_CONFIG_DIR` environment variable for a list of configuration directories.
*/
fun parseConfigDirEnvVar(): Array<String> =
EnvVars.getEnv("IMPOSTER_CONFIG_DIR")?.splitOnCommaAndTrim()?.toTypedArray() ?: emptyArray()

fun initInterpolators(environment: Map<String, String>) {
// reset the environment used by the expression evaluator
expressionEvaluators = mapOf("env" to EnvEvaluator(environment))
Expand Down
1 change: 1 addition & 0 deletions docs/environment_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The following environment variables are supported:
| IMPOSTER_ADD_ENGINE_RESPONSE_HEADERS | Whether to add response headers for `server` and unique request ID. | `true` | `false` |
| IMPOSTER_AUTO_BASE_PATH | Whether to automatically set the base path for each configuration file, based on its relative path from the configuration root directory. | `false` | `true` |
| IMPOSTER_CACHE_DIR | Path to a directory in which to store cached data, such as remote specifications. | `<Java temporary directory>/imposter-cache` | `/path/to/dir` |
| IMPOSTER_CONFIG_DIR | The path to the configuration directory. Can be specified as a comma-separated list. See [configuration location](config_location.md). | `false` | `true` |
| IMPOSTER_CONFIG_SCAN_RECURSIVE | Whether to scan for configuration files recursively within the configuration directories. | `false` | `true` |
| IMPOSTER_CONFIG_DISCOVER_ENVFILES | Whether to discover envfiles. See below. | `true` | `false` |
| IMPOSTER_FEATURES | Enables or disables features. See [Features](./features.md) documentation. | Per [default features](./features.md). | `metrics=false,stores=true` |
Expand Down

0 comments on commit d3e3974

Please sign in to comment.