|
| 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