File tree 6 files changed +81
-57
lines changed
6 files changed +81
-57
lines changed Original file line number Diff line number Diff line change 1
1
plugins {
2
2
id ' org.jetbrains.kotlin.jvm' version ' 1.2.71'
3
+ id " net.ltgt.apt" version " 0.10"
4
+
3
5
}
4
6
5
7
apply plugin : ' kotlin'
@@ -8,7 +10,10 @@ apply plugin: 'application'
8
10
group ' com.tubelli'
9
11
version ' 1.0-SNAPSHOT'
10
12
11
- ext. ktor_version = ' 0.9.5'
13
+ ext {
14
+ ktor_version = ' 0.9.5'
15
+ koin_version = ' 0.9.3'
16
+ }
12
17
13
18
repositories {
14
19
mavenCentral()
@@ -22,6 +27,12 @@ dependencies {
22
27
compile " io.ktor:ktor-server-netty:$ktor_version "
23
28
compile " io.ktor:ktor-gson:$ktor_version "
24
29
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
+
25
36
compile(" ch.qos.logback:logback-classic:1.2.3" )
26
37
}
27
38
Original file line number Diff line number Diff line change 1
1
import io.ktor.application.Application
2
- import io.ktor.application.call
3
2
import io.ktor.application.install
4
3
import io.ktor.features.ContentNegotiation
5
4
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
9
6
import io.ktor.server.engine.embeddedServer
10
7
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
13
10
14
11
fun main (args : Array <String >) {
12
+ startKoin(listOf (applicationContext))
15
13
embeddedServer(Netty , 8080 , module = Application ::main).start(wait = true )
16
14
}
17
15
18
16
fun Application.main () {
17
+ val itemRepository: ItemRepository by inject()
19
18
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()
28
22
}
23
+ }
29
24
30
- itemRouting(itemRepository)
25
+ install(Routing ) {
26
+ itemsApi(itemRepository)
31
27
}
32
- }
28
+ }
29
+
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -4,39 +4,3 @@ import io.ktor.request.receive
4
4
import io.ktor.response.respond
5
5
import io.ktor.routing.*
6
6
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
- }
Original file line number Diff line number Diff line change
1
+ import org.koin.standalone.KoinComponent
1
2
import java.util.concurrent.CopyOnWriteArrayList
2
3
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 ()
7
8
8
9
fun add (i : Item ) =
9
10
if (! items.contains(i)) {
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments