-
Notifications
You must be signed in to change notification settings - Fork 17
feat(#821): ktor works with any dependency provider, and also with ktor-di with auto detection #937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| dependencies { | ||
| api(projects.lib.stoveTestingE2e) | ||
| implementation(libs.ktor.server.host.common) | ||
| implementation(libs.koin.ktor) | ||
|
|
||
| // Both DI systems as compileOnly - users bring their preferred DI at runtime | ||
| compileOnly(libs.koin.ktor) | ||
| compileOnly(libs.ktor.server.di) | ||
| } |
55 changes: 55 additions & 0 deletions
55
...ve-ktor-testing-e2e/src/main/kotlin/com/trendyol/stove/testing/e2e/DependencyResolvers.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package com.trendyol.stove.testing.e2e | ||
|
|
||
| import io.ktor.server.application.* | ||
| import io.ktor.server.plugins.di.* | ||
| import io.ktor.util.reflect.* | ||
| import org.koin.ktor.ext.getKoin | ||
| import kotlin.reflect.KClass | ||
| import kotlin.reflect.full.starProjectedType | ||
|
|
||
| /** | ||
| * Type alias for a dependency resolver function. | ||
| * Takes an Application and a KClass, returns the resolved dependency. | ||
| */ | ||
| typealias DependencyResolver = (Application, KClass<*>) -> Any | ||
|
|
||
| /** | ||
| * Default resolver implementations for supported DI frameworks. | ||
| */ | ||
| object DependencyResolvers { | ||
| /** | ||
| * Resolver for Koin DI framework. | ||
| */ | ||
| val koin: DependencyResolver = { application, klass -> application.getKoin().get(klass) } | ||
|
|
||
| /** | ||
| * Resolver for Ktor-DI framework. | ||
| */ | ||
| val ktorDi: DependencyResolver = { application, klass -> | ||
| require(application.attributes.contains(DependencyRegistryKey)) { | ||
| "Ktor-DI not installed in application. Make sure to install(DI) { ... } in your application." | ||
| } | ||
| val typeInfo = TypeInfo(klass, klass.starProjectedType) | ||
| application.dependencies.getBlocking(DependencyKey(type = typeInfo)) | ||
| } | ||
|
|
||
| /** | ||
| * Auto-detects and returns the appropriate resolver based on available DI frameworks. | ||
| * Prefers Ktor-DI over Koin if both are available. | ||
| * Detection is deferred to runtime to ensure classpath is fully resolved. | ||
| */ | ||
| fun autoDetect(): DependencyResolver = { application, klass -> | ||
| val resolver = when { | ||
| KtorDiCheck.isKtorDiAvailable() -> ktorDi | ||
|
|
||
| KtorDiCheck.isKoinAvailable() -> koin | ||
|
|
||
| else -> error( | ||
| "No supported DI framework found. " + | ||
| "Add either Koin (io.insert-koin:koin-ktor) or Ktor-DI (io.ktor:ktor-server-di) to your classpath, " + | ||
| "or provide a custom resolver via bridge(resolver = { app, klass -> ... })" | ||
| ) | ||
| } | ||
| resolver(application, klass) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...ktor/stove-ktor-testing-e2e/src/main/kotlin/com/trendyol/stove/testing/e2e/KtorDiCheck.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| @file:Suppress("TooGenericExceptionCaught", "SwallowedException") | ||
|
|
||
| package com.trendyol.stove.testing.e2e | ||
|
|
||
| /** | ||
| * Checks which DI system is available on the classpath. | ||
| */ | ||
| object KtorDiCheck { | ||
| /** | ||
| * Returns true if Koin is available on the classpath. | ||
| */ | ||
| fun isKoinAvailable(): Boolean = try { | ||
| Class.forName("org.koin.ktor.ext.ApplicationExtKt") | ||
| true | ||
| } catch (_: ClassNotFoundException) { | ||
| false | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if Ktor-DI is available on the classpath. | ||
| */ | ||
| fun isKtorDiAvailable(): Boolean = try { | ||
| Class.forName("io.ktor.server.plugins.di.DependencyInjectionConfig") | ||
| true | ||
| } catch (_: ClassNotFoundException) { | ||
| false | ||
| } | ||
| } |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| dependencies { | ||
| api(projects.starters.ktor.stoveKtorTestingE2e) | ||
| implementation(libs.ktor.server.netty) | ||
| implementation(libs.ktor.server.di) | ||
| testImplementation(testFixtures(projects.starters.ktor.tests.ktorTestFixtures)) | ||
| } | ||
|
|
||
| dependencies { | ||
osoykan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| testImplementation(libs.slf4j.simple) | ||
| } | ||
|
|
||
| tasks.test.configure { | ||
| systemProperty("kotest.framework.config.fqn", "com.trendyol.stove.testing.e2e.ktor.KtorDiStove") | ||
| } | ||
|
|
||
23 changes: 23 additions & 0 deletions
23
...ers/ktor/tests/ktor-di-tests/src/test/kotlin/com/trendyol/stove/testing/e2e/ktor/Stove.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.trendyol.stove.testing.e2e.ktor | ||
|
|
||
| import com.trendyol.stove.testing.e2e.bridge | ||
| import com.trendyol.stove.testing.e2e.ktor | ||
| import com.trendyol.stove.testing.e2e.system.TestSystem | ||
| import io.kotest.core.config.AbstractProjectConfig | ||
|
|
||
| class KtorDiStove : AbstractProjectConfig() { | ||
| override suspend fun beforeProject(): Unit = | ||
| TestSystem() | ||
| .with { | ||
| bridge() // Auto-detects Ktor-DI | ||
| ktor( | ||
| runner = { params -> | ||
| KtorDiTestApp.run(params) | ||
| } | ||
| ) | ||
| }.run() | ||
|
|
||
| override suspend fun afterProject(): Unit = TestSystem.stop() | ||
| } | ||
|
|
||
| class KtorDiBridgeSystemTests : BridgeSystemTests(KtorDiStove()) |
31 changes: 31 additions & 0 deletions
31
starters/ktor/tests/ktor-di-tests/src/test/kotlin/com/trendyol/stove/testing/e2e/ktor/app.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.trendyol.stove.testing.e2e.ktor | ||
|
|
||
| import io.ktor.server.application.* | ||
| import io.ktor.server.engine.* | ||
| import io.ktor.server.netty.* | ||
| import io.ktor.server.plugins.di.* | ||
| import org.junit.platform.commons.logging.LoggerFactory | ||
| import java.net.ServerSocket | ||
|
|
||
| /** | ||
| * Test Ktor application using Ktor-DI for dependency injection. | ||
| */ | ||
| object KtorDiTestApp { | ||
| private val logger = LoggerFactory.getLogger(KtorDiTestApp::class.java) | ||
|
|
||
| fun run(args: Array<String>): Application { | ||
| logger.info { "Starting Ktor-DI test application with args: ${args.joinToString(" ")}" } | ||
| val port = findAvailablePort() | ||
| val applicationEngine = embeddedServer(Netty, port = port, host = "localhost") { | ||
| dependencies { | ||
| provide<GetUtcNow> { SystemTimeGetUtcNow() } | ||
| provide<ExampleService> { ExampleService(resolve()) } | ||
| provide<TestConfig> { TestConfig() } | ||
| } | ||
| } | ||
| applicationEngine.start(wait = false) | ||
| return applicationEngine.application | ||
| } | ||
|
|
||
| private fun findAvailablePort(): Int = ServerSocket(0).use { it.localPort } | ||
| } |
5 changes: 5 additions & 0 deletions
5
starters/ktor/tests/ktor-di-tests/src/test/resources/simplelogger.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| org.slf4j.simpleLogger.defaultLogLevel=info | ||
| org.slf4j.simpleLogger.showDateTime=true | ||
| org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss.SSS | ||
| org.slf4j.simpleLogger.showShortLogName=true | ||
|
|
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| dependencies { | ||
| api(projects.starters.ktor.stoveKtorTestingE2e) | ||
| implementation(libs.ktor.server.netty) | ||
| implementation(libs.koin.ktor) | ||
| testImplementation(testFixtures(projects.starters.ktor.tests.ktorTestFixtures)) | ||
| } | ||
|
|
||
| dependencies { | ||
osoykan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| testImplementation(libs.slf4j.simple) | ||
| } | ||
|
|
||
| tasks.test.configure { | ||
| systemProperty("kotest.framework.config.fqn", "com.trendyol.stove.testing.e2e.ktor.KoinStove") | ||
| } | ||
|
|
||
23 changes: 23 additions & 0 deletions
23
...s/ktor/tests/ktor-koin-tests/src/test/kotlin/com/trendyol/stove/testing/e2e/ktor/Stove.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.trendyol.stove.testing.e2e.ktor | ||
|
|
||
| import com.trendyol.stove.testing.e2e.bridge | ||
| import com.trendyol.stove.testing.e2e.ktor | ||
| import com.trendyol.stove.testing.e2e.system.TestSystem | ||
| import io.kotest.core.config.AbstractProjectConfig | ||
|
|
||
| class KoinStove : AbstractProjectConfig() { | ||
| override suspend fun beforeProject(): Unit = | ||
| TestSystem() | ||
| .with { | ||
| bridge() // Auto-detects Koin | ||
| ktor( | ||
| runner = { params -> | ||
| KoinTestApp.run(params) | ||
| } | ||
| ) | ||
| }.run() | ||
|
|
||
| override suspend fun afterProject(): Unit = TestSystem.stop() | ||
| } | ||
|
|
||
| class KoinBridgeSystemTests : BridgeSystemTests(KoinStove()) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.