Skip to content

Instantly share code, notes, and snippets.

@mattrussell-sonocent
Created March 8, 2019 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattrussell-sonocent/faa63a417fd3b3f7424de93d4a5e7d1f to your computer and use it in GitHub Desktop.
Save mattrussell-sonocent/faa63a417fd3b3f7424de93d4a5e7d1f to your computer and use it in GitHub Desktop.
Ktor example
package com.example
import com.fasterxml.jackson.databind.SerializationFeature
import io.ktor.application.Application
import io.ktor.application.call
import io.ktor.application.install
import io.ktor.features.ContentNegotiation
import io.ktor.http.ContentType
import io.ktor.jackson.jackson
import io.ktor.request.receive
import io.ktor.response.respond
import io.ktor.response.respondText
import io.ktor.routing.get
import io.ktor.routing.post
import io.ktor.routing.routing
data class Credentials(val username: String, val password: String)
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
install(ContentNegotiation) {
jackson {
enable(SerializationFeature.INDENT_OUTPUT)
}
}
routing {
get("/") {
call.respondText("HELLO WORLD!", contentType = ContentType.Text.Plain)
}
post("/login") {
val credentials = call.receive<Credentials>()
call.respond(credentials)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment