Skip to content

Commit

Permalink
chore: migrate to latest ktor
Browse files Browse the repository at this point in the history
  • Loading branch information
d1snin committed Oct 20, 2024
1 parent 4b75a3b commit bbe9483
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 41 deletions.
2 changes: 1 addition & 1 deletion exkt-ktor-server-koin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ kotlin {
val koinVersion: String by project

api("io.ktor:ktor-server:$ktorVersion")
api("io.ktor:ktor-server-host-common:$ktorVersion")
api("io.ktor:ktor-server-netty:$ktorVersion")

api("io.insert-koin:koin-ktor:$koinVersion")
implementation("io.insert-koin:koin-logger-slf4j:$koinVersion")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.d1s.exkt.ktor.server.koin.configuration

import io.ktor.server.engine.*

/**
* [Configurer] supposed to configure application engine like Netty.
* i. e. `connector`, `rootPath`
*/
public interface ApplicationEngineConfigurer<TEngineConfig : BaseApplicationEngine.Configuration> : Configurer<TEngineConfig>
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

package dev.d1s.exkt.ktor.server.koin.configuration

import io.ktor.server.application.*
import io.ktor.server.engine.*

/**
* [Configurer] supposed to configure [ApplicationEngineEnvironment].
* [Configurer] supposed to configure [ApplicationEnvironment].
* i. e. `connectors`, `rootPath`
*/
public interface EnvironmentConfigurer : Configurer<ApplicationEngineEnvironmentBuilder>
public interface EnvironmentConfigurer : Configurer<ApplicationEnvironmentBuilder>
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import io.ktor.server.config.*
import io.ktor.server.engine.*
import io.ktor.server.routing.*
import org.koin.core.component.KoinComponent
import org.koin.core.error.NoBeanDefFoundException
import org.koin.core.error.NoDefinitionFoundException
import org.koin.core.module.Module
import kotlin.getValue
import kotlin.lazy
Expand All @@ -34,20 +34,6 @@ public typealias Configurers = List<Configurer<*>>
/**
* [ServerApplication] class provides quick extensions for configuring Ktor Server at startup.
*
* Example usage:
* ```kotlin
* class MyApplication : ServerApplication() {
*
* override val configurers = listOf(Routing, Security)
*
* override fun launch() {
* val environment = createApplicationEngineEnvironment()
*
* embeddedServer(Netty, environment).start(wait = true)
* }
* }
* ```
*
* @see Configurer
*/
public abstract class ServerApplication : KoinComponent {
Expand All @@ -69,27 +55,23 @@ public abstract class ServerApplication : KoinComponent {

public fun createApplicationEngineEnvironment(
koinModule: Module = this.koinModule,
config: ApplicationConfig = this.config
): ApplicationEngineEnvironment = applicationEngineEnvironment {
config: ApplicationConfig = this.config,
): ApplicationEnvironment = applicationEnvironment {
this.applyEnvironmentConfiguration(koinModule, config)
}

public fun ApplicationEngineEnvironmentBuilder.applyEnvironmentConfiguration(
public fun ApplicationEnvironmentBuilder.applyEnvironmentConfiguration(
koinModule: Module = this@ServerApplication.koinModule,
config: ApplicationConfig = this@ServerApplication.config
config: ApplicationConfig = this@ServerApplication.config,
) {
this.config = config

this.applyEnvironmentConfigurers(koinModule, config)

module {
applyApplicationConfigurersAndRoutes(koinModule, config)
}
}

public fun Application.applyApplicationConfigurersAndRoutes(
koinModule: Module = this@ServerApplication.koinModule,
config: ApplicationConfig = environment.config
config: ApplicationConfig = environment.config,
) {
applicationConfigurers.withEach {
configure(koinModule, config)
Expand All @@ -98,9 +80,9 @@ public abstract class ServerApplication : KoinComponent {
installRoutes()
}

private fun ApplicationEngineEnvironmentBuilder.applyEnvironmentConfigurers(
private fun ApplicationEnvironmentBuilder.applyEnvironmentConfigurers(
koinModule: Module,
config: ApplicationConfig
config: ApplicationConfig,
) {
environmentConfigurers.withEach {
configure(koinModule, config)
Expand All @@ -112,7 +94,7 @@ public abstract class ServerApplication : KoinComponent {
routing {
installRoutes()
}
} catch (_: NoBeanDefFoundException) {
} catch (_: NoDefinitionFoundException) {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@

package dev.d1s.exkt.ktor.server.koin.configuration.builtin

import dev.d1s.exkt.ktor.server.koin.configuration.EnvironmentConfigurer
import dev.d1s.exkt.ktor.server.koin.configuration.ApplicationEngineConfigurer
import io.ktor.server.application.*
import io.ktor.server.config.ApplicationConfig
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import org.koin.core.module.Module

public object Connector : EnvironmentConfigurer {
public object NettyConnector : ApplicationEngineConfigurer<NettyApplicationEngine.Configuration> {

public override fun ApplicationEngineEnvironmentBuilder.configure(module: Module, config: ApplicationConfig) {
public override fun NettyApplicationEngine.Configuration.configure(module: Module, config: ApplicationConfig) {
connector {
port = config.port
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Routes internal constructor() : MutableList<Route> by mutableListOf
}

/**
* Supposed to be used in [Configturers][dev.d1s.exkt.ktor.server.koin.configuration.Configurer] to configure routes.
* Supposed to be used in [Configurers][dev.d1s.exkt.ktor.server.koin.configuration.Configurer] to configure routes.
*/
public fun Module.configureRoutes(builder: Routes.() -> Unit) {
val routes = Routes().apply(builder)
Expand Down
14 changes: 7 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ koverVersion=0.7.5

versionsPluginVersion=0.51.0

ktorVersion=2.3.5
ktorVersion=3.0.0

ktormVersion=3.6.0
ktormVersion=4.1.1

konformVersion=0.4.0
konformVersion=0.7.0

postgresVersion=42.7.1
postgresVersion=42.7.4

mockkVersion=1.13.9
mockkVersion=1.13.13

coroutinesVersion=1.7.3

serializationVersion=1.6.2

koinVersion=3.4.1
koinVersion=4.0.0

kvisionVersion=7.4.0

kwebVersion=1.4.11
kwebVersion=1.4.12

0 comments on commit bbe9483

Please sign in to comment.