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: about 1 year ago
JSON representation
A dead-simple Middleware Implementation for the Ktor Web Framework
- Host: GitHub
- URL: https://github.com/kimar/ktor-middleware
- Owner: kimar
- Created: 2017-10-03T10:10:11.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-01-19T10:44:55.000Z (over 5 years ago)
- Last Synced: 2025-02-09T03:14:50.563Z (over 1 year ago)
- Topics: backend, kotlin, ktor, maven, middleware, webapp
- Language: Kotlin
- Homepage:
- Size: 32.2 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
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) {
...
}
```