<dependency>
<groupId>com.github.kimar</groupId>
<artifactId>Ktor-Middleware</artifactId>
<version>0.2.2</version>
</dependency>
compile 'com.github.kimar:Ktor-Middleware:0.2.2'
Let's assume you've got an upload
route you'd like to secure using BearerAuthentication
, one way would be to install this routing using Ktor-Middleware
which will then, in turn, automatically filter all failed middlewares and report and error accordingly, e.g.:
fun Application.module() {
...
install(Routing) {
upload(listOf(BearerAuthentication("secret_token"))
}
}
fun Routing.upload(middlewares: List<Middleware<Unit, HttpStatus>>): Route {
return post("upload") {
val eval = middlewares.evaluate(this)
when(eval.success) {
true -> success(call)
else -> call.respondText(eval.result!!.gsonify(), ContentType.Application.Json)
}
}
}
private fun success(call: ApplicationCall) {
...
}