Skip to content

Commit

Permalink
refactor: moves api, config, engine and S3 config resolver modules un…
Browse files Browse the repository at this point in the history
…der core.
  • Loading branch information
outofcoffee committed Dec 1, 2021
1 parent d5ff3ec commit 253560f
Show file tree
Hide file tree
Showing 169 changed files with 1,103 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import io.gatehill.imposter.awslambda.util.ImposterBuilderKt
import io.gatehill.imposter.awslambda.util.LambdaPlugin
import io.gatehill.imposter.embedded.MockEngine
import io.gatehill.imposter.plugin.openapi.OpenApiPluginImpl
import io.gatehill.imposter.plugin.openapi.loader.S3FileDownloader
import io.gatehill.imposter.plugin.rest.RestPluginImpl
import io.gatehill.imposter.server.RequestHandlingMode
import io.gatehill.imposter.util.InjectorUtil
Expand All @@ -75,14 +74,11 @@ class Handler : RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyRespo
System.setProperty("vertx.cacheDirBase", "/tmp/.vertx")
System.setProperty("java.io.tmpdir", "/tmp")

logger.debug("Retrieving configuration from ${Settings.s3ConfigUrl}")
S3FileDownloader.getInstance().downloadAllFiles(Settings.s3ConfigUrl, Settings.configDir)

engine = ImposterBuilderKt()
.withPluginClass(LambdaPlugin::class.java)
.withPluginClass(OpenApiPluginImpl::class.java)
.withPluginClass(RestPluginImpl::class.java)
.withConfigurationDir(Settings.configDir.path)
.withConfigurationDir(Settings.configDir ?: Settings.s3ConfigUrl)
.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,15 +43,14 @@

package io.gatehill.imposter.awslambda.config

import io.gatehill.imposter.util.EnvVars
import java.io.File
import io.gatehill.imposter.config.util.EnvVars

