Any real service is likely going to support multiple routes, so let's add an additional route to our service.
- To begin, we'll create a new file, /src/routes/route_agenda.kt.
- Next, we'll define an extension function, Route.agenda(), as we did previously.
- Within route_agenda.kt, we will create const sample JSON val, which will be served from our endpoint, as follows:
// route_agenda.kt
const val AGENDA_JSON_SAMPLE = """
{
"days":[
{
"title":"Day 1",
"date":"6\/4\/20",
"sessions":[
{
"id":0,
"title":"Kotlin 101",
"speaker":"John Smith"
},
{
"id":1,
"title":"Kotlin 201",
"speaker":"Jane Smith"
}
]
},
...
]
}
"""
- Now, we'll use that JSON to build our new route for the /agenda endpoint:
fun Route.agenda() = get("/agenda") {
call.respondText(AGENDA_JSON_SAMPLE.trimIndent(), ContentType.Text.Plain)
}
- Now, once we install route, we can serve that JSON from http://0.0.0.0:8080/agenda.
- To install our route, once again, we will simply add to the routing {} block of Application.module as follows:
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
HttpClient(Jetty) {
routing {
root()
agenda()
}
}
}
- And if we redeploy the service and navigate to http://0.0.0.0:8080/agenda, we should see the following output:
Now, once this is deployed, we can continue to build our service, adding additional routes as needed and serving static text, JSON, or any other response types that we need in order to scale our application, be they a single service or a collection of microservices.