Skip to content

Commit 823872d

Browse files
chore(rest-api): add simple rest api controller
1 parent 3d0b78f commit 823872d

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

Diff for: src/main/kotlin/App.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
* https://opensource.org/licenses/MIT.
77
*/
88

9+
import infrastructure.api.APIController
10+
911
/**
1012
* Template for kotlin projects.
1113
*/
1214
fun main() {
13-
println("Hello World from Kotlin Template")
15+
APIController().start()
1416
}

Diff for: src/main/kotlin/infrastructure/api/APIController.kt

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2023. Smart Operating Block
3+
*
4+
* Use of this source code is governed by an MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
package infrastructure.api
10+
11+
import io.ktor.server.application.Application
12+
import io.ktor.server.application.call
13+
import io.ktor.server.engine.embeddedServer
14+
import io.ktor.server.netty.Netty
15+
import io.ktor.server.response.respondText
16+
import io.ktor.server.routing.get
17+
import io.ktor.server.routing.routing
18+
19+
/**
20+
* It manages the REST-API of the microservice.
21+
*/
22+
class APIController {
23+
/**
24+
* Starts the http server to serve the client requests.
25+
*/
26+
fun start() {
27+
embeddedServer(Netty, port = 3000, module = this::dispatcher).start(wait = true)
28+
}
29+
30+
/**
31+
* Dispatcher of the http routing.
32+
* Needs to be public due to reflection used by ktor.
33+
*/
34+
fun dispatcher(app: Application) {
35+
with(app) {
36+
roomAPI(this)
37+
medicalTechnologyPI(this)
38+
}
39+
}
40+
41+
/**
42+
* The Room API available to handle room requests.
43+
* @param[app] it represents the running ktor web application
44+
*/
45+
private fun roomAPI(app: Application) {
46+
with(app) {
47+
routing {
48+
get("/room") {
49+
call.respondText("[${Thread.currentThread().name}] Room!")
50+
}
51+
}
52+
}
53+
}
54+
55+
/**
56+
* The Medical Technology API available to handle medical technology requests.
57+
* @param[app] it represents the running ktor web application
58+
*/
59+
private fun medicalTechnologyPI(app: Application) {
60+
with(app) {
61+
routing {
62+
get("/medicaltechnology") {
63+
call.respondText("[${Thread.currentThread().name}] Medical Technology!")
64+
}
65+
}
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)