[BAEL-1753] Cleanup code

This commit is contained in:
Syed Mansoor 2018-06-18 23:31:04 +10:00
parent 86fe6cfb56
commit 423ccc6a83
5 changed files with 14 additions and 11 deletions

View File

@ -7,6 +7,7 @@
#ignore build and generated files #ignore build and generated files
build/ build/
node/ node/
out/
#ignore installed node modules and package lock file #ignore installed node modules and package lock file
node_modules/ node_modules/

View File

@ -37,7 +37,7 @@ repositories {
dependencies { dependencies {
compile "io.ktor:ktor-server-netty:$ktor_version" compile "io.ktor:ktor-server-netty:$ktor_version"
compile "ch.qos.logback:logback-classic:1.2.1" compile "ch.qos.logback:logback-classic:1.2.1"
compile "com.google.code.gson:gson:2.8.1" compile "io.ktor:ktor-gson:$ktor_version"
testCompile group: 'junit', name: 'junit', version: '4.12' testCompile group: 'junit', name: 'junit', version: '4.12'
} }

View File

@ -1,5 +0,0 @@
ktor {
application {
modules = [ io.ktor.samples.HelloKt.main ]
}
}

View File

@ -1,12 +1,16 @@
@file:JvmName("APIServer") @file:JvmName("APIServer")
import com.google.gson.Gson
import io.ktor.application.call import io.ktor.application.call
import io.ktor.application.install import io.ktor.application.install
import io.ktor.features.CallLogging import io.ktor.features.CallLogging
import io.ktor.features.ContentNegotiation
import io.ktor.features.DefaultHeaders import io.ktor.features.DefaultHeaders
import io.ktor.gson.gson
import io.ktor.http.ContentType import io.ktor.http.ContentType
import io.ktor.request.path import io.ktor.request.path
import io.ktor.response.respond
import io.ktor.response.respondText import io.ktor.response.respondText
import io.ktor.routing.get import io.ktor.routing.get
import io.ktor.routing.routing import io.ktor.routing.routing
@ -14,6 +18,7 @@ import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty import io.ktor.server.netty.Netty
import org.slf4j.event.Level import org.slf4j.event.Level
data class Author(val name: String, val website: String)
fun main(args: Array<String>) { fun main(args: Array<String>) {
val jsonResponse = """{ val jsonResponse = """{
@ -22,7 +27,6 @@ fun main(args: Array<String>) {
"description": "Pay water bill today", "description": "Pay water bill today",
}""" }"""
data class Author(val name: String, val website: String)
embeddedServer(Netty, 8080) { embeddedServer(Netty, 8080) {
install(DefaultHeaders) { install(DefaultHeaders) {
@ -33,15 +37,18 @@ fun main(args: Array<String>) {
filter { call -> call.request.path().startsWith("/todo") } filter { call -> call.request.path().startsWith("/todo") }
filter { call -> call.request.path().startsWith("/author") } filter { call -> call.request.path().startsWith("/author") }
} }
install(ContentNegotiation) {
gson {
setPrettyPrinting()
}
}
routing { routing {
get("/todo") { get("/todo") {
call.respondText(jsonResponse, ContentType.Application.Json) call.respondText(jsonResponse, ContentType.Application.Json)
} }
get("/author") { get("/author") {
val author = Author("baeldung", "baeldung.com") val author = Author("baeldung", "baeldung.com")
val gson = Gson() call.respond(author)
val json = gson.toJson(author)
call.respondText(json, ContentType.Application.Json)
} }