Add kodein di for injections and sample controller
This commit is contained in:
parent
f3c922e417
commit
cc0058892e
@ -79,9 +79,9 @@
|
|||||||
<version>${ktor_version}</version>
|
<version>${ktor_version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.ktor</groupId>
|
<groupId>org.kodein.di</groupId>
|
||||||
<artifactId>ktor-client-core-jvm</artifactId>
|
<artifactId>kodein-di-generic-jvm</artifactId>
|
||||||
<version>${ktor_version}</version>
|
<version>6.5.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
|||||||
@ -4,6 +4,6 @@ ktor {
|
|||||||
port = ${?PORT}
|
port = ${?PORT}
|
||||||
}
|
}
|
||||||
application {
|
application {
|
||||||
modules = [ be.vandewalleh.NotesApiApplicationKt.module ]
|
modules = [ be.vandewalleh.NotesApplicationKt.module ]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package be.vandewalleh
|
package be.vandewalleh
|
||||||
|
|
||||||
|
import be.vandewalleh.controllers.KodeinController
|
||||||
|
import be.vandewalleh.controllers.controllerModule
|
||||||
import com.fasterxml.jackson.databind.SerializationFeature
|
import com.fasterxml.jackson.databind.SerializationFeature
|
||||||
import io.ktor.application.Application
|
import io.ktor.application.Application
|
||||||
import io.ktor.application.install
|
import io.ktor.application.install
|
||||||
@ -7,6 +9,11 @@ import io.ktor.features.CORS
|
|||||||
import io.ktor.features.ContentNegotiation
|
import io.ktor.features.ContentNegotiation
|
||||||
import io.ktor.jackson.jackson
|
import io.ktor.jackson.jackson
|
||||||
import io.ktor.locations.Locations
|
import io.ktor.locations.Locations
|
||||||
|
import io.ktor.routing.routing
|
||||||
|
import org.kodein.di.Kodein
|
||||||
|
import org.kodein.di.generic.instance
|
||||||
|
|
||||||
|
lateinit var kodein: Kodein
|
||||||
|
|
||||||
@Suppress("unused") // Referenced in application.conf
|
@Suppress("unused") // Referenced in application.conf
|
||||||
fun Application.module() {
|
fun Application.module() {
|
||||||
@ -21,4 +28,17 @@ fun Application.module() {
|
|||||||
enable(SerializationFeature.INDENT_OUTPUT)
|
enable(SerializationFeature.INDENT_OUTPUT)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
kodein = Kodein {
|
||||||
|
import(controllerModule)
|
||||||
|
}
|
||||||
|
|
||||||
|
val controllers: Set<KodeinController> by kodein.instance()
|
||||||
|
|
||||||
|
routing {
|
||||||
|
controllers.forEach {
|
||||||
|
it.apply { registerRoutes() }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
16
api/src/controllers/Controllers.kt
Normal file
16
api/src/controllers/Controllers.kt
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package be.vandewalleh.controllers
|
||||||
|
|
||||||
|
import org.kodein.di.Kodein
|
||||||
|
import org.kodein.di.generic.bind
|
||||||
|
import org.kodein.di.generic.inSet
|
||||||
|
import org.kodein.di.generic.setBinding
|
||||||
|
import org.kodein.di.generic.singleton
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [Kodein] controller module containing the app controllers
|
||||||
|
*/
|
||||||
|
val controllerModule = Kodein.Module(name = "Controller") {
|
||||||
|
bind() from setBinding<KodeinController>()
|
||||||
|
|
||||||
|
bind<KodeinController>().inSet() with singleton { TestController(this.kodein) }
|
||||||
|
}
|
||||||
27
api/src/controllers/KodeinController.kt
Normal file
27
api/src/controllers/KodeinController.kt
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package be.vandewalleh.controllers
|
||||||
|
|
||||||
|
import io.ktor.application.Application
|
||||||
|
import io.ktor.locations.locations
|
||||||
|
import io.ktor.routing.Routing
|
||||||
|
import org.kodein.di.Kodein
|
||||||
|
import org.kodein.di.KodeinAware
|
||||||
|
import org.kodein.di.generic.instance
|
||||||
|
|
||||||
|
abstract class KodeinController(override val kodein: Kodein) : KodeinAware {
|
||||||
|
/**
|
||||||
|
* Injected dependency with the current [Application].
|
||||||
|
*/
|
||||||
|
val application: Application by instance()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shortcut to get the url of a [TypedRoute].
|
||||||
|
*/
|
||||||
|
val TypedRoute.href get() = application.locations.href(this)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method that subtypes must override to register the handled [Routing] routes.
|
||||||
|
*/
|
||||||
|
abstract fun Routing.registerRoutes()
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TypedRoute
|
||||||
22
api/src/controllers/TestController.kt
Normal file
22
api/src/controllers/TestController.kt
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package be.vandewalleh.controllers
|
||||||
|
|
||||||
|
import io.ktor.application.call
|
||||||
|
import io.ktor.locations.Location
|
||||||
|
import io.ktor.locations.get
|
||||||
|
import io.ktor.response.respond
|
||||||
|
import io.ktor.routing.Routing
|
||||||
|
import org.kodein.di.Kodein
|
||||||
|
|
||||||
|
class TestController(kodein: Kodein) : KodeinController(kodein) {
|
||||||
|
override fun Routing.registerRoutes() {
|
||||||
|
get<Routes.Hello>{
|
||||||
|
data class Response(val msg: String)
|
||||||
|
call.respond(Response("Hello"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
object Routes {
|
||||||
|
@Location("/hello")
|
||||||
|
class Hello
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user