Skip to content

Commit a86e3b6

Browse files
committed
Added koin for dipendency injection
1 parent 12db123 commit a86e3b6

File tree

6 files changed

+81
-57
lines changed

6 files changed

+81
-57
lines changed

build.gradle

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
plugins {
22
id 'org.jetbrains.kotlin.jvm' version '1.2.71'
3+
id "net.ltgt.apt" version "0.10"
4+
35
}
46

57
apply plugin: 'kotlin'
@@ -8,7 +10,10 @@ apply plugin: 'application'
810
group 'com.tubelli'
911
version '1.0-SNAPSHOT'
1012

11-
ext.ktor_version = '0.9.5'
13+
ext {
14+
ktor_version = '0.9.5'
15+
koin_version = '0.9.3'
16+
}
1217

1318
repositories {
1419
mavenCentral()
@@ -22,6 +27,12 @@ dependencies {
2227
compile "io.ktor:ktor-server-netty:$ktor_version"
2328
compile "io.ktor:ktor-gson:$ktor_version"
2429

30+
compile "com.google.dagger:dagger:2.17"
31+
annotationProcessor 'com.google.dagger:dagger-compiler:2.17'
32+
33+
compile "org.koin:koin-ktor:$koin_version"
34+
compile "org.koin:koin-core:$koin_version"
35+
2536
compile("ch.qos.logback:logback-classic:1.2.3")
2637
}
2738

src/main/kotlin/App.kt

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
import io.ktor.application.Application
2-
import io.ktor.application.call
32
import io.ktor.application.install
43
import io.ktor.features.ContentNegotiation
54
import io.ktor.gson.gson
6-
import io.ktor.response.respondText
7-
import io.ktor.routing.get
8-
import io.ktor.routing.routing
5+
import io.ktor.routing.Routing
96
import io.ktor.server.engine.embeddedServer
107
import io.ktor.server.netty.Netty
11-
12-
private val itemRepository = ItemRepository()
8+
import org.koin.ktor.ext.inject
9+
import org.koin.standalone.StandAloneContext.startKoin
1310

1411
fun main(args: Array<String>) {
12+
startKoin(listOf(applicationContext))
1513
embeddedServer(Netty, 8080, module = Application::main).start(wait = true)
1614
}
1715

1816
fun Application.main() {
17+
val itemRepository: ItemRepository by inject()
1918

20-
routing {
21-
install(ContentNegotiation) {
22-
gson {
23-
setPrettyPrinting()
24-
}
25-
}
26-
get("/") {
27-
call.respondText { "Hello from my first ktor service :)" }
19+
install(ContentNegotiation) {
20+
gson {
21+
setPrettyPrinting()
2822
}
23+
}
2924

30-
itemRouting(itemRepository)
25+
install(Routing) {
26+
itemsApi(itemRepository)
3127
}
32-
}
28+
}
29+

src/main/kotlin/ApplicationContext.kt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import java.util.concurrent.CopyOnWriteArrayList
2+
import java.util.concurrent.atomic.AtomicInteger
3+
4+
val applicationContext = org.koin.dsl.module.applicationContext {
5+
bean { ItemRepository() }
6+
bean { AtomicInteger() }
7+
bean { CopyOnWriteArrayList<Item>() }
8+
}

src/main/kotlin/DefaultController.kt

-36
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,3 @@ import io.ktor.request.receive
44
import io.ktor.response.respond
55
import io.ktor.routing.*
66

7-
fun Route.itemRouting(itemRepository: ItemRepository) {
8-
get("/items") {
9-
call.respond(itemRepository.getAll())
10-
}
11-
12-
get("/itemRouting/{id}") {
13-
call.parameters["id"]?.let { id ->
14-
itemRepository.get(id.toInt())?.let { item ->
15-
call.respond(item)
16-
}
17-
}
18-
call.respond(HttpStatusCode.NotFound)
19-
}
20-
21-
delete("/itemRouting/{id}") {
22-
call.parameters["id"]?.let { id ->
23-
itemRepository.remove(id.toInt())
24-
}
25-
26-
call.respond(HttpStatusCode.Accepted)
27-
}
28-
29-
put("/itemRouting") {
30-
val item = call.receive<Item>()
31-
itemRepository.add(item)
32-
33-
call.respond(HttpStatusCode.Created)
34-
}
35-
36-
post("/itemRouting") {
37-
val item = call.receive<Item>()
38-
itemRepository.update(item)
39-
40-
call.respond(HttpStatusCode.OK)
41-
}
42-
}

src/main/kotlin/ItemRepository.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import org.koin.standalone.KoinComponent
12
import java.util.concurrent.CopyOnWriteArrayList
23
import java.util.concurrent.atomic.AtomicInteger
3-
4-
class ItemRepository {
5-
private val idCounter = AtomicInteger()
6-
private val items = CopyOnWriteArrayList<Item>()
4+
import org.koin.standalone.inject
5+
class ItemRepository : KoinComponent {
6+
private val idCounter: AtomicInteger by inject()
7+
private val items: CopyOnWriteArrayList<Item> by inject()
78

89
fun add(i: Item) =
910
if (!items.contains(i)) {

src/main/kotlin/itemsApi.kt

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import io.ktor.application.call
2+
import io.ktor.http.HttpStatusCode
3+
import io.ktor.request.receive
4+
import io.ktor.response.respond
5+
import io.ktor.response.respondText
6+
import io.ktor.routing.*
7+
8+
fun Routing.itemsApi(repository: ItemRepository) {
9+
get("/") {
10+
call.respondText { "Hello from my first ktor service :)" }
11+
}
12+
13+
get("/items") {
14+
call.respond(repository.getAll())
15+
}
16+
get("/itemRouting/{id}") {
17+
call.parameters["id"]?.let { id ->
18+
repository.get(id.toInt())?.let { item ->
19+
call.respond(item)
20+
}
21+
}
22+
call.respond(HttpStatusCode.NotFound)
23+
}
24+
delete("/itemRouting/{id}") {
25+
call.parameters["id"]?.let { id ->
26+
repository.remove(id.toInt())
27+
}
28+
29+
call.respond(HttpStatusCode.Accepted)
30+
}
31+
put("/itemRouting") {
32+
val item = call.receive<Item>()
33+
repository.add(item)
34+
35+
call.respond(HttpStatusCode.Created)
36+
}
37+
post("/itemRouting") {
38+
val item = call.receive<Item>()
39+
repository.update(item)
40+
41+
call.respond(HttpStatusCode.OK)
42+
}
43+
}

0 commit comments

Comments
 (0)