/**
* @author Pete Cornish
*/
object Settings {
val configDir: File by lazy {
File(EnvVars.getEnv("IMPOSTER_CONFIG_DIR") ?: "/tmp/imposter-config")
val configDir: String? by lazy {
EnvVars.getEnv("IMPOSTER_CONFIG_DIR")
}
val s3ConfigUrl: String by lazy {
EnvVars.getEnv("IMPOSTER_S3_CONFIG_URL") ?: throw IllegalStateException("Missing S3 configuration URL")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@

package io.gatehill.imposter.awslambda.util

import io.gatehill.imposter.http.HttpRouter
import io.gatehill.imposter.plugin.Plugin
import io.gatehill.imposter.plugin.RequireModules

Expand All @@ -52,6 +51,4 @@ import io.gatehill.imposter.plugin.RequireModules
* @author Pete Cornish
*/
@RequireModules(LambdaModule::class)
class LambdaPlugin : Plugin {
override fun configureRoutes(router: HttpRouter) {}
}
class LambdaPlugin : Plugin
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,16 @@ import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
import com.amazonaws.services.lambda.runtime.tests.annotations.Event
import com.amazonaws.services.s3.AmazonS3ClientBuilder
import io.gatehill.imposter.awslambda.Handler
import io.gatehill.imposter.plugin.openapi.loader.S3FileDownloader
import io.gatehill.imposter.util.EnvVars
import io.gatehill.imposter.config.S3FileDownloader
import io.gatehill.imposter.config.util.EnvVars
import io.gatehill.imposter.util.TestEnvironmentUtil.assumeDockerAccessible
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.params.ParameterizedTest
import org.mockito.Mockito.mock
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.io.path.absolutePathString

class HandlerTest {
private var s3Mock: S3MockContainer? = null
Expand All @@ -83,9 +81,7 @@ class HandlerTest {
uploadFileToS3("/config", "pet-api.yaml")
uploadFileToS3("/config", "subdir/response.json")

val configDir = Files.createTempDirectory("imposter-config")
EnvVars.populate(mapOf(
"IMPOSTER_CONFIG_DIR" to configDir.absolutePathString(),
"IMPOSTER_S3_CONFIG_URL" to "s3://test/",
))

Expand Down
2 changes: 1 addition & 1 deletion adapter/vertxweb/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ compileJava {
}

dependencies {
implementation project(':imposter-core')
implementation project(':core:imposter-engine')
api "io.vertx:vertx-web:$version_vertx"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@
*/
package io.gatehill.imposter.cmd

import io.gatehill.imposter.config.util.EnvVars.Companion.getEnv
import io.gatehill.imposter.config.util.MetaUtil.readVersion
import io.gatehill.imposter.plugin.internal.MetaInfPluginDetectorImpl
import io.gatehill.imposter.server.ConfigHolder
import io.gatehill.imposter.server.vertxweb.VertxWebServerFactoryImpl
import io.gatehill.imposter.util.CryptoUtil.DEFAULT_KEYSTORE_PASSWORD
import io.gatehill.imposter.util.CryptoUtil.DEFAULT_KEYSTORE_PATH
import io.gatehill.imposter.util.EnvVars.Companion.getEnv
import io.gatehill.imposter.util.FileUtil.CLASSPATH_PREFIX
import io.gatehill.imposter.util.HttpUtil.BIND_ALL_HOSTS
import io.gatehill.imposter.util.HttpUtil.DEFAULT_HTTPS_LISTEN_PORT
import io.gatehill.imposter.util.HttpUtil.DEFAULT_HTTP_LISTEN_PORT
import io.gatehill.imposter.util.LogUtil
import io.gatehill.imposter.util.MetaUtil.readVersion
import io.vertx.core.logging.SLF4JLogDelegateFactory
import org.apache.logging.log4j.LogManager
import org.kohsuke.args4j.CmdLineException
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ package io.gatehill.imposter
import io.gatehill.imposter.server.RequestHandlingMode

/**
* Mock engine settings.
*
* @author Pete Cornish
*/
class ImposterConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@
*/
package io.gatehill.imposter.plugin

import io.gatehill.imposter.http.HttpRouter

/**
* @author Pete Cornish
*/
interface Plugin {
fun configureRoutes(router: HttpRouter)
}
interface Plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2016-2021.
*
* This file is part of Imposter.
*
* "Commons Clause" License Condition v1.0
*
* The Software is provided to you by the Licensor under the License, as
* defined below, subject to the following condition.
*
* Without limiting other conditions in the License, the grant of rights
* under the License will not include, and the License does not grant to
* you, the right to Sell the Software.
*
* For purposes of the foregoing, "Sell" means practicing any or all of
* the rights granted to you under the License to provide to third parties,
* for a fee or other consideration (including without limitation fees for
* hosting or consulting/support services related to the Software), a
* product or service whose value derives, entirely or substantially, from
* the functionality of the Software. Any license notice or attribution
* required by the License must also include this Commons Clause License
* Condition notice.
*
* Software: Imposter
*
* License: GNU Lesser General Public License version 3
*
* Licensor: Peter Cornish
*
* Imposter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Imposter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Imposter. If not, see <https://www.gnu.org/licenses/>.
*/
package io.gatehill.imposter.plugin

import com.google.inject.Injector
import io.gatehill.imposter.ImposterConfig
import java.io.File

/**
* @author Pete Cornish
*/
interface PluginManager {
fun preparePluginsFromConfig(
imposterConfig: ImposterConfig,
plugins: List<String>,
pluginConfigs: Map<String, MutableList<File>>
): List<PluginDependencies>

fun determinePluginClass(plugin: String): String
fun registerPlugins(injector: Injector)
fun configurePlugins(pluginConfigs: Map<String, MutableList<File>>)

fun isProviderRegistered(provider: Class<out PluginProvider>): Boolean
fun registerProvider(provider: Class<out PluginProvider>)
fun <P : Plugin?> getPlugin(pluginClassName: String): P?
fun getPlugins(): Collection<Plugin>
fun registerInstance(instance: Plugin)
fun getPluginClasses(): Collection<Class<out Plugin>>
fun registerClass(plugin: Class<out Plugin>): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2016-2021.
*
* This file is part of Imposter.
*
* "Commons Clause" License Condition v1.0
*
* The Software is provided to you by the Licensor under the License, as
* defined below, subject to the following condition.
*
* Without limiting other conditions in the License, the grant of rights
* under the License will not include, and the License does not grant to
* you, the right to Sell the Software.
*
* For purposes of the foregoing, "Sell" means practicing any or all of
* the rights granted to you under the License to provide to third parties,
* for a fee or other consideration (including without limitation fees for
* hosting or consulting/support services related to the Software), a
* product or service whose value derives, entirely or substantially, from
* the functionality of the Software. Any license notice or attribution
* required by the License must also include this Commons Clause License
* Condition notice.
*
* Software: Imposter
*
* License: GNU Lesser General Public License version 3
*
* Licensor: Peter Cornish
*
* Imposter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Imposter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Imposter. If not, see <https://www.gnu.org/licenses/>.
*/
package io.gatehill.imposter.plugin

import io.gatehill.imposter.http.HttpRouter

/**
* @author Pete Cornish
*/
interface RoutablePlugin : Plugin {
fun configureRoutes(router: HttpRouter)
}
16 changes: 6 additions & 10 deletions core/build.gradle → core/config/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,22 @@ compileJava {
}

dependencies {
api project(':imposter-api')

implementation "org.apache.logging.log4j:log4j-core:$version_log4j"
implementation project(':core:imposter-api')

api "com.fasterxml.jackson.core:jackson-databind:$version_jackson"
implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:$version_jackson"
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$version_jackson"
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:$version_jackson"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

implementation "io.github.classgraph:classgraph:$version_classgraph"

implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

api "commons-io:commons-io:$version_commons_io"
api "org.apache.commons:commons-text:$version_commons_text"
api "com.jayway.jsonpath:json-path:$version_json_path"

// metrics
api "io.vertx:vertx-micrometer-metrics:$version_vertx"
api "io.micrometer:micrometer-registry-prometheus:$version_micrometer"

testImplementation "junit:junit:$version_junit"
// test
testImplementation "junit:junit:$version_junit"
testImplementation group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2016-2021.
*
* This file is part of Imposter.
*
* "Commons Clause" License Condition v1.0
*
* The Software is provided to you by the Licensor under the License, as
* defined below, subject to the following condition.
*
* Without limiting other conditions in the License, the grant of rights
* under the License will not include, and the License does not grant to
* you, the right to Sell the Software.
*
* For purposes of the foregoing, "Sell" means practicing any or all of
* the rights granted to you under the License to provide to third parties,
* for a fee or other consideration (including without limitation fees for
* hosting or consulting/support services related to the Software), a
* product or service whose value derives, entirely or substantially, from
* the functionality of the Software. Any license notice or attribution
* required by the License must also include this Commons Clause License
* Condition notice.
*
* Software: Imposter
*
* License: GNU Lesser General Public License version 3
*
* Licensor: Peter Cornish
*
* Imposter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Imposter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Imposter. If not, see <https://www.gnu.org/licenses/>.
*/
package io.gatehill.imposter.config.resolver

import java.io.File

interface ConfigResolver {
fun handles(configPath: String): Boolean
fun resolve(configPath: String): File
}
Loading

0 comments on commit 253560f

Please sign in to comment.