Skip to content

Instantly share code, notes, and snippets.

@saltyJeff
Created September 7, 2018 00:11
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 saltyJeff/62a2e865f73cc31d446543dabc6b4d87 to your computer and use it in GitHub Desktop.
Save saltyJeff/62a2e865f73cc31d446543dabc6b4d87 to your computer and use it in GitHub Desktop.
Ktor App Code
fun Application.main() {
val log = LoggerFactory.getLogger("Main App")
install(DefaultHeaders)
install(CallLogging)
install(ContentNegotiation) {
register(ContentType.Application.Json, GsonConverter())
}
install(StatusPages) {
exception<Throwable> { cause ->
log.error("Unhandled error", cause)
val status = when (cause) {
is AccessControlException -> HttpStatusCode.Forbidden
else -> HttpStatusCode.InternalServerError
}
call.respondText("Error: ${cause}", status = status)
}
}
install(Authentication) {
basic {
realm = "Y U NO WORK"
validate { credentials ->
null
}
}
}
routing {
authenticate {
get("/posts/{path}") {
val path = call.parameters["path"]
val principal = call.authentication.principal<UserIdPrincipal>()!!
call.respond (HttpStatusCode.Accepted) //if I replace this with call.respondText, it can listen for HTTP requests
//but if it is call.respond(Any), it dies before it can even start listening
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment