rocketraman
11/08/2016, 11:34 PMpackage org.kotlin.community.web.helloworld.ktor
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import org.jetbrains.ktor.application.*
import org.jetbrains.ktor.content.*
import org.jetbrains.ktor.features.*
import org.jetbrains.ktor.http.HttpStatusCode.Companion.OK
import org.jetbrains.ktor.jetty.embeddedJettyServer
import org.jetbrains.ktor.response.*
import org.jetbrains.ktor.routing.*
import org.kotlin.community.web.helloworld.common.*
import java.io.InputStream
inline fun <reified T: Any> ApplicationCall.fromJson(mapper: ObjectMapper): T =
mapper.readValue<T>(request.content.get<InputStream>(), T::class.java)
data class Foo(val s: String)
/*
fun main(args: Array<String>) {
embeddedJettyServer(8080) {
configure()
}.start(true)
}
*/
fun Application.main() {
install(DefaultHeaders)
routing {
get("/") {
call.respondText(HELLO_WORLD)
}
post("/foo") {
val foo = call.fromJson<Foo>(ObjectMapper().registerModule(KotlinModule()))
println(foo)
call.respond(OK)
}
serveClasspathResources("public")
}
}