Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kimar/ktor-middleware

A dead-simple Middleware Implementation for the Ktor Web Framework
https://github.com/kimar/ktor-middleware

backend kotlin ktor maven middleware webapp

Last synced: 9 days ago
JSON representation

A dead-simple Middleware Implementation for the Ktor Web Framework

Awesome Lists containing this project

README

        

# Ktor-Middleware

## A dead-simple middleware for Kotlin/Ktor

### Installation

### pom.xml

```java

com.github.kimar
Ktor-Middleware
0.2.2

```

### build.gradle

```java
compile 'com.github.kimar:Ktor-Middleware:0.2.2'
```

### Usage

#### Inside your route handler

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.:

```java

fun Application.module() {
...
install(Routing) {
upload(listOf(BearerAuthentication("secret_token"))
}
}

fun Routing.upload(middlewares: List>): 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) {
...
}
